feat(Flash): Normalize flash types with object

help prevent typo errors
This commit is contained in:
Berkeley Martinez
2018-01-29 11:34:44 -08:00
parent ae3ccdd672
commit 1ee9d9259c
4 changed files with 17 additions and 15 deletions

View File

@ -4,7 +4,7 @@ import { CloseButton } from 'react-bootstrap';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import ns from './ns.json'; import ns from './ns.json';
import { alertTypes } from './redux/utils.js'; import { alertTypes } from '../../utils/flash.js';
import { import {
latestMessageSelector, latestMessageSelector,
clickOnClose clickOnClose

View File

@ -1,13 +1,5 @@
import _ from 'lodash/fp'; import _ from 'lodash/fp';
import { alertTypes, normalizeAlertType } from '../../../utils/flash.js';
export const alertTypes = _.keyBy(_.identity)([
'success',
'info',
'warning',
'danger'
]);
export const normalizeAlertType = alertType => alertTypes[alertType] || 'info';
// interface ExpressFlash { // interface ExpressFlash {
// [alertType]: [String...] // [alertType]: [String...]
@ -16,7 +8,6 @@ export const normalizeAlertType = alertType => alertTypes[alertType] || 'info';
// type: AlertType, // type: AlertType,
// message: String // message: String
// } // }
export const expressToStack = _.flow( export const expressToStack = _.flow(
_.toPairs, _.toPairs,
_.flatMap(([ type, messages ]) => messages.map(msg => ({ _.flatMap(([ type, messages ]) => messages.map(msg => ({

10
common/utils/flash.js Normal file
View File

@ -0,0 +1,10 @@
import _ from 'lodash';
export const alertTypes = _.keyBy([
'success',
'info',
'warning',
'danger'
], _.identity);
export const normalizeAlertType = alertType => alertTypes[alertType] || 'info';

View File

@ -6,6 +6,7 @@ import {
} from '../utils/middleware'; } from '../utils/middleware';
import supportedLanguages from '../../common/utils/supported-languages.js'; import supportedLanguages from '../../common/utils/supported-languages.js';
import { themes } from '../../common/utils/themes.js'; import { themes } from '../../common/utils/themes.js';
import { alertTypes } from '../../common/utils/flash.js';
export default function settingsController(app) { export default function settingsController(app) {
const api = app.loopback.Router(); const api = app.loopback.Router();
@ -76,7 +77,7 @@ export default function settingsController(app) {
'/update-my-current-challenge', '/update-my-current-challenge',
ifNoUser401, ifNoUser401,
updateMyCurrentChallengeValidators, updateMyCurrentChallengeValidators,
createValidatorErrorHandler('errors'), createValidatorErrorHandler(alertTypes.danger),
updateMyCurrentChallenge updateMyCurrentChallenge
); );
@ -88,11 +89,11 @@ export default function settingsController(app) {
function updateMyTheme(req, res, next) { function updateMyTheme(req, res, next) {
const { body: { theme } } = req; const { body: { theme } } = req;
if (req.user.theme === theme) { if (req.user.theme === theme) {
return res.sendFlash('info', 'Theme already set'); return res.sendFlash(alertTypes.info, 'Theme already set');
} }
return req.user.updateTheme(theme) return req.user.updateTheme(theme)
.then( .then(
() => res.sendFlash('info', 'Your theme has been updated'), () => res.sendFlash(alertTypes.info, 'Your theme has been updated'),
next next
); );
} }
@ -100,7 +101,7 @@ export default function settingsController(app) {
'/update-my-theme', '/update-my-theme',
ifNoUser401, ifNoUser401,
updateMyThemeValidators, updateMyThemeValidators,
createValidatorErrorHandler('errors'), createValidatorErrorHandler(alertTypes.danger),
updateMyTheme updateMyTheme
); );