Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Last active September 20, 2016 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BerkeleyTrue/9be6d4415e4aedf9d02c256072821aa6 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/9be6d4415e4aedf9d02c256072821aa6 to your computer and use it in GitHub Desktop.
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