1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

flow redux

This commit is contained in:
muerwre 2019-08-24 17:58:39 +07:00
parent 9691504af6
commit 95443635b4
11 changed files with 118 additions and 10 deletions

View file

@ -7,5 +7,6 @@ export const API = {
},
NODE: {
SAVE: '/node/',
GET: '/node/',
},
};

View 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
View 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);

View file

@ -0,0 +1,6 @@
const prefix = 'FLOW.';
export const FLOW_ACTIONS = {
GET_FLOW: `${prefix}.GET_FLOW`,
SET_NODES: `${prefix}.SET_NODES`,
};

View 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
View 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
View 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);
}

View file

@ -0,0 +1,4 @@
import { IState } from '../store';
import { INodeState } from './reducer';
export const selectNode = (state: IState): INodeState => state.node;

View file

@ -6,10 +6,20 @@ export const postNode = ({
access,
node,
}: {
access: string,
node: INode,
}): Promise<IResultWithStatus<INode>> => (
api.post(API.NODE.SAVE, { node }, configWithToken(access))
access: string;
node: INode;
}): Promise<IResultWithStatus<INode>> =>
api
.post(API.NODE.SAVE, { node }, configWithToken(access))
.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);

View file

@ -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 { nodeSave } from './actions';
import { nodeSave, nodeSetSaveErrors } from './actions';
import { postNode } from './api';
import { reqWrapper } from '../auth/sagas';
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 });
}

View file

@ -13,6 +13,9 @@ import authSaga from '~/redux/auth/sagas';
import nodeReducer, { INodeState } from '~/redux/node/reducer';
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 uploadSaga from '~/redux/uploads/sagas';
@ -32,6 +35,7 @@ export interface IState {
router: RouterState;
node: INodeState;
uploads: IUploadState;
flow: IFlowState;
}
export const sagaMiddleware = createSagaMiddleware();
@ -49,6 +53,7 @@ export const store = createStore(
router: connectRouter(history),
node: nodeReducer,
uploads: uploadReducer,
flow: flowReducer,
}),
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
);
@ -57,6 +62,7 @@ export function configureStore(): { store: Store<IState>; persistor: Persistor }
sagaMiddleware.run(authSaga);
sagaMiddleware.run(nodeSaga);
sagaMiddleware.run(uploadSaga);
sagaMiddleware.run(flowSaga);
const persistor = persistStore(store);