-
Notifications
You must be signed in to change notification settings - Fork 115
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
NEW Add save action to element inline edit forms #392
Conversation
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) |
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. |
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,
+ });
});
}; It also doesn't automatically add a new item to the UI. We'd also need to pull the title out of
|
96edaeb
to
d45afe8
Compare
There was a problem hiding this 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! |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
That branch name 👍 |
There was a problem hiding this 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 💤 😃
d45afe8
to
3746d25
Compare
… bubbling logic to Element
3746d25
to
5f1d92d
Compare
This PR:
<button>
for<DropdownItem>
in the more actions menu so that clicking on the items will correctly close the menu via toggle stateIssue: #296 (doesn't close it yet)