2018-01-04 16:24:03 -08:00
|
|
|
import _ from 'lodash/fp';
|
2018-01-04 16:39:43 -08:00
|
|
|
import {
|
|
|
|
createTypes,
|
|
|
|
createAction,
|
|
|
|
composeReducers,
|
|
|
|
handleActions
|
|
|
|
} from 'berkeleys-redux-utils';
|
2018-01-04 09:18:20 -08:00
|
|
|
|
2018-01-04 16:24:03 -08:00
|
|
|
import ns from '../ns.json';
|
|
|
|
|
2018-01-04 16:39:43 -08:00
|
|
|
export const types = createTypes([
|
|
|
|
'clickOnClose'
|
|
|
|
], ns);
|
|
|
|
|
|
|
|
export const clickOnClose = createAction(types.clickOnClose, _.noop);
|
|
|
|
|
2018-01-04 16:24:03 -08:00
|
|
|
export const alertTypes = _.keyBy(_.identity)([
|
|
|
|
'success',
|
|
|
|
'info',
|
|
|
|
'warning',
|
|
|
|
'danger'
|
|
|
|
]);
|
|
|
|
|
|
|
|
export const getFlashAction = _.flow(
|
|
|
|
_.property('meta'),
|
|
|
|
_.property('flash')
|
|
|
|
);
|
|
|
|
|
|
|
|
export const isFlashAction = _.flow(
|
|
|
|
getFlashAction,
|
|
|
|
Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
stack: [{ alertType: 'danger', message: 'foo bar' }]
|
|
|
|
};
|
|
|
|
|
|
|
|
const getNS = _.property(ns);
|
|
|
|
|
|
|
|
export const latestMessageSelector = _.flow(
|
|
|
|
getNS,
|
|
|
|
_.property('stack'),
|
|
|
|
_.head,
|
2018-01-04 16:39:43 -08:00
|
|
|
_.defaultTo({})
|
2018-01-04 16:24:03 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
export default composeReducers(
|
|
|
|
ns,
|
2018-01-04 16:39:43 -08:00
|
|
|
handleActions(
|
|
|
|
() => ({
|
|
|
|
[types.clickOnClose]: (state) => ({
|
|
|
|
...state,
|
|
|
|
stack: _.tail(state.stack)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
defaultState,
|
|
|
|
),
|
2018-01-04 16:24:03 -08:00
|
|
|
function metaReducer(state = defaultState, action) {
|
|
|
|
if (isFlashAction(action)) {
|
|
|
|
const { payload: { alertType, message } } = getFlashAction(action);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
stack: [
|
|
|
|
...state.stack,
|
|
|
|
{
|
|
|
|
alertType: alertTypes[alertType] || 'info',
|
|
|
|
message: _.escape(message)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
);
|