Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Add save action to element inline edit forms #392

Merged

Conversation

robbieaverill
Copy link
Contributor

@robbieaverill robbieaverill commented Sep 17, 2018

This PR:

  • adds inline save action into the more actions menu
  • moves the click bubbling protection from the MoreActions wrapper into the Element
  • adds CSRF protection to the form submissions
  • adds some helpers to get the serialized content of the current form (because we aren't doing actual form submissions), and to get config from the Redux store
  • switches <button> for <DropdownItem> in the more actions menu so that clicking on the items will correctly close the menu via toggle state

Issue: #296 (doesn't close it yet)

@robbieaverill
Copy link
Contributor Author

robbieaverill commented Sep 19, 2018

Note that if #397 is merged then we will need to remove the "get serialised form data" helper and simply pull out the redux form state instead, because that change would mean that the DOM doesn't always contain the form fields, but the state will after first initial load. We could also conditionally render the save button if there is some data in the redux-form state for that element (would solve one of the outstanding tasks)

@robbieaverill
Copy link
Contributor Author

I've pushed an update to refetch apollo queries when saving an inline form. This is a stop-gap solution to get the Element data to update after editing the contents within it (e.g. the title and versioning state).

In a perfect world we would be doing this optimistically but for now telling Apollo to refetch is better than nothing.

@robbieaverill
Copy link
Contributor Author

robbieaverill commented Sep 19, 2018

I have this nasty piece of code which is optimistically adding to the query cache, but it needs to replace the node rather than append the new one:

diff --git a/client/src/components/ElementActions/SaveAction.js b/client/src/components/ElementActions/SaveAction.js
index d62c35d..b22cf0d 100644
--- a/client/src/components/ElementActions/SaveAction.js
+++ b/client/src/components/ElementActions/SaveAction.js
@@ -7,6 +7,7 @@ import backend from 'lib/Backend';
 import i18n from 'i18n';
 import { loadElementSchemaValue } from 'state/editor/loadElementSchemaValue';
 import { getSerializedFormData } from 'state/editor/getSerializedFormData';
+import { query as readBlocksQuery, config as readBlocksConfig } from 'state/editor/readBlocksForPageQuery';
 
 /**
  * Using a REST backend, serialize the current form data and post it to the backend endpoint to save
@@ -16,7 +17,7 @@ const SaveAction = (MenuComponent) => (props) => {
   const handleClick = (event) => {
     event.stopPropagation();
 
-    const { id, securityId } = props;
+    const { id, pageId, securityId } = props;
 
     const formData = getSerializedFormData(`Form_ElementForm_${id}`);
 
@@ -34,9 +35,38 @@ const SaveAction = (MenuComponent) => (props) => {
       // Update the Apollo query cache with the new form data
       const { apolloClient } = window.ss;
 
-      // @todo optimistically update the data for the current element instead of
-      // rerunning the whole query
-      apolloClient.queryManager.reFetchObservableQueries();
+      const variables = readBlocksConfig.options({ pageId }).variables;
+
+      const query = apolloClient.readQuery({ query: readBlocksQuery, variables });
+
+      const oldData = query.readOnePage.ElementalAreaIfExists.Elements.edges.find(element => element.node.ID === id);
+      const newData = {
+        node: {
+          ...oldData.node,
+          Title: 'Testing',
+        },
+      };
+
+      console.log(newData);
+      const newQuery = {
+        ...query.readOnePage,
+        ElementalAreaIfExists: {
+          ...query.readOnePage.ElementalAreaIfExists,
+          Elements: {
+            ...query.readOnePage.ElementalAreaIfExists.Elements,
+            edges: [
+              ...query.readOnePage.ElementalAreaIfExists.Elements.edges,
+              newData,
+            ],
+          },
+        },
+      };
+      console.log(newQuery);
+
+      apolloClient.writeQuery({
+        query: readBlocksQuery,
+        data: newQuery,
+      });
     });
   };

image

It also doesn't automatically add a new item to the UI. We'd also need to pull the title out of formData, e.g.

  Title: formData[`PageElements_${id}_Title`],

@robbieaverill robbieaverill changed the title WIP NEW Add save action to element inline edit forms NEW Add save action to element inline edit forms Sep 20, 2018
Copy link
Contributor

@raissanorth raissanorth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Seems to need a rebase, but happy to merge otherwise.

/**
* Save an inline edit form for a block
*
* @todo CSRF protection!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not done on line 121?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it is done now, sorry

@raissanorth
Copy link
Contributor

That branch name 👍

Copy link
Contributor

@NightJar NightJar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pair-peer-reviewed with @raissanorth, so her comments stand for us both.
Looks good to me :) I like the use of DropdownItem instead of button!

Raissa will rebase (and remove that @todo) and merge while you 💤 😃

@raissanorth raissanorth merged commit da0740c into silverstripe:master Sep 25, 2018
@raissanorth raissanorth deleted the pulls/4.0/li-save-ne branch September 25, 2018 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants