fix: fields with relationTo[] correctly load returned data from form submission (#3317)
This commit is contained in:
@@ -90,7 +90,7 @@ const Content: React.FC<DocumentDrawerProps> = ({
|
||||
|
||||
const isEditing = Boolean(id);
|
||||
const apiURL = id ? `${serverURL}${api}/${collectionSlug}/${id}?locale=${locale}` : null;
|
||||
const action = `${serverURL}${api}/${collectionSlug}${id ? `/${id}` : ''}?locale=${locale}&depth=0&fallback-locale=null`;
|
||||
const action = `${serverURL}${api}/${collectionSlug}${id ? `/${id}` : ''}?locale=${locale}&fallback-locale=null`;
|
||||
const hasSavePermission = (isEditing && docPermissions?.update?.permission) || (!isEditing && (docPermissions as CollectionPermission)?.create?.permission);
|
||||
const isLoading = !internalState || !docPermissions || isLoadingDocument;
|
||||
|
||||
|
||||
@@ -66,11 +66,11 @@ const Status: React.FC = () => {
|
||||
}
|
||||
|
||||
if (collection) {
|
||||
url = `${serverURL}${api}/${collection.slug}/${id}?depth=0&locale=${locale}&fallback-locale=null`;
|
||||
url = `${serverURL}${api}/${collection.slug}/${id}?locale=${locale}&fallback-locale=null`;
|
||||
method = 'patch';
|
||||
}
|
||||
if (global) {
|
||||
url = `${serverURL}${api}/globals/${global.slug}?depth=0&locale=${locale}&fallback-locale=null`;
|
||||
url = `${serverURL}${api}/globals/${global.slug}?locale=${locale}&fallback-locale=null`;
|
||||
method = 'post';
|
||||
}
|
||||
|
||||
|
||||
@@ -248,6 +248,12 @@ export const addFieldStatePromise = async ({
|
||||
case 'relationship': {
|
||||
if (field.hasMany) {
|
||||
const relationshipValue = Array.isArray(valueWithDefault) ? valueWithDefault.map((relationship) => {
|
||||
if (Array.isArray(field.relationTo)) {
|
||||
return {
|
||||
relationTo: relationship.relationTo,
|
||||
value: typeof relationship.value === 'string' ? relationship.value : relationship.value?.id,
|
||||
};
|
||||
}
|
||||
if (typeof relationship === 'object' && relationship !== null) {
|
||||
return relationship.id;
|
||||
}
|
||||
@@ -256,16 +262,24 @@ export const addFieldStatePromise = async ({
|
||||
|
||||
fieldState.value = relationshipValue;
|
||||
fieldState.initialValue = relationshipValue;
|
||||
|
||||
state[`${path}${field.name}`] = fieldState;
|
||||
} else if (Array.isArray(field.relationTo)) {
|
||||
if (valueWithDefault && typeof valueWithDefault === 'object' && 'relationTo' in valueWithDefault && 'value' in valueWithDefault) {
|
||||
const value = typeof valueWithDefault?.value === 'object' && 'id' in valueWithDefault.value ? valueWithDefault.value.id : valueWithDefault.value;
|
||||
const relationshipValue = {
|
||||
relationTo: valueWithDefault?.relationTo,
|
||||
value,
|
||||
};
|
||||
fieldState.value = relationshipValue;
|
||||
fieldState.initialValue = relationshipValue;
|
||||
}
|
||||
} else {
|
||||
const relationshipValue = valueWithDefault && typeof valueWithDefault === 'object' && 'id' in valueWithDefault ? valueWithDefault.id : valueWithDefault;
|
||||
fieldState.value = relationshipValue;
|
||||
fieldState.initialValue = relationshipValue;
|
||||
|
||||
state[`${path}${field.name}`] = fieldState;
|
||||
}
|
||||
|
||||
state[`${path}${field.name}`] = fieldState;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ const AccountView: React.FC = () => {
|
||||
const dataToRender = locationState?.data || data;
|
||||
const apiURL = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}`;
|
||||
|
||||
const action = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}&depth=0`;
|
||||
const action = `${serverURL}${api}/${slug}/${data?.id}?locale=${locale}`;
|
||||
|
||||
const onSave = React.useCallback(async (json: any) => {
|
||||
getDocPermissions();
|
||||
|
||||
Reference in New Issue
Block a user