Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue BerkeleyTrue/actions.js

Last active Sep 20, 2016
Embed
What would you like to do?
redux create types
import createTypes from 'redux-create-types';
export const ns = 'app';
export const types = createTypes(
[
'openModal',
'onClick',
'updateEmailSettings',
createAsyncTypes('fetch')
],
ns
);
// types = {
// openModal: 'app.openModal',
// onClick: 'app.onClick',
// updateEmailSettings: 'app.updateEmailSettings'
// fetch: {
// start: 'app.fetch.start',
// next: 'app.fetch.next',
// error: 'app.fetch.error',
// completed: 'app.fetch.compeleted',
// toString() { return 'app.fetch'; }
// }
// };
export const openModal = () => ({ type: types.openModal });
export const updateEmailSettings = () => ({ type: types.updateEmailSettings });
export const config = {
delimiter: '.',
next: 'next',
start: 'start',
error: 'error',
completed: 'completed'
};
export function createTypes(types, ns, delimiter = config.delimiter) {
const delimitedWithNs = ns ? nd + delimiter : '';
if (!Array.isArray(types)) {
return {};
}
return types.reduce((types, type) => {
if (typeof type === 'string') {
types[type] = ns + delimiter + type;
} else if (type && typeof type === 'object' && typeof type.toString === 'function') {
types[type.toString()] = Object.keys(type).reduce((typeObj, type) => {
if (type === 'toString') {
typeObj.toString = function toString() { return ns + delimiter + type() };
} else {
typeObj[type] = ns + delimiter + type;
}
return typeObj;
});
}
return types;
}, {});
}
export function createAsyncTypes(type, delimiter = config.delimiter) {
return {
start: type + delimiter + config.start,
next: type + delimiter + config.next,
error: type + delimiter + config.error,
completed: type + delimter + config.completed,
toString() { return type }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.