Skip to content

Commit 3545577

Browse files
committed
reducer refactoring. move logic to dispatchers
1 parent 7217aca commit 3545577

File tree

14 files changed

+112
-96
lines changed

14 files changed

+112
-96
lines changed

lib/actions/alert.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
"use strict";
22
var _types_1 = require('./_types');
3-
function alertToggle(alert) {
3+
function alertToggle(alert, filter) {
44
return function (dispatch, getState) {
5-
var isOpen = getState().alert.open;
6-
if (!alert) {
7-
alert = {
8-
action: '',
9-
message: '',
10-
open: false,
11-
};
12-
}
13-
else {
14-
alert = Object.assign({}, { open: !isOpen }, alert);
15-
}
165
dispatch({ type: _types_1.ALERT_TOGGLE, payload: { alert: alert } });
176
};
187
}

lib/actions/page.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
var _types_1 = require('./_types');
33
var config_task_tests_1 = require('./config-task-tests');
4+
var hint_1 = require('./hint');
45
function pageNext() {
56
return function (dispatch, getState) {
67
var _a = getState(), pagePosition = _a.pagePosition, tutorial = _a.tutorial;
@@ -26,6 +27,7 @@ function pageSet(pagePosition) {
2627
dispatch({
2728
type: _types_1.PAGE_SET, payload: { dir: dir, pagePosition: pagePosition, tutorial: tutorial, progress: progress, tasks: tasks }
2829
});
30+
dispatch(hint_1.hintPositionSet(0));
2931
};
3032
}
3133
exports.pageSet = pageSet;

lib/actions/progress.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
var _types_1 = require('./_types');
3+
var alert_1 = require('./alert');
34
function progressPagePositionLoad() {
45
return function (dispatch, getState) {
56
var progress = getState().progress;
@@ -18,17 +19,27 @@ exports.progressLoad = progressLoad;
1819
function completePage() {
1920
return function (dispatch, getState) {
2021
var _a = getState(), pagePosition = _a.pagePosition, progress = _a.progress, tutorial = _a.tutorial;
22+
dispatch({ type: _types_1.COMPLETE_PAGE, payload: { pagePosition: pagePosition, tutorial: tutorial } });
2123
if (progress.pages.every(function (x) { return x.completed; })) {
2224
dispatch(completeTutorial());
2325
}
24-
dispatch({ type: _types_1.COMPLETE_PAGE, payload: { pagePosition: pagePosition, tutorial: tutorial } });
26+
else {
27+
dispatch(alert_1.alertToggle({
28+
message: "Page " + (pagePosition + 1) + " Complete",
29+
action: 'pass',
30+
}));
31+
}
2532
};
2633
}
2734
exports.completePage = completePage;
2835
function completeTutorial() {
2936
return function (dispatch, getState) {
3037
var tutorial = getState().tutorial;
3138
dispatch({ type: _types_1.COMPLETE_TUTORIAL, payload: { tutorial: tutorial } });
39+
dispatch(alert_1.alertToggle({
40+
message: 'Tutorial Complete',
41+
action: 'pass',
42+
}));
3243
};
3344
}
3445
exports.completeTutorial = completeTutorial;

lib/actions/test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
var _types_1 = require('./_types');
3+
var hint_1 = require('./hint');
34
function testRun() {
45
return function (dispatch, getState) {
56
var _a = getState(), taskTests = _a.taskTests, dir = _a.dir, tutorial = _a.tutorial, taskPosition = _a.taskPosition;
@@ -12,10 +13,24 @@ exports.testRun = testRun;
1213
function testResult(result) {
1314
return function (dispatch, getState) {
1415
var taskActions = getState().taskActions;
15-
dispatch({ type: _types_1.TEST_RESULT, payload: { result: result, taskActions: taskActions } });
16+
var filter = getTestFilter(result);
17+
if (result.change !== 0) {
18+
dispatch(hint_1.hintPositionSet(0));
19+
}
20+
dispatch({ type: _types_1.TEST_RESULT, payload: { result: result, taskActions: taskActions }, filter: filter });
1621
};
1722
}
1823
exports.testResult = testResult;
24+
function getTestFilter(result) {
25+
switch (true) {
26+
case result.pass && result.change > 0:
27+
return 'PASS';
28+
case result.pass === false && result.change < 1:
29+
return 'FAIL';
30+
default:
31+
return 'NOTE';
32+
}
33+
}
1934
function testComplete() {
2035
return { type: _types_1.TEST_COMPLETE };
2136
}

lib/actions/tutorial.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ function tutorialSet(name) {
88
}
99
exports.tutorialSet = tutorialSet;
1010
function tutorialUpdate(name) {
11-
return { type: _types_1.TUTORIAL_UPDATE, payload: { name: name } };
11+
return function (dispatch, getState) {
12+
var alert = {
13+
message: "run `npm install --save-dev " + name + "`",
14+
action: 'note',
15+
duration: 4000,
16+
};
17+
dispatch({ type: _types_1.TUTORIAL_UPDATE, payload: { name: name } });
18+
dispatch({ type: _types_1.ALERT_TOGGLE, payload: { alert: alert } });
19+
};
1220
}
1321
exports.tutorialUpdate = tutorialUpdate;
1422
function tutorialsFind() {

lib/reducers/alert/index.js

+12-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ var _types_1 = require('../../actions/_types');
33
var _alert = {
44
message: '',
55
open: false,
6-
action: '',
6+
action: 'note',
7+
duration: 1500,
78
};
89
var open = {
910
open: true,
10-
action: 'pass',
11+
action: 'note',
12+
duration: 1500
1113
};
1214
var current = _alert;
1315
function setAlert(options, color) {
@@ -24,43 +26,32 @@ function alertReducer(alert, action) {
2426
case _types_1.ALERT_REPLAY:
2527
return setAlert(current);
2628
case _types_1.ALERT_TOGGLE:
27-
return action.payload.alert || _alert;
29+
return setAlert(action.payload.alert || _alert);
2830
case _types_1.TUTORIAL_UPDATE:
2931
return setAlert({
3032
message: "run `npm install --save-dev " + action.payload.name + "`",
31-
action: 'note',
3233
duration: 4000,
3334
});
3435
case _types_1.TEST_RESULT:
3536
var result = action.payload.result;
36-
switch (true) {
37-
case result.pass && result.change > 0:
37+
switch (action.payload.filter) {
38+
case 'PASS':
3839
return setAlert({
3940
message: result.msg,
40-
duration: result.duration || 1500,
41+
action: 'pass',
42+
duration: result.duration || 1200,
4143
}, '#73C990');
42-
case result.pass === false && result.change < 1:
44+
case 'FAIL':
4345
return setAlert({
4446
message: result.msg,
4547
action: 'fail',
46-
duration: result.duration || 2500,
48+
duration: result.duration || 2200,
4749
}, '#FF4081');
48-
default:
49-
break;
5050
}
5151
return setAlert({
5252
message: result.msg,
53-
action: 'note',
54-
duration: result.duration || 2500,
53+
duration: result.duration || 2200,
5554
}, '#9DA5B4');
56-
case _types_1.COMPLETE_PAGE:
57-
return setAlert({
58-
message: "Page " + (action.payload.pagePosition + 1) + " Complete",
59-
});
60-
case _types_1.COMPLETE_TUTORIAL:
61-
return setAlert({
62-
message: 'Tutorial Complete',
63-
});
6455
default:
6556
return alert;
6657
}

lib/reducers/hint-position/index.js

-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ var _types_1 = require('../../actions/_types');
33
function hintPositionReducer(hintPosition, action) {
44
if (hintPosition === void 0) { hintPosition = 0; }
55
switch (action.type) {
6-
case _types_1.PAGE_SET:
7-
return 0;
8-
case _types_1.TEST_RESULT:
9-
if (action.payload.result.change !== 0) {
10-
return 0;
11-
}
12-
return hintPosition;
136
case _types_1.HINT_POSITION_SET:
147
return action.payload.hintPosition;
158
default:

src/actions/alert.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
import {ALERT_TOGGLE, ALERT_REPLAY} from './_types';
1+
import {ALERT_REPLAY, ALERT_TOGGLE} from './_types';
22

3-
export function alertToggle(alert?: CR.Alert): ReduxThunk.ThunkInterface {
3+
export function alertToggle(alert: Object, filter?: string): ReduxThunk.ThunkInterface {
44
return (dispatch, getState): void => {
5-
const isOpen = getState().alert.open;
6-
if (!alert) {
7-
alert = {
8-
action: '',
9-
message: '',
10-
open: false,
11-
};
12-
} else {
13-
alert = Object.assign({}, { open: !isOpen }, alert);
14-
}
155
dispatch({ type: ALERT_TOGGLE, payload: { alert } });
166
};
177
}

src/actions/page.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ROUTE_SET, PAGE_SET, PAGE_POSITION_SET
33
} from './_types';
44
import configTaskTests from './config-task-tests';
5+
import {hintPositionSet} from './hint';
56

67
export function pageNext(): ReduxThunk.ThunkInterface | Action {
78
return (dispatch, getState): void => {
@@ -29,6 +30,7 @@ export function pageSet(pagePosition = 0): ReduxThunk.ThunkInterface {
2930
dispatch({
3031
type: PAGE_SET, payload: { dir, pagePosition, tutorial, progress, tasks }
3132
});
33+
dispatch(hintPositionSet(0));
3234
};
3335
}
3436

src/actions/progress.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
PROGRESS_PAGE_POSITION_LOAD, PROGRESS_LOAD, COMPLETE_PAGE, COMPLETE_TUTORIAL
33
} from './_types';
4+
import {alertToggle} from './alert';
45

56
export function progressPagePositionLoad(): ReduxThunk.ThunkInterface {
67
return (dispatch, getState): void => {
@@ -22,16 +23,25 @@ export function completePage(): ReduxThunk.ThunkInterface {
2223
return (dispatch, getState): void => {
2324
const {pagePosition, progress, tutorial} = getState();
2425
// all pages are true, tutorial complete
26+
dispatch({ type: COMPLETE_PAGE, payload: { pagePosition, tutorial } });
2527
if (progress.pages.every(x => x.completed)) {
2628
dispatch(completeTutorial());
29+
} else {
30+
dispatch(alertToggle({
31+
message: `Page ${pagePosition + 1} Complete`,
32+
action: 'pass',
33+
}));
2734
}
28-
dispatch({ type: COMPLETE_PAGE, payload: { pagePosition, tutorial } });
2935
};
3036
}
3137

3238
export function completeTutorial(): ReduxThunk.ThunkInterface {
3339
return (dispatch, getState): void => {
3440
const {tutorial} = getState();
3541
dispatch({ type: COMPLETE_TUTORIAL, payload: { tutorial } });
42+
dispatch(alertToggle({
43+
message: 'Tutorial Complete',
44+
action: 'pass',
45+
}));
3646
};
3747
}

src/actions/test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
TEST_RUN, TEST_RESULT, TEST_COMPLETE, TEST_SAVE
33
} from './_types';
4+
import {hintPositionSet} from './hint';
45

56
export function testRun(): ReduxThunk.ThunkInterface {
67
return (dispatch, getState): void => {
@@ -14,10 +15,25 @@ export function testRun(): ReduxThunk.ThunkInterface {
1415
export function testResult(result: Test.Result): ReduxThunk.ThunkInterface {
1516
return (dispatch, getState): void => {
1617
const {taskActions} = getState();
17-
dispatch({ type: TEST_RESULT, payload: { result, taskActions } });
18+
const filter: string = getTestFilter(result);
19+
if (result.change !== 0) {
20+
dispatch(hintPositionSet(0));
21+
}
22+
dispatch({ type: TEST_RESULT, payload: { result, taskActions }, filter });
1823
};
1924
}
2025

26+
function getTestFilter(result: Test.Result): string {
27+
switch (true) {
28+
case result.pass && result.change > 0:
29+
return 'PASS';
30+
case result.pass === false && result.change < 1:
31+
return 'FAIL';
32+
default:
33+
return 'NOTE';
34+
}
35+
}
36+
2137
export function testComplete(): Action {
2238
return { type: TEST_COMPLETE };
2339
}

src/actions/tutorial.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
2-
TUTORIALS_FIND, TUTORIAL_UPDATE, TUTORIAL_SET
2+
TUTORIALS_FIND, TUTORIAL_UPDATE, TUTORIAL_SET,
3+
ALERT_TOGGLE
34
} from './_types';
45

56
export function tutorialSet(name: string): ReduxThunk.ThunkInterface {
@@ -9,8 +10,16 @@ export function tutorialSet(name: string): ReduxThunk.ThunkInterface {
910
};
1011
}
1112

12-
export function tutorialUpdate(name: string): Action {
13-
return { type: TUTORIAL_UPDATE, payload: { name } };
13+
export function tutorialUpdate(name: string): ReduxThunk.ThunkInterface {
14+
return (dispatch, getState): void => {
15+
const alert = {
16+
message: `run \`npm install --save-dev ${name}\``,
17+
action: 'note',
18+
duration: 4000,
19+
};
20+
dispatch({ type: TUTORIAL_UPDATE, payload: { name } });
21+
dispatch({ type: ALERT_TOGGLE, payload: { alert } });
22+
};
1423
}
1524

1625
export function tutorialsFind(): ReduxThunk.ThunkInterface {

0 commit comments

Comments
 (0)