diff --git a/web/components/custom-attributes/object-modal.tsx b/web/components/custom-attributes/object-modal.tsx index 985a2d8d0..4ce2a51b7 100644 --- a/web/components/custom-attributes/object-modal.tsx +++ b/web/components/custom-attributes/object-modal.tsx @@ -30,19 +30,12 @@ export const ObjectModal: React.FC = observer( description: "", }); const [isCreatingObject, setIsCreatingObject] = useState(false); + const [isUpdatingObject, setIsUpdatingObject] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; - const { customAttributes: customAttributesStore } = useMobxStore(); - const { - createEntity, - createEntityAttribute, - createEntityAttributeLoader, - entityAttributes, - fetchEntityDetails, - fetchEntityDetailsLoader, - } = customAttributesStore; + const { customAttributes } = useMobxStore(); const handleClose = () => { onClose(); @@ -52,7 +45,7 @@ export const ObjectModal: React.FC = observer( }, 300); }; - const handleCreateEntity = async () => { + const handleCreateObject = async () => { if (!workspaceSlug || !projectId) return; setIsCreatingObject(true); @@ -64,7 +57,8 @@ export const ObjectModal: React.FC = observer( type: "entity", }; - await createEntity(workspaceSlug.toString(), payload) + await customAttributes + .createEntity(workspaceSlug.toString(), payload) .then((res) => { setObject((prevData) => ({ ...prevData, ...res })); if (onSubmit) onSubmit(); @@ -72,6 +66,21 @@ export const ObjectModal: React.FC = observer( .finally(() => setIsCreatingObject(false)); }; + const handleUpdateObject = async () => { + if (!workspaceSlug || !object || !object.id) return; + + setIsUpdatingObject(true); + + const payload: Partial = { + description: object.description ?? "", + display_name: object.display_name ?? "", + }; + + await customAttributes + .updateEntity(workspaceSlug.toString(), object.id, payload) + .finally(() => setIsUpdatingObject(false)); + }; + const handleCreateEntityAttribute = async (type: TCustomAttributeTypes) => { if (!workspaceSlug || !object || !object.id) return; @@ -83,21 +92,24 @@ export const ObjectModal: React.FC = observer( ...typeMetaData.initialPayload, }; - await createEntityAttribute(workspaceSlug.toString(), { ...payload, parent: object.id }); + await customAttributes.createEntityAttribute(workspaceSlug.toString(), { + ...payload, + parent: object.id, + }); }; // fetch the object details if object state has id useEffect(() => { if (!object.id || object.id === "") return; - if (!entityAttributes[object.id]) { + if (!customAttributes.entityAttributes[object.id]) { if (!workspaceSlug) return; - fetchEntityDetails(workspaceSlug.toString(), object.id).then((res) => { + customAttributes.fetchEntityDetails(workspaceSlug.toString(), object.id).then((res) => { setObject({ ...res }); }); } - }, [object.id, workspaceSlug, fetchEntityDetails, entityAttributes]); + }, [customAttributes, object.id, workspaceSlug]); // update the object state if objectIdToEdit is present useEffect(() => { @@ -163,30 +175,40 @@ export const ObjectModal: React.FC = observer( setObject((prevData) => ({ ...prevData, description: e.target.value })) } /> + {object.id && ( +
+ + {isUpdatingObject ? "Saving..." : "Save changes"} + +
+ )} {object.id && (

Attributes

- {fetchEntityDetailsLoader ? ( + {customAttributes.fetchEntityDetailsLoader ? ( ) : ( - Object.keys(entityAttributes[object.id] ?? {})?.map((attributeId) => { - const attribute = entityAttributes[object.id ?? ""][attributeId]; + Object.keys(customAttributes.entityAttributes[object.id] ?? {})?.map( + (attributeId) => { + const attribute = + customAttributes.entityAttributes[object.id ?? ""][attributeId]; - return ( - - ); - }) + return ( + + ); + } + ) )} - {createEntityAttributeLoader && ( + {customAttributes.createEntityAttributeLoader && ( @@ -200,13 +222,11 @@ export const ObjectModal: React.FC = observer(
Cancel - - {object.id - ? "Save changes" - : isCreatingObject - ? "Creating..." - : "Create Object"} - + {!object.id && ( + + {isCreatingObject ? "Creating..." : "Create Object"} + + )}
diff --git a/web/store/custom-attributes.ts b/web/store/custom-attributes.ts index e73d62fa0..a08262baa 100644 --- a/web/store/custom-attributes.ts +++ b/web/store/custom-attributes.ts @@ -27,6 +27,7 @@ class CustomAttributesStore { fetchEntities: action, fetchEntityDetails: action, createEntity: action, + updateEntity: action, deleteEntity: action, createEntityAttribute: action, updateEntityAttribute: action, @@ -112,6 +113,30 @@ class CustomAttributesStore { } }; + updateEntity = async ( + workspaceSlug: string, + objectId: string, + data: Partial + ) => { + try { + const response = await customAttributesService.patchProperty(workspaceSlug, objectId, data); + + const newEntities = [...(this.entities ?? [])].map((entity) => + entity.id === objectId ? { ...entity, ...response } : entity + ); + + runInAction(() => { + this.entities = newEntities; + }); + + return response; + } catch (error) { + runInAction(() => { + this.error = error; + }); + } + }; + deleteEntity = async (workspaceSlug: string, propertyId: string) => { try { await customAttributesService.deleteProperty(workspaceSlug, propertyId);