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

Fix item form submitting an invalid request after saving returns errors #6509

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-chicken-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Fixed item form submitting an invalid request after saving returns errors
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ function ItemForm({

const [state, setValue] = useState(() => {
const value = deserializeValue(list.fields, itemGetter);
return { value, item: itemGetter.data };
return { value, item: itemGetter };
});
if (
!loading &&
state.item !== itemGetter.data &&
state.item.data !== itemGetter.data &&
(itemGetter.errors || []).every(x => x.path?.length !== 1)
) {
const value = deserializeValue(list.fields, itemGetter);
setValue({ value, item: itemGetter.data });
setValue({ value, item: itemGetter });
}

const { changedFields, dataForUpdate } = useChangedFieldsAndDataForUpdate(
list.fields,
itemGetter,
state.item,
state.value
);

Expand All @@ -118,7 +118,7 @@ function ItemForm({
setForceValidation(newForceValidation);
if (newForceValidation) return;

update({ variables: { data: dataForUpdate, id: itemGetter.get('id').data } })
update({ variables: { data: dataForUpdate, id: state.item.get('id').data } })
// TODO -- Experimenting with less detail in the toasts, so the data lines are commented
// out below. If we're happy with this, clean up the unused lines.
.then(({ /* data, */ errors }) => {
Expand All @@ -145,8 +145,8 @@ function ItemForm({
toasts.addToast({ title: 'Failed to update item', tone: 'negative', message: err.message });
});
});
const labelFieldValue = itemGetter.data?.[list.labelField];
const itemId = itemGetter.data?.id!;
const labelFieldValue = state.item.data?.[list.labelField];
const itemId = state.item.data?.id!;
return (
<Box marginTop="xlarge">
<GraphQLErrorNotice
Expand All @@ -172,7 +172,10 @@ function ItemForm({
onSave={onSave}
hasChangedFields={!!changedFields.size}
onReset={useEventCallback(() => {
setValue({ item: itemGetter.data, value: deserializeValue(list.fields, itemGetter) });
setValue(state => ({
item: state.item,
value: deserializeValue(list.fields, state.item),
}));
})}
loading={loading}
deleteButton={useMemo(
Expand Down