Skip to content

Commit

Permalink
Merge branch 'master' into add-error-margin-in-admin-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
JedWatson authored Sep 9, 2021
2 parents e282e23 + 6a589bc commit d4b5d08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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

0 comments on commit d4b5d08

Please sign in to comment.