feat: add Object Field Edit page sections (#2243)
Closes #2160, Closes #2163
This commit is contained in:
@ -20,7 +20,7 @@ export const SettingsObjectEdit = () => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { objectSlug = '' } = useParams();
|
||||
const { activeObjects, disableObject, editObject, findActiveObjectBySlug } =
|
||||
const { disableObject, editObject, findActiveObjectBySlug, loading } =
|
||||
useObjectMetadata();
|
||||
const activeObject = findActiveObjectBySlug(objectSlug);
|
||||
|
||||
@ -34,7 +34,7 @@ export const SettingsObjectEdit = () => {
|
||||
>({});
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeObjects.length) return;
|
||||
if (loading) return;
|
||||
|
||||
if (!activeObject) {
|
||||
navigate(AppPath.NotFound);
|
||||
@ -49,22 +49,22 @@ export const SettingsObjectEdit = () => {
|
||||
description: activeObject.description ?? undefined,
|
||||
});
|
||||
}
|
||||
}, [activeObject, activeObjects.length, formValues, navigate]);
|
||||
}, [activeObject, formValues, loading, navigate]);
|
||||
|
||||
if (!activeObject) return null;
|
||||
|
||||
const areRequiredFieldsFilled =
|
||||
!!formValues.labelSingular && !!formValues.labelPlural;
|
||||
|
||||
const hasChanges =
|
||||
formValues.description !== activeObject?.description ||
|
||||
formValues.icon !== activeObject?.icon ||
|
||||
formValues.labelPlural !== activeObject?.labelPlural ||
|
||||
formValues.labelSingular !== activeObject?.labelSingular;
|
||||
formValues.description !== activeObject.description ||
|
||||
formValues.icon !== activeObject.icon ||
|
||||
formValues.labelPlural !== activeObject.labelPlural ||
|
||||
formValues.labelSingular !== activeObject.labelSingular;
|
||||
|
||||
const canSave = areRequiredFieldsFilled && hasChanges;
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!activeObject) return;
|
||||
|
||||
const editedObject = { ...activeObject, ...formValues };
|
||||
|
||||
await editObject(editedObject);
|
||||
@ -72,6 +72,11 @@ export const SettingsObjectEdit = () => {
|
||||
navigate(`/settings/objects/${getObjectSlug(editedObject)}`);
|
||||
};
|
||||
|
||||
const handleDisable = async () => {
|
||||
await disableObject(activeObject);
|
||||
navigate('/settings/objects');
|
||||
};
|
||||
|
||||
return (
|
||||
<SubMenuTopBarContainer Icon={IconSettings} title="Settings">
|
||||
<SettingsPageContainer>
|
||||
@ -80,61 +85,52 @@ export const SettingsObjectEdit = () => {
|
||||
links={[
|
||||
{ children: 'Objects', href: '/settings/objects' },
|
||||
{
|
||||
children: activeObject?.labelPlural ?? '',
|
||||
children: activeObject.labelPlural,
|
||||
href: `/settings/objects/${objectSlug}`,
|
||||
},
|
||||
{ children: 'Edit' },
|
||||
]}
|
||||
/>
|
||||
{!!activeObject?.isCustom && (
|
||||
{!!activeObject.isCustom && (
|
||||
<SaveAndCancelButtons
|
||||
isSaveDisabled={!canSave}
|
||||
onCancel={() => {
|
||||
navigate(`/settings/objects/${objectSlug}`);
|
||||
}}
|
||||
onCancel={() => navigate(`/settings/objects/${objectSlug}`)}
|
||||
onSave={handleSave}
|
||||
/>
|
||||
)}
|
||||
</SettingsHeaderContainer>
|
||||
{activeObject && (
|
||||
<>
|
||||
<SettingsObjectIconSection
|
||||
disabled={!activeObject.isCustom}
|
||||
iconKey={formValues.icon}
|
||||
label={formValues.labelPlural}
|
||||
onChange={({ iconKey }) =>
|
||||
setFormValues((previousFormValues) => ({
|
||||
...previousFormValues,
|
||||
icon: iconKey,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
<SettingsObjectFormSection
|
||||
disabled={!activeObject.isCustom}
|
||||
singularName={formValues.labelSingular}
|
||||
pluralName={formValues.labelPlural}
|
||||
description={formValues.description}
|
||||
onChange={(values) =>
|
||||
setFormValues((previousFormValues) => ({
|
||||
...previousFormValues,
|
||||
...values,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
<Section>
|
||||
<H2Title title="Danger zone" description="Disable object" />
|
||||
<Button
|
||||
Icon={IconArchive}
|
||||
title="Disable"
|
||||
size="small"
|
||||
onClick={() => {
|
||||
disableObject(activeObject);
|
||||
navigate('/settings/objects');
|
||||
}}
|
||||
/>
|
||||
</Section>
|
||||
</>
|
||||
)}
|
||||
<SettingsObjectIconSection
|
||||
disabled={!activeObject.isCustom}
|
||||
iconKey={formValues.icon}
|
||||
label={formValues.labelPlural}
|
||||
onChange={({ iconKey }) =>
|
||||
setFormValues((previousFormValues) => ({
|
||||
...previousFormValues,
|
||||
icon: iconKey,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
<SettingsObjectFormSection
|
||||
disabled={!activeObject.isCustom}
|
||||
singularName={formValues.labelSingular}
|
||||
pluralName={formValues.labelPlural}
|
||||
description={formValues.description}
|
||||
onChange={(values) =>
|
||||
setFormValues((previousFormValues) => ({
|
||||
...previousFormValues,
|
||||
...values,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
<Section>
|
||||
<H2Title title="Danger zone" description="Disable object" />
|
||||
<Button
|
||||
Icon={IconArchive}
|
||||
title="Disable"
|
||||
size="small"
|
||||
onClick={handleDisable}
|
||||
/>
|
||||
</Section>
|
||||
</SettingsPageContainer>
|
||||
</SubMenuTopBarContainer>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user