Last active
January 9, 2021 17:56
-
-
Save tjarbo/c3f3c6025922fe82c8503c2b6829a8c3 to your computer and use it in GitHub Desktop.
Extended StoreUtil.js - originally written by Larbi JIRARI
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
/** | |
* StoreUtil.js | |
* | |
* Original by | |
* @author Larbi JIRARI | |
* | |
* Extended by | |
* @author Tjark @tjarbo | |
*/ | |
export default class StoreUtil { | |
/** | |
* init the state. | |
* @return {Object} default state | |
*/ | |
static state(data = null) { | |
return { | |
data, | |
status: { | |
pending: false, | |
success: false, | |
fail: false, | |
error: null, | |
}, | |
}; | |
} | |
/** | |
* @param {Object} state - the state to update | |
* @param {Object,Error} data - data to update with | |
* @param {Boolean} clear - clear data during "pending" state | |
* @returns {Object} state after update | |
*/ | |
static updateState(state, data = undefined, clear = false) { | |
if (!state) throw new Error('state object is missing'); | |
if (data === undefined) { | |
// PENDING | |
return this._mutationPending({ ...state }, clear); | |
} | |
if (data === null) { | |
// RESET | |
return this.state(); | |
} | |
// SUCCESS or FAIL | |
return data instanceof Error | |
? this._mutationFail({ ...state }, data) | |
: this._mutationSuccess({ ...state }, data); | |
} | |
/** | |
* @param {Object} state - the state to lock | |
* @returns {Object} locked state | |
*/ | |
static lockState(state) { | |
state.status.pending = true; | |
state.status.success = false; | |
state.status.fail = false; | |
return state; | |
} | |
/** | |
* @param {Object} state - the state to unlock | |
* @param {Boolean} success - set state as successful | |
* @returns {Object} unlocked state | |
*/ | |
static unlockState(state, success) { | |
state.status.pending = false; | |
state.status.success = success; | |
state.status.fail = !success; | |
return state; | |
} | |
/** | |
* @param {Object} state - the status to be put in pending state | |
* @param {Boolean} clear - clear state data? | |
* @returns {Object} updated state | |
*/ | |
static _mutationPending(state, clear) { | |
if (clear) state.data = null; | |
state.status.pending = true; | |
state.status.success = false; | |
state.status.fail = false; | |
state.status.error = null; | |
return state; | |
} | |
/** | |
* @param {Object} state - the status to be put in success state | |
* @param {Object,array} - data to update with | |
* @returns {Object} updated state | |
*/ | |
static _mutationSuccess(state, data) { | |
state.data = data instanceof Array ? data : [data]; | |
state.status.pending = false; | |
state.status.success = true; | |
state.status.fail = false; | |
state.status.error = null; | |
return state; | |
} | |
/** | |
* @param {Object} state - the status to be put in success state | |
* @param {Object,array} - data to update with | |
* @returns {object} updated state | |
*/ | |
static _mutationFail(state, data) { | |
state.data = null; | |
state.status.pending = false; | |
state.status.success = false; | |
state.status.fail = true; | |
state.status.error = data; | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment