Skip to content

Commit 67d823c

Browse files
committed
refactor alert actions/reducers
1 parent 3d63d12 commit 67d823c

File tree

12 files changed

+67
-73
lines changed

12 files changed

+67
-73
lines changed

lib/actions/alert.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use strict";
22
var _types_1 = require('./_types');
3-
function alertToggle(alert, filter) {
3+
function alertToggle(alert) {
44
return function (dispatch, getState) {
5-
dispatch({ type: _types_1.ALERT_TOGGLE, payload: { alert: alert }, filter: filter });
5+
dispatch({ type: _types_1.ALERT_TOGGLE, payload: { alert: alert } });
66
};
77
}
88
exports.alertToggle = alertToggle;

lib/actions/progress.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function completePage(completed) {
3030
else {
3131
dispatch(alert_1.alertToggle({
3232
message: "Page " + (pagePosition + 1) + " Complete",
33-
action: 'pass',
33+
action: 'PASS',
3434
}));
3535
}
3636
}
@@ -47,7 +47,7 @@ function completeTutorial(completed) {
4747
dispatch({ type: _types_1.COMPLETE_TUTORIAL, payload: { tutorial: tutorial, completed: completed } });
4848
dispatch(alert_1.alertToggle({
4949
message: 'Tutorial Complete',
50-
action: 'pass',
50+
action: 'PASS',
5151
}));
5252
};
5353
}

