Last active
May 31, 2018 09:47
-
-
Save jdltechworks/c5d875c739dd33425a051dbb4c4f3856 to your computer and use it in GitHub Desktop.
mutation types with prefix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createMutationTypes = (opts) => { | |
opts = opts || {}; | |
let separator = opts.separator || '_' | |
if (typeof opts === 'string') { | |
opts = {prefix: opts} | |
} | |
const defineType = (obj, prefix, n, v) => { | |
v = v || [].concat.apply([], [prefix, n]) | |
.join(separator) | |
.toUpperCase() | |
Object.defineProperty(obj, n, { | |
value: v, | |
enumerable: true, | |
writable: false, | |
configurable: false | |
}) | |
return obj | |
} | |
const definer = (obj, prefix) => { | |
return (...definitions) => { | |
definitions.forEach((def) => { | |
defineType(obj, prefix, def); | |
}) | |
return obj | |
} | |
} | |
let prefix = opts.prefix || ''; | |
return definer({}, prefix); | |
} | |
export default createMutationTypes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import createMutationTypes from './mutation-types-generator' | |
export default createMutationTypes('auth')( | |
'LOGGED_IN', | |
'ERRORS', | |
'PENDING' | |
) | |
/* | |
* expected output | |
* { LOGGED_IN: 'AUTH_LOGGED_IN', ERRORS: 'AUTH_ERRORS', PENDING: 'AUTH_PENDING' } | |
* | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types from './mutation-types.js' | |
const mutation = { | |
[types.LOGGED_IN](state, { credentials }) { | |
state.credentials = credentials | |
}, | |
[types.ERRORS](state, { errors}) { | |
state.errors = errors | |
}, | |
[types.PENGING](state, { message }) { | |
state.message = message | |
} | |
} | |
export default mutation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment