mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
flow redux
This commit is contained in:
parent
9691504af6
commit
95443635b4
11 changed files with 118 additions and 10 deletions
|
@ -7,5 +7,6 @@ export const API = {
|
||||||
},
|
},
|
||||||
NODE: {
|
NODE: {
|
||||||
SAVE: '/node/',
|
SAVE: '/node/',
|
||||||
|
GET: '/node/',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
7
src/redux/flow/actions.ts
Normal file
7
src/redux/flow/actions.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { INode } from '../types';
|
||||||
|
import { FLOW_ACTIONS } from './constants';
|
||||||
|
|
||||||
|
export const flowSetNodes = (nodes: INode[]) => ({
|
||||||
|
nodes,
|
||||||
|
type: FLOW_ACTIONS.SET_NODES,
|
||||||
|
});
|
25
src/redux/flow/api.ts
Normal file
25
src/redux/flow/api.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { api, configWithToken, resultMiddleware, errorMiddleware } from '~/utils/api';
|
||||||
|
import { INode, IResultWithStatus } from '../types';
|
||||||
|
import { API } from '~/constants/api';
|
||||||
|
|
||||||
|
export const postNode = ({
|
||||||
|
access,
|
||||||
|
node,
|
||||||
|
}: {
|
||||||
|
access: string;
|
||||||
|
node: INode;
|
||||||
|
}): Promise<IResultWithStatus<INode>> =>
|
||||||
|
api
|
||||||
|
.post(API.NODE.SAVE, { node }, configWithToken(access))
|
||||||
|
.then(resultMiddleware)
|
||||||
|
.catch(errorMiddleware);
|
||||||
|
|
||||||
|
export const getNodes = ({
|
||||||
|
skip = 0,
|
||||||
|
}: {
|
||||||
|
skip: number;
|
||||||
|
}): Promise<IResultWithStatus<{ nodes: INode[] }>> =>
|
||||||
|
api
|
||||||
|
.get(API.NODE.GET, { params: { skip } })
|
||||||
|
.then(resultMiddleware)
|
||||||
|
.catch(errorMiddleware);
|
6
src/redux/flow/constants.ts
Normal file
6
src/redux/flow/constants.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
const prefix = 'FLOW.';
|
||||||
|
|
||||||
|
export const FLOW_ACTIONS = {
|
||||||
|
GET_FLOW: `${prefix}.GET_FLOW`,
|
||||||
|
SET_NODES: `${prefix}.SET_NODES`,
|
||||||
|
};
|
11
src/redux/flow/handlers.ts
Normal file
11
src/redux/flow/handlers.ts
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import assocPath from 'ramda/es/assocPath';
|
||||||
|
import { FLOW_ACTIONS } from './constants';
|
||||||
|
import { flowSetNodes } from './actions';
|
||||||
|
import { IFlowState } from './reducer';
|
||||||
|
|
||||||
|
const setNodes = (state: IFlowState, { nodes }: ReturnType<typeof flowSetNodes>) =>
|
||||||
|
assocPath(['nodes'], nodes, state);
|
||||||
|
|
||||||
|
export const FLOW_HANDLERS = {
|
||||||
|
[FLOW_ACTIONS.SET_NODES]: setNodes,
|
||||||
|
};
|
15
src/redux/flow/reducer.ts
Normal file
15
src/redux/flow/reducer.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { createReducer } from '~/utils/reducer';
|
||||||
|
import { INode, UUID } from '../types';
|
||||||
|
import { FLOW_HANDLERS } from './handlers';
|
||||||
|
|
||||||
|
export type IFlowState = Readonly<{
|
||||||
|
is_loading: boolean;
|
||||||
|
nodes: Record<UUID, INode>;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const INITIAL_STATE: IFlowState = {
|
||||||
|
nodes: {},
|
||||||
|
is_loading: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createReducer(INITIAL_STATE, FLOW_HANDLERS);
|
14
src/redux/flow/sagas.ts
Normal file
14
src/redux/flow/sagas.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import { takeLatest, call } from 'redux-saga/effects';
|
||||||
|
import { REHYDRATE } from 'redux-persist';
|
||||||
|
import { FLOW_ACTIONS } from './constants';
|
||||||
|
import { getNodes } from '../node/api';
|
||||||
|
|
||||||
|
function* onGetFlow() {
|
||||||
|
const { data, error } = yield call(getNodes, {});
|
||||||
|
|
||||||
|
console.log('flow', { data, error });
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function* nodeSaga() {
|
||||||
|
yield takeLatest([FLOW_ACTIONS.GET_FLOW, REHYDRATE], onGetFlow);
|
||||||
|
}
|
4
src/redux/flow/selectors.ts
Normal file
4
src/redux/flow/selectors.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { IState } from '../store';
|
||||||
|
import { INodeState } from './reducer';
|
||||||
|
|
||||||
|
export const selectNode = (state: IState): INodeState => state.node;
|
|
@ -6,10 +6,20 @@ export const postNode = ({
|
||||||
access,
|
access,
|
||||||
node,
|
node,
|
||||||
}: {
|
}: {
|
||||||
access: string,
|
access: string;
|
||||||
node: INode,
|
node: INode;
|
||||||
}): Promise<IResultWithStatus<INode>> => (
|
}): Promise<IResultWithStatus<INode>> =>
|
||||||
api.post(API.NODE.SAVE, { node }, configWithToken(access))
|
api
|
||||||
|
.post(API.NODE.SAVE, { node }, configWithToken(access))
|
||||||
.then(resultMiddleware)
|
.then(resultMiddleware)
|
||||||
.catch(errorMiddleware)
|
.catch(errorMiddleware);
|
||||||
);
|
|
||||||
|
export const getNodes = ({
|
||||||
|
skip = 0,
|
||||||
|
}: {
|
||||||
|
skip?: number;
|
||||||
|
}): Promise<IResultWithStatus<{ nodes: INode[] }>> =>
|
||||||
|
api
|
||||||
|
.get(API.NODE.GET, { params: { skip } })
|
||||||
|
.then(resultMiddleware)
|
||||||
|
.catch(errorMiddleware);
|
||||||
|
|
|
@ -1,11 +1,20 @@
|
||||||
import { takeLatest, call } from 'redux-saga/effects';
|
import { takeLatest, call, put } from 'redux-saga/effects';
|
||||||
import { NODE_ACTIONS } from './constants';
|
import { NODE_ACTIONS } from './constants';
|
||||||
import { nodeSave } from './actions';
|
import { nodeSave, nodeSetSaveErrors } from './actions';
|
||||||
import { postNode } from './api';
|
import { postNode } from './api';
|
||||||
import { reqWrapper } from '../auth/sagas';
|
import { reqWrapper } from '../auth/sagas';
|
||||||
|
|
||||||
function* onNodeSave({ node }: ReturnType<typeof nodeSave>) {
|
function* onNodeSave({ node }: ReturnType<typeof nodeSave>) {
|
||||||
const { data, errors } = yield call(reqWrapper, postNode, { node });
|
yield put(nodeSetSaveErrors({}));
|
||||||
|
|
||||||
|
const {
|
||||||
|
data,
|
||||||
|
data: { errors },
|
||||||
|
} = yield call(reqWrapper, postNode, { node });
|
||||||
|
|
||||||
|
if (errors && Object.values(errors).length > 0) {
|
||||||
|
return yield put(nodeSetSaveErrors(errors));
|
||||||
|
}
|
||||||
|
|
||||||
console.log({ data, errors });
|
console.log({ data, errors });
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,9 @@ import authSaga from '~/redux/auth/sagas';
|
||||||
import nodeReducer, { INodeState } from '~/redux/node/reducer';
|
import nodeReducer, { INodeState } from '~/redux/node/reducer';
|
||||||
import nodeSaga from '~/redux/node/sagas';
|
import nodeSaga from '~/redux/node/sagas';
|
||||||
|
|
||||||
|
import flowReducer, { IFlowState } from '~/redux/flow/reducer';
|
||||||
|
import flowSaga from '~/redux/flow/sagas';
|
||||||
|
|
||||||
import uploadReducer, { IUploadState } from '~/redux/uploads/reducer';
|
import uploadReducer, { IUploadState } from '~/redux/uploads/reducer';
|
||||||
import uploadSaga from '~/redux/uploads/sagas';
|
import uploadSaga from '~/redux/uploads/sagas';
|
||||||
|
|
||||||
|
@ -32,6 +35,7 @@ export interface IState {
|
||||||
router: RouterState;
|
router: RouterState;
|
||||||
node: INodeState;
|
node: INodeState;
|
||||||
uploads: IUploadState;
|
uploads: IUploadState;
|
||||||
|
flow: IFlowState;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sagaMiddleware = createSagaMiddleware();
|
export const sagaMiddleware = createSagaMiddleware();
|
||||||
|
@ -49,6 +53,7 @@ export const store = createStore(
|
||||||
router: connectRouter(history),
|
router: connectRouter(history),
|
||||||
node: nodeReducer,
|
node: nodeReducer,
|
||||||
uploads: uploadReducer,
|
uploads: uploadReducer,
|
||||||
|
flow: flowReducer,
|
||||||
}),
|
}),
|
||||||
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
|
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
|
||||||
);
|
);
|
||||||
|
@ -57,6 +62,7 @@ export function configureStore(): { store: Store<IState>; persistor: Persistor }
|
||||||
sagaMiddleware.run(authSaga);
|
sagaMiddleware.run(authSaga);
|
||||||
sagaMiddleware.run(nodeSaga);
|
sagaMiddleware.run(nodeSaga);
|
||||||
sagaMiddleware.run(uploadSaga);
|
sagaMiddleware.run(uploadSaga);
|
||||||
|
sagaMiddleware.run(flowSaga);
|
||||||
|
|
||||||
const persistor = persistStore(store);
|
const persistor = persistStore(store);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue