2016-12-08 09:42:50 +01:00
|
|
|
/*
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-12-08 09:42:50 +01:00
|
|
|
HoC that provides access to flash messages stored in Redux state and actions to operate on them
|
2016-02-23 13:10:08 +09:00
|
|
|
|
2016-12-08 09:42:50 +01:00
|
|
|
*/
|
2016-03-27 17:30:28 +09:00
|
|
|
|
2017-03-23 16:27:59 +09:00
|
|
|
import { getActions, addAction, addReducer } from 'meteor/vulcan:lib';
|
2016-12-08 09:42:50 +01:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-02-23 13:10:08 +09:00
|
|
|
|
2017-01-26 09:56:53 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Messages actions
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
addAction({
|
|
|
|
messages: {
|
|
|
|
flash(content, flashType) {
|
|
|
|
return {
|
|
|
|
type: 'FLASH',
|
|
|
|
content,
|
|
|
|
flashType,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
clear(i) {
|
|
|
|
return {
|
|
|
|
type: 'CLEAR',
|
|
|
|
i,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
markAsSeen(i) {
|
|
|
|
return {
|
|
|
|
type: 'MARK_AS_SEEN',
|
|
|
|
i,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
clearSeen() {
|
|
|
|
return {
|
|
|
|
type: 'CLEAR_SEEN'
|
|
|
|
};
|
|
|
|
},
|
2016-10-31 17:13:14 +01:00
|
|
|
}
|
2017-01-04 14:31:38 +01:00
|
|
|
});
|
|
|
|
|
2017-01-26 09:56:53 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Messages reducers
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
addReducer({
|
|
|
|
messages: (state = [], action) => {
|
2017-01-09 13:33:26 +01:00
|
|
|
// default values
|
|
|
|
const flashType = typeof action.flashType === 'undefined' ? 'error' : action.flashType;
|
2017-01-26 12:41:02 +08:00
|
|
|
const currentMsg = typeof action.i === 'undefined' ? {} : state[action.i];
|
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
switch(action.type) {
|
|
|
|
case 'FLASH':
|
|
|
|
return [
|
|
|
|
...state,
|
2017-01-09 13:33:26 +01:00
|
|
|
{ _id: state.length, content: action.content, flashType, seen: false, show: true },
|
2017-01-04 14:31:38 +01:00
|
|
|
];
|
|
|
|
case 'MARK_AS_SEEN':
|
|
|
|
return [
|
|
|
|
...state.slice(0, action.i),
|
|
|
|
{ ...currentMsg, seen: true },
|
|
|
|
...state.slice(action.i + 1),
|
|
|
|
];
|
2017-01-26 12:41:02 +08:00
|
|
|
case 'CLEAR':
|
2017-01-04 14:31:38 +01:00
|
|
|
return [
|
|
|
|
...state.slice(0, action.i),
|
|
|
|
{ ...currentMsg, show: false },
|
|
|
|
...state.slice(action.i + 1),
|
|
|
|
];
|
|
|
|
case 'CLEAR_SEEN':
|
2017-01-09 13:33:26 +01:00
|
|
|
return state.map(message => message.seen ? { ...message, show: false } : message);
|
2017-01-04 14:31:38 +01:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2016-10-31 17:13:14 +01:00
|
|
|
|
2017-01-26 09:56:53 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
withMessages HOC
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-12-08 09:42:50 +01:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages, });
|
2017-01-26 12:41:02 +08:00
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(getActions().messages, dispatch);
|
2016-12-08 09:42:50 +01:00
|
|
|
|
2017-01-09 13:33:26 +01:00
|
|
|
const withMessages = component => connect(mapStateToProps, mapDispatchToProps)(component);
|
2016-12-12 16:43:23 +09:00
|
|
|
|
2017-01-04 14:31:38 +01:00
|
|
|
export default withMessages;
|