interface: finalize reducers / api logic for pending

This commit is contained in:
Logan Allen 2021-03-03 12:54:03 -06:00
parent 379e1840bc
commit 32cd345680
3 changed files with 55 additions and 30 deletions

View File

@ -239,13 +239,11 @@ export default class GraphApi extends BaseApi<StoreState> {
action['add-nodes'].resource.ship.slice(1); action['add-nodes'].resource.ship.slice(1);
return pendingPromise.then((pendingHashes) => { return pendingPromise.then((pendingHashes) => {
action['add-nodes'].nodes = for (let index in action['add-nodes'].nodes) {
Object.keys(action['add-nodes'].nodes).map((ind) => { action['add-nodes'].nodes[index].post.hash =
action['add-nodes'].nodes[ind].post.hash = pendingHashes['pending-indices'][index] || null;
pendingHashes['pending-indices'][ind] || null; }
return action['add-nodes'].nodes[ind];
});
this.store.handleEvent({ data: { this.store.handleEvent({ data: {
'graph-update': { 'graph-update': {
'pending-indices': pendingHashes['pending-indices'], 'pending-indices': pendingHashes['pending-indices'],

View File

@ -125,20 +125,34 @@ const addNodes = (json, state) => {
return graph; 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]) { if (post.hash && state.pendingIndices[post.hash]) {
let pendingIndex = state.pendingIndices[post.hash]; let index = state.pendingIndices[post.hash];
removeNodes({
'graph-update': { if (index.split('/').length === 0) { return; }
'remove-nodes': { let indexArr = index.split('/').slice(1).map((ind) => {
resource, return bigInt(ind);
indices: [pendingIndex]
}
}
}); });
graph = _remove(graph, indexArr);
delete state.pendingIndices[post.hash]; delete state.pendingIndices[post.hash];
} }
return graph;
}; };
const data = _.get(json, 'add-nodes', false); const data = _.get(json, 'add-nodes', false);
@ -150,10 +164,20 @@ const addNodes = (json, state) => {
state.graphs[resource] = new BigIntOrderedMap(); state.graphs[resource] = new BigIntOrderedMap();
} }
state.graphKeys.add(resource); 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]; let node = data.nodes[index];
_removePending(data.resource, index, node.post); graph = _removePending(graph, node.post);
if (index.split('/').length === 0) { return; } if (index.split('/').length === 0) { return; }
index = index.split('/').slice(1).map((ind) => { index = index.split('/').slice(1).map((ind) => {
@ -163,26 +187,32 @@ const addNodes = (json, state) => {
if (index.length === 0) { return; } if (index.length === 0) { return; }
node.children = mapifyChildren(node?.children || {}); node.children = mapifyChildren(node?.children || {});
state.graphs[resource] = _addNode( graph = _addNode(
state.graphs[resource], graph,
index, index,
node node
); );
} });
state.graphs[resource] = graph;
} }
}; };
const removeNodes = (json, state) => { const removeNodes = (json, state) => {
const _remove = (graph, index) => { const _remove = (graph, index) => {
if (index.length === 1) { if (index.length === 1) {
graph.delete(index[0]); graph.delete(index[0]);
} else { } else {
const child = graph.get(index[0]); const child = graph.get(index[0]);
_remove(child.children, index.slice(1)); if (child) {
graph.set(index[0], child); _remove(child.children, index.slice(1));
graph.set(index[0], child);
}
} }
}; };
const data = _.get(json, 'remove-nodes', false); const data = _.get(json, 'remove-nodes', false);
if (data) { if (data) {
const { ship, name } = data.resource; const { ship, name } = data.resource;

View File

@ -30,12 +30,9 @@ export default function NewPost(props: NewPostProps & RouteComponentProps) {
) => { ) => {
const { title, body } = values; const { title, body } = values;
try { try {
const [noteId, nodes] = newPost(title, body) const [noteId, nodes] = newPost(title, body);
await api.graph.addNodes(ship, book, nodes) await api.graph.addNodes(ship, book, nodes);
await waiter(p => history.push(`${props.baseUrl}`);
p.graph.has(noteId) && !p.graph.get(noteId)?.post?.pending
);
history.push(`${props.baseUrl}/note/${noteId}`);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
actions.setStatus({ error: "Posting note failed" }); actions.setStatus({ error: "Posting note failed" });