From c88cd574215265154396121a304adf88a7ffddf1 Mon Sep 17 00:00:00 2001 From: Sarah Scott Date: Tue, 27 Jul 2021 16:57:19 -0700 Subject: [PATCH] feedback in the editor --- api.planx.uk/publish.js | 71 +++++++++++-------- .../FlowEditor/components/PreviewBrowser.tsx | 7 +- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/api.planx.uk/publish.js b/api.planx.uk/publish.js index 404f757ca2..99f40f3266 100644 --- a/api.planx.uk/publish.js +++ b/api.planx.uk/publish.js @@ -72,44 +72,57 @@ const publishFlow = async (req, res) => { if (!req.user?.sub) return res.status(401).json({ error: "User ID missing from JWT" }); - const flattenedFlow = await dataMerged(req.params.flowId); - - const pub = await getMostRecentPublishedFlow(req.params.flowId); + try { + const flattenedFlow = await dataMerged(req.params.flowId); + const mostRecent = await getMostRecentPublishedFlow(req.params.flowId); - const delta = jsondiffpatch.diff(pub, flattenedFlow); + const delta = jsondiffpatch.diff(mostRecent, flattenedFlow); - // return published flow record if changes were made - const response = delta - ? await client.request( + if (delta) { + const response = await client.request( ` - mutation PublishFlow( - $data: jsonb = {}, - $flow_id: uuid, - $publisher_id: Int, - ) { - - insert_published_flows_one(object: { - data: $data, - flow_id: $flow_id, - publisher_id: $publisher_id, - }) { - id - flow_id - publisher_id - created_at - data - } - }`, + mutation PublishFlow( + $data: jsonb = {}, + $flow_id: uuid, + $publisher_id: Int, + ) { + insert_published_flows_one(object: { + data: $data, + flow_id: $flow_id, + publisher_id: $publisher_id, + }) { + id + flow_id + publisher_id + created_at + data + } + } + `, { data: flattenedFlow, flow_id: req.params.flowId, publisher_id: parseInt(req.user.sub, 10), } - ) - : "No new changes"; + ); + const publishedFlow = + response.insert_published_flows_one && + response.insert_published_flows_one.data; + const alteredNodes = Object.keys(delta).map((key) => ({ + id: key, + ...publishedFlow[key], + })); - try { - res.json(response); + res.json({ + alteredNodes, + publishedFlow, + }); + } else { + res.json({ + alteredNodes: null, + message: "No new changes", + }); + } } catch (error) { console.error(error); res.status(500).json({ error }); diff --git a/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx b/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx index b058b3c5db..a838a44541 100644 --- a/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx +++ b/editor.planx.uk/src/pages/FlowEditor/components/PreviewBrowser.tsx @@ -144,8 +144,13 @@ const PreviewBrowser: React.FC<{ url: string }> = React.memo((props) => { variant="contained" color="primary" onClick={async () => { + setLastPublishedTitle("Sending changes..."); const publishedFlow = await publishFlow(flowId); - setLastPublishedTitle("Successfully published"); + setLastPublishedTitle( + publishedFlow?.data.alteredNodes + ? `Successfully published changes to ${publishedFlow.data.alteredNodes.length} node(s)` + : "No new changes to publish!" + ); }} > PUBLISH