lib/actions/test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var _types_1 = require('./_types');
33
var hint_1 = require('./hint');
44
var progress_1 = require('./progress');
5+
var alert_1 = require('./alert');
56
function testRun() {
67
return function (dispatch, getState) {
78
var _a = getState(), taskTests = _a.taskTests, dir = _a.dir, tutorial = _a.tutorial, taskPosition = _a.taskPosition;
@@ -15,13 +16,26 @@ function testResult(result) {
1516
return function (dispatch, getState) {
1617
var _a = getState(), taskActions = _a.taskActions, progress = _a.progress, pagePosition = _a.pagePosition;
1718
var filter = getTestFilter(result);
19+
var alert = {
20+
message: result.msg,
21+
action: 'note',
22+
};
1823
if (filter === 'PASS' || filter === 'FAIL') {
1924
dispatch(hint_1.hintPositionSet(0));
25+
alert = Object.assign({}, alert, {
26+
action: filter,
27+
duration: 1200,
28+
});
2029
}
21-
if (filter === 'FAIL' && progress.pages[pagePosition]) {
30+
else if (filter === 'FAIL' && progress.pages[pagePosition]) {
2231
dispatch(progress_1.completePage(false));
32+
alert = Object.assign({}, alert, {
33+
action: filter,
34+
duration: 2200,
35+
});
2336
}
24-
dispatch({ type: _types_1.TEST_RESULT, payload: { result: result, taskActions: taskActions }, filter: filter });
37+
dispatch({ type: _types_1.TEST_RESULT, payload: { result: result, taskActions: taskActions } });
38+
dispatch(alert_1.alertToggle(alert));
2539
};
2640
}
2741
exports.testResult = testResult;

lib/components/Tutorials/LoadTutorials/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ var __metadata = (this && this.__metadata) || function (k, v) {
1616
var React = require('react');
1717
var react_redux_1 = require('react-redux');
1818
var actions_1 = require('../../../actions');
19-
var RaisedButton_1 = require('material-ui/RaisedButton');
19+
var FlatButton_1 = require('material-ui/FlatButton');
2020
var LoadTutorials = (function (_super) {
2121
__extends(LoadTutorials, _super);
2222
function LoadTutorials() {
2323
_super.apply(this, arguments);
2424
}
2525
LoadTutorials.prototype.render = function () {
26-
return (React.createElement(RaisedButton_1.default, {label: 'Check for Tutorials', secondary: true, onTouchTap: this.props.tutorialsFind}));
26+
return (React.createElement(FlatButton_1.default, {label: 'Check for Tutorials', secondary: true, onTouchTap: this.props.tutorialsFind}));
2727
};
2828
LoadTutorials = __decorate([
2929
react_redux_1.connect(null, function (dispatch) {

lib/reducers/alert/index.js

+9-25
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ var open = {
1111
action: 'note',
1212
duration: 1500
1313
};
14+
var colors = {
15+
pass: '#73C990',
16+
FAIL: '#FF4081',
17+
NOTE: '#9DA5B4',
18+
};
1419
var current = _alert;
1520
function setAlert(options, color) {
1621
if (color) {
@@ -26,32 +31,11 @@ function alertReducer(alert, action) {
2631
case _types_1.ALERT_REPLAY:
2732
return setAlert(current);
2833
case _types_1.ALERT_TOGGLE:
29-
return setAlert(action.payload.alert || _alert);
30-
case _types_1.TUTORIAL_UPDATE:
31-
return setAlert({
32-
message: "run `npm install --save-dev " + action.payload.name + "`",
33-
duration: 4000,
34-
});
35-
case _types_1.TEST_RESULT:
36-
var result = action.payload.result;
37-
switch (action.filter) {
38-
case 'PASS':
39-
return setAlert({
40-
message: result.msg,
41-
action: 'pass',
42-
duration: result.duration || 1200,
43-
}, '#73C990');
44-
case 'FAIL':
45-
return setAlert({
46-
message: result.msg,
47-
action: 'fail',
48-
duration: result.duration || 2200,
49-
}, '#FF4081');
34+
var a = action.payload.alert;
35+
if (!a) {
36+
return _alert;
5037
}
51-
return setAlert({
52-
message: result.msg,
53-
duration: result.duration || 2200,
54-
}, '#9DA5B4');
38+
return setAlert(a, colors[a.action] || colors.NOTE);
5539
default:
5640
return alert;
5741
}

lib/store/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var reducers_1 = require('../reducers');
44
var redux_thunk_1 = require('redux-thunk');
55
var createLogger = require('redux-logger');
66
var middlewares = [redux_thunk_1.default];
7-
var devMode = false;
7+
var devMode = true;
88
if (devMode) {
99
var logger = createLogger();
1010
middlewares.push(logger);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
"material-ui": "0.15.0",
4444
"react": "15.0.2",
4545
"react-dom": "15.0.2",
46-
"react-redux": "4.4.5",
4746
"react-tap-event-plugin": "1.0.0",
47+
"react-redux": "4.4.5",
4848
"redux": "3.5.2",
4949
"redux-logger": "2.6.1",
5050
"redux-thunk": "^2.0.1"

src/actions/alert.ts

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

3-
export function alertToggle(alert: Object, filter?: string): ReduxThunk.ThunkInterface {
3+
export function alertToggle(alert: Object): ReduxThunk.ThunkInterface {
44
return (dispatch, getState): void => {
5-
dispatch({ type: ALERT_TOGGLE, payload: { alert }, filter });
5+
dispatch({ type: ALERT_TOGGLE, payload: { alert } });
66
};
77
}
88

src/actions/progress.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function completePage(completed = true): ReduxThunk.ThunkInterface {
3232
} else {
3333
dispatch(alertToggle({
3434
message: `Page ${pagePosition + 1} Complete`,
35-
action: 'pass',
35+
action: 'PASS',
3636
}));
3737
}
3838
} else if (progress.completed) {
@@ -47,7 +47,7 @@ export function completeTutorial(completed = true): ReduxThunk.ThunkInterface {
4747
dispatch({ type: COMPLETE_TUTORIAL, payload: { tutorial, completed } });
4848
dispatch(alertToggle({
4949
message: 'Tutorial Complete',
50-
action: 'pass',
50+
action: 'PASS',
5151
}));
5252
};
5353
}

src/actions/test.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
} from './_types';
44
import {hintPositionSet} from './hint';
55
import {completePage} from './progress';
6+
import {alertToggle} from './alert';
67

78
export function testRun(): ReduxThunk.ThunkInterface {
89
return (dispatch, getState): void => {
@@ -17,13 +18,25 @@ export function testResult(result: Test.Result): ReduxThunk.ThunkInterface {
1718
return (dispatch, getState): void => {
1819
const {taskActions, progress, pagePosition} = getState();
1920
const filter: string = getTestFilter(result);
21+
let alert: CR.Alert = {
22+
message: result.msg,
23+
action: 'note',
24+
};
2025
if (filter === 'PASS' || filter === 'FAIL') {
2126
dispatch(hintPositionSet(0));
22-
}
23-
if (filter === 'FAIL' && progress.pages[pagePosition]) {
27+
alert = Object.assign({}, alert, {
28+
action: filter,
29+
duration: 1200,
30+
});
31+
} else if (filter === 'FAIL' && progress.pages[pagePosition]) {
2432
dispatch(completePage(false));
33+
alert = Object.assign({}, alert, {
34+
action: filter,
35+
duration: 2200,
36+
});
2537
}
26-
dispatch({ type: TEST_RESULT, payload: { result, taskActions }, filter });
38+
dispatch({ type: TEST_RESULT, payload: { result, taskActions } });
39+
dispatch(alertToggle(alert));
2740
};
2841
}
2942

src/reducers/alert/index.ts

+13-30
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ const _alert: CR.Alert = {
99
action: 'note',
1010
duration: 1500,
1111
};
12+
1213
const open = {
1314
open: true,
1415
action: 'note',
1516
duration: 1500
1617
};
1718

19+
const colors = {
20+
pass: '#73C990',
21+
FAIL: '#FF4081',
22+
NOTE: '#9DA5B4',
23+
};
24+
1825
let current: CR.Alert = _alert;
1926

2027
function setAlert(options: Object, color?: string) {
@@ -35,38 +42,14 @@ export default function alertReducer(
3542
return setAlert(current);
3643

3744
case ALERT_TOGGLE:
38-
return setAlert(action.payload.alert || _alert);
39-
40-
case TUTORIAL_UPDATE:
41-
return setAlert({
42-
message: `run \`npm install --save-dev ${action.payload.name}\``,
43-
duration: 4000,
44-
});
45+
const a = action.payload.alert;
4546

46-
case TEST_RESULT:
47-
const result = action.payload.result;
48-
49-
switch (action.filter) {
50-
51-
case 'PASS':
52-
return setAlert({
53-
message: result.msg,
54-
action: 'pass',
55-
duration: result.duration || 1200,
56-
}, '#73C990');
57-
58-
case 'FAIL':
59-
return setAlert({
60-
message: result.msg,
61-
action: 'fail',
62-
duration: result.duration || 2200,
63-
}, '#FF4081');
47+
if (!a) {
48+
// close alert
49+
return _alert;
6450
}
65-
// Note
66-
return setAlert({
67-
message: result.msg,
68-
duration: result.duration || 2200,
69-
}, '#9DA5B4');
51+
52+
return setAlert(a, colors[a.action] || colors.NOTE);
7053

7154
default:
7255
return alert;

src/store/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as createLogger from 'redux-logger';
55

66
const middlewares = [thunk];
77

8-
const devMode = false;
8+
const devMode = true;
99
if (devMode) {
1010
const logger = createLogger();
1111
middlewares.push(logger);

0 commit comments

Comments
 (0)