Fix multiple api calls

Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
soupette 2020-08-31 15:22:26 +02:00 committed by Pierre Noël
parent 31ad694a7e
commit 307bcf9a08
2 changed files with 16 additions and 24 deletions

View File

@ -32,8 +32,9 @@ const Header = () => {
onPublish, onPublish,
onUnpublish, onUnpublish,
} = useDataManager(); } = useDataManager();
const onPublishRef = useRef(onPublish);
const { const {
allowedActions: { canDelete, canUpdate, canCreate, canPublish }, allowedActions: { canUpdate, canCreate, canPublish },
} = useEditView(); } = useEditView();
const currentContentTypeMainField = useMemo(() => get(layout, ['settings', 'mainField'], 'id'), [ const currentContentTypeMainField = useMemo(() => get(layout, ['settings', 'mainField'], 'id'), [
@ -83,39 +84,30 @@ const Header = () => {
]; ];
} }
if (hasDraftAndPublish && canPublish && !initialData.published_at) { if (hasDraftAndPublish && canPublish) {
headerActions.unshift({ const isPublished = !isEmpty(initialData.published_at);
...primaryButtonObject, const isLoading = isPublished ? status === 'unpublish-pending' : status === 'publish-pending';
disabled: didChangeData, const labelID = isPublished ? 'app.utils.unpublish' : 'app.utils.publish';
label: formatMessage({ const onClick = isPublished ? () => setWarningUnpublish(true) : onPublishRef.current;
id: 'app.utils.publish',
}),
onClick: onPublish,
isLoading: status === 'publish-pending',
});
}
if (hasDraftAndPublish && canPublish && initialData.published_at) { const action = {
headerActions.unshift({
...primaryButtonObject, ...primaryButtonObject,
disabled: didChangeData, disabled: isCreatingEntry,
label: formatMessage({ isLoading,
id: 'app.utils.unpublish', label: formatMessage({ id: labelID }),
}), onClick,
onClick: () => setWarningUnpublish(true), };
isLoading: status === 'unpublish-pending',
}); headerActions.unshift(action);
} }
return headerActions; return headerActions;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ }, [
isCreatingEntry, isCreatingEntry,
canCreate, canCreate,
canUpdate, canUpdate,
hasDraftAndPublish, hasDraftAndPublish,
canPublish, canPublish,
canDelete,
didChangeData, didChangeData,
formatMessage, formatMessage,
status, status,

View File

@ -244,7 +244,7 @@ const EditViewDataManagerProvider = ({
abortController.abort(); abortController.abort();
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [id, slug, isCreatingEntry, isLoadingForPermissions]); }, [id, slug, isLoadingForPermissions]);
const addComponentToDynamicZone = useCallback((keys, componentUid, shouldCheckErrors = false) => { const addComponentToDynamicZone = useCallback((keys, componentUid, shouldCheckErrors = false) => {
emitEvent('didAddComponentToDynamicZone'); emitEvent('didAddComponentToDynamicZone');