|
@@ -1,44 +0,0 @@
|
|
|
-import { createSlice } from "@reduxjs/toolkit"
|
|
|
-
|
|
|
-const createPromiseReducerSlice = name => createSlice({ //promiseReducer
|
|
|
- name: name, //префикс типа наподобие AUTH_
|
|
|
- initialState: {}, //state={} в параметрах
|
|
|
- reducers: {
|
|
|
- pending(state, { payload: { name } }) { //if (type === 'promise/pending')
|
|
|
- state[name] = { status: 'PENDING' }
|
|
|
- },
|
|
|
- fulfilled(state, { payload: { name, payload } }) { //if (type === 'promise/fulfilled')
|
|
|
- state[name] = { status: 'FULFILLED', payload }
|
|
|
- },
|
|
|
- rejected(state, { payload: { name, error } }) { //if (type === 'promise/rejected')
|
|
|
- state[name] = { status: 'REJECTED', error }
|
|
|
- },
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-const promiseReducerSlice = createPromiseReducerSlice('promise');
|
|
|
-
|
|
|
-const actionPromiseGeneric = (promiseReducerSlice, name, promise) =>
|
|
|
- async dispatch => {
|
|
|
- try {
|
|
|
- dispatch(promiseReducerSlice.actions.pending({ name }))
|
|
|
- let payload = await promise
|
|
|
- if (payload && payload.data)
|
|
|
- payload = Object.values(payload.data)[0];
|
|
|
- dispatch(promiseReducerSlice.actions.fulfilled({ name, payload }))
|
|
|
- return payload
|
|
|
- }
|
|
|
- catch (error) {
|
|
|
- dispatch(promiseReducerSlice.actions.rejected({ name, error }))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-const actionPromise = (name, promise) =>
|
|
|
- actionPromiseGeneric(promiseReducerSlice, name, promise);
|
|
|
-
|
|
|
-
|
|
|
-let promiseReducer = promiseReducerSlice.reducer;
|
|
|
-let actionPending = promiseReducerSlice.actions.pending;
|
|
|
-let actionFulfilled = promiseReducerSlice.actions.fulfilled;
|
|
|
-let actionRejected = promiseReducerSlice.actions.rejected;
|
|
|
-export { promiseReducer, actionPending, actionFulfilled, actionRejected, actionPromise, createPromiseReducerSlice, actionPromiseGeneric };
|