From a0272f5baa5ca48f5a9d6cc93b8fcb97046b987b Mon Sep 17 00:00:00 2001
From: muerwre <gotham48@gmail.com>
Date: Sun, 25 Aug 2019 17:04:13 +0700
Subject: [PATCH] loading node

---
 src/components/node/NodeImageBlock/index.tsx |  8 +++++---
 src/constants/api.ts                         |  1 +
 src/redux/node/api.ts                        | 10 ++++++++++
 src/redux/node/sagas.ts                      | 14 ++++++++++++--
 4 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/components/node/NodeImageBlock/index.tsx b/src/components/node/NodeImageBlock/index.tsx
index 72a364d8..1e675602 100644
--- a/src/components/node/NodeImageBlock/index.tsx
+++ b/src/components/node/NodeImageBlock/index.tsx
@@ -12,9 +12,11 @@ interface IProps {
 }
 
 const NodeImageBlock: FC<IProps> = ({ node, is_loading }) => {
-  const images = useMemo(() => node.files.filter(({ type }) => type === UPLOAD_TYPES.IMAGE), [
-    node,
-  ]);
+  const images = useMemo(
+    () =>
+      (node && node.files && node.files.filter(({ type }) => type === UPLOAD_TYPES.IMAGE)) || [],
+    [node]
+  );
 
   return (
     <div className={classNames(styles.wrap, { is_loading })}>
diff --git a/src/constants/api.ts b/src/constants/api.ts
index 04e3fb17..8ddb366f 100644
--- a/src/constants/api.ts
+++ b/src/constants/api.ts
@@ -8,5 +8,6 @@ export const API = {
   NODE: {
     SAVE: '/node/',
     GET: '/node/',
+    GET_NODE: (id: number | string) => `/node/${id}`,
   },
 };
diff --git a/src/redux/node/api.ts b/src/redux/node/api.ts
index bc977744..cd798da6 100644
--- a/src/redux/node/api.ts
+++ b/src/redux/node/api.ts
@@ -23,3 +23,13 @@ export const getNodes = ({
     .get(API.NODE.GET, { params: { skip } })
     .then(resultMiddleware)
     .catch(errorMiddleware);
+
+export const getNode = ({
+  id,
+}: {
+  id: string | number;
+}): Promise<IResultWithStatus<{ nodes: INode[] }>> =>
+  api
+    .get(API.NODE.GET_NODE(id))
+    .then(resultMiddleware)
+    .catch(errorMiddleware);
diff --git a/src/redux/node/sagas.ts b/src/redux/node/sagas.ts
index 2bc3e1aa..ad0c84a3 100644
--- a/src/redux/node/sagas.ts
+++ b/src/redux/node/sagas.ts
@@ -9,7 +9,7 @@ import {
   nodeSetLoading,
   nodeSetCurrent,
 } from './actions';
-import { postNode } from './api';
+import { postNode, getNode } from './api';
 import { reqWrapper } from '../auth/sagas';
 import { flowSetNodes } from '../flow/actions';
 import { ERRORS } from '~/constants/errors';
@@ -45,10 +45,20 @@ function* onNodeLoad({ id, node_type }: ReturnType<typeof nodeLoadNode>) {
 
   yield put(push(URLS.NODE_URL(id)));
 
-  yield delay(1000);
+  const {
+    data: { node, error },
+  } = yield call(getNode, { id });
 
   yield put(nodeSetLoading(false));
+
+  if (error) {
+    return yield put(nodeSetSaveErrors({ error }));
+  }
+
   yield put(nodeSetSaveErrors({}));
+  yield put(nodeSetCurrent(node));
+
+  return;
 }
 
 export default function* nodeSaga() {