From 32cd3456808a96738a8bee278f11295417b47190 Mon Sep 17 00:00:00 2001 From: Logan Allen Date: Wed, 3 Mar 2021 12:54:03 -0600 Subject: [PATCH] interface: finalize reducers / api logic for pending --- pkg/interface/src/logic/api/graph.ts | 12 ++-- .../src/logic/reducers/graph-update.js | 64 ++++++++++++++----- .../apps/publish/components/new-post.tsx | 9 +-- 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/pkg/interface/src/logic/api/graph.ts b/pkg/interface/src/logic/api/graph.ts index 003e414e9b..8203f4ccb3 100644 --- a/pkg/interface/src/logic/api/graph.ts +++ b/pkg/interface/src/logic/api/graph.ts @@ -239,13 +239,11 @@ export default class GraphApi extends BaseApi { action['add-nodes'].resource.ship.slice(1); return pendingPromise.then((pendingHashes) => { - action['add-nodes'].nodes = - Object.keys(action['add-nodes'].nodes).map((ind) => { - action['add-nodes'].nodes[ind].post.hash = - pendingHashes['pending-indices'][ind] || null; - return action['add-nodes'].nodes[ind]; - }); - + for (let index in action['add-nodes'].nodes) { + action['add-nodes'].nodes[index].post.hash = + pendingHashes['pending-indices'][index] || null; + } + this.store.handleEvent({ data: { 'graph-update': { 'pending-indices': pendingHashes['pending-indices'], diff --git a/pkg/interface/src/logic/reducers/graph-update.js b/pkg/interface/src/logic/reducers/graph-update.js index f0cb1b4eee..7c2f40b6e6 100644 --- a/pkg/interface/src/logic/reducers/graph-update.js +++ b/pkg/interface/src/logic/reducers/graph-update.js @@ -125,20 +125,34 @@ const addNodes = (json, state) => { return graph; }; - const _removePending = (resource, index, post) => { + const _remove = (graph, index) => { + if (index.length === 1) { + graph.delete(index[0]); + } else { + const child = graph.get(index[0]); + if (child) { + graph = _remove(child.children, index.slice(1)); + graph.set(index[0], child); + } + } + + return graph; + }; + + const _removePending = (graph, post) => { if (post.hash && state.pendingIndices[post.hash]) { - let pendingIndex = state.pendingIndices[post.hash]; - removeNodes({ - 'graph-update': { - 'remove-nodes': { - resource, - indices: [pendingIndex] - } - } + let index = state.pendingIndices[post.hash]; + + if (index.split('/').length === 0) { return; } + let indexArr = index.split('/').slice(1).map((ind) => { + return bigInt(ind); }); + graph = _remove(graph, indexArr); delete state.pendingIndices[post.hash]; } + + return graph; }; const data = _.get(json, 'add-nodes', false); @@ -150,10 +164,20 @@ const addNodes = (json, state) => { state.graphs[resource] = new BigIntOrderedMap(); } state.graphKeys.add(resource); + + let indices = Array.from(Object.keys(data.nodes)); - for (let index in data.nodes) { + indices.sort((a, b) => { + let aArr = a.split('/'); + let bArr = b.split('/'); + return bArr.length < aArr.length; + }); + + let graph = state.graphs[resource]; + + indices.forEach((index) => { let node = data.nodes[index]; - _removePending(data.resource, index, node.post); + graph = _removePending(graph, node.post); if (index.split('/').length === 0) { return; } index = index.split('/').slice(1).map((ind) => { @@ -163,26 +187,32 @@ const addNodes = (json, state) => { if (index.length === 0) { return; } node.children = mapifyChildren(node?.children || {}); - - state.graphs[resource] = _addNode( - state.graphs[resource], + + graph = _addNode( + graph, index, node ); - } + }); + + state.graphs[resource] = graph; } }; + const removeNodes = (json, state) => { const _remove = (graph, index) => { if (index.length === 1) { graph.delete(index[0]); } else { const child = graph.get(index[0]); - _remove(child.children, index.slice(1)); - graph.set(index[0], child); + if (child) { + _remove(child.children, index.slice(1)); + graph.set(index[0], child); + } } }; + const data = _.get(json, 'remove-nodes', false); if (data) { const { ship, name } = data.resource; diff --git a/pkg/interface/src/views/apps/publish/components/new-post.tsx b/pkg/interface/src/views/apps/publish/components/new-post.tsx index 2b0dd366a2..19f09cd741 100644 --- a/pkg/interface/src/views/apps/publish/components/new-post.tsx +++ b/pkg/interface/src/views/apps/publish/components/new-post.tsx @@ -30,12 +30,9 @@ export default function NewPost(props: NewPostProps & RouteComponentProps) { ) => { const { title, body } = values; try { - const [noteId, nodes] = newPost(title, body) - await api.graph.addNodes(ship, book, nodes) - await waiter(p => - p.graph.has(noteId) && !p.graph.get(noteId)?.post?.pending - ); - history.push(`${props.baseUrl}/note/${noteId}`); + const [noteId, nodes] = newPost(title, body); + await api.graph.addNodes(ship, book, nodes); + history.push(`${props.baseUrl}`); } catch (e) { console.error(e); actions.setStatus({ error: "Posting note failed" });