|
@@ -0,0 +1,55 @@
|
|
|
|
+import { createStore, combineReducers, applyMiddleware } from "redux";
|
|
|
|
+import thunk from 'redux-thunk';
|
|
|
|
+
|
|
|
|
+const authReducer = (state, action) => {
|
|
|
|
+ // if(action.type===)
|
|
|
|
+}
|
|
|
|
+const promiseReducer = (state, action) => {
|
|
|
|
+ if (action.type === 'LOGIN' || action.type === 'LOGOUT') {
|
|
|
|
+ return {}
|
|
|
|
+ }
|
|
|
|
+ if (action.type === 'PROMISE') {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ return state
|
|
|
|
+}
|
|
|
|
+const actionPromise = (name, promise) => {
|
|
|
|
+ const actionPending = () => ({
|
|
|
|
+ type: 'PROMISE',
|
|
|
|
+ name,
|
|
|
|
+ status: 'PENDING',
|
|
|
|
+ payload: null,
|
|
|
|
+ error: null
|
|
|
|
+ })
|
|
|
|
+ const actionResolved = (payload) => ({
|
|
|
|
+ type: 'PROMISE',
|
|
|
|
+ name,
|
|
|
|
+ status: 'RESOLVED',
|
|
|
|
+ payload,
|
|
|
|
+ error: null
|
|
|
|
+ })
|
|
|
|
+ const actionRejected = (error) => ({
|
|
|
|
+ type: 'PROMISE',
|
|
|
|
+ name,
|
|
|
|
+ status: 'REJECTED',
|
|
|
|
+ payload: null,
|
|
|
|
+ error
|
|
|
|
+ })
|
|
|
|
+ return async (dispatch) => {
|
|
|
|
+ dispatch(actionPending())
|
|
|
|
+ let payload;
|
|
|
|
+ try {
|
|
|
|
+ payload = await promise
|
|
|
|
+ dispatch(actionResolved(payload))
|
|
|
|
+ }
|
|
|
|
+ catch (e) {
|
|
|
|
+ dispatch(actionRejected(e))
|
|
|
|
+ }
|
|
|
|
+ return payload
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export const store2 = createStore(combineReducers({
|
|
|
|
+ auth: authReducer,
|
|
|
|
+ promise: promiseReducer
|
|
|
|
+}), applyMiddleware(thunk));
|