|
@@ -1,4 +1,44 @@
|
|
|
-export function promiseReducer(state = {}, action) { // диспетчер обработки
|
|
|
+import { createSlice } from "@reduxjs/toolkit"
|
|
|
+
|
|
|
+const promiseReducerSlice = createSlice({ //promiseReducer
|
|
|
+ name: 'promise', //префикс типа наподобие 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 actionPromise = (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 }))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+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 };
|
|
|
+
|
|
|
+/*export function promiseReducer(state = {}, action) { // диспетчер обработки
|
|
|
if (action) {
|
|
|
if (action.type === 'PROMISE') {
|
|
|
let newState = { ...state };
|
|
@@ -26,4 +66,4 @@ export const actionPromise = (name, promise) => {
|
|
|
dispatch(actionRejected(name, error)) //в случае ошибки - сигнализируем redux, что промис несложился
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}*/
|