1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-26 05:16:41 +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

@ -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;