From 4c49ed000a135050bd6deef60279a27075a3d97b Mon Sep 17 00:00:00 2001 From: muerwre Date: Sun, 25 Aug 2019 11:04:28 +0700 Subject: [PATCH] setting nodes on flow\ --- src/redux/flow/reducer.ts | 8 +++++--- src/redux/flow/sagas.ts | 12 +++++++----- src/redux/types.ts | 3 ++- src/utils/fn.ts | 2 ++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/redux/flow/reducer.ts b/src/redux/flow/reducer.ts index e6ec1e1e..f828c6c1 100644 --- a/src/redux/flow/reducer.ts +++ b/src/redux/flow/reducer.ts @@ -1,15 +1,17 @@ import { createReducer } from '~/utils/reducer'; -import { INode, UUID } from '../types'; +import { INode, IError } from '../types'; import { FLOW_HANDLERS } from './handlers'; export type IFlowState = Readonly<{ is_loading: boolean; - nodes: Record; + nodes: INode[]; + error: IError; }>; const INITIAL_STATE: IFlowState = { - nodes: {}, + nodes: [], is_loading: false, + error: null, }; export default createReducer(INITIAL_STATE, FLOW_HANDLERS); diff --git a/src/redux/flow/sagas.ts b/src/redux/flow/sagas.ts index 89b2d6fa..cb621c23 100644 --- a/src/redux/flow/sagas.ts +++ b/src/redux/flow/sagas.ts @@ -1,23 +1,25 @@ -import { takeLatest, call } from 'redux-saga/effects'; +import { takeLatest, call, put } from 'redux-saga/effects'; import { REHYDRATE } from 'redux-persist'; import { FLOW_ACTIONS } from './constants'; import { getNodes } from '../node/api'; import { flowSetNodes } from './actions'; +import { objFromArray } from '~/utils/fn'; +import { IResultWithStatus, INode } from '../types'; function* onGetFlow() { const { data: { nodes = null }, error, - } = yield call(getNodes, {}); + }: IResultWithStatus<{ nodes: INode[] }> = yield call(getNodes, {}); - if (!nodes) { + if (!nodes || !nodes.length) { // todo: set error empty response } // todo: write nodes - // yield put(flowSetNodes()); + yield put(flowSetNodes(nodes)); - console.log('flow', { nodes, error }); + // console.log('flow', { nodes, error }); } export default function* nodeSaga() { diff --git a/src/redux/types.ts b/src/redux/types.ts index 0d39d4b2..f9a5aa0a 100644 --- a/src/redux/types.ts +++ b/src/redux/types.ts @@ -114,4 +114,5 @@ export interface INode { } export type IUploadProgressHandler = (progress: ProgressEvent) => void; -export type IValidationErrors = Record>; +export type IError = ValueOf; +export type IValidationErrors = Record; diff --git a/src/utils/fn.ts b/src/utils/fn.ts index e043404f..dd239fae 100644 --- a/src/utils/fn.ts +++ b/src/utils/fn.ts @@ -4,3 +4,5 @@ import nth from 'ramda/es/nth'; import remove from 'ramda/es/remove'; export const moveArrItem = curry((at, to, list) => insert(to, nth(at, list), remove(at, 1, list))); +export const objFromArray = (array: any[], key: string) => + array.reduce((obj, el) => (key && el[key] ? { ...obj, [el[key]]: el } : obj), {});