From 3c3f0f75811144050b46350c1c8a861f788365fa Mon Sep 17 00:00:00 2001
From: Aaryan Khandelwal
Date: Wed, 20 Sep 2023 18:52:38 +0530
Subject: [PATCH] chore: edit/delete option
---
.../select-attribute/option-form.tsx | 106 +++++++++++++-----
.../select-attribute-form.tsx | 20 +++-
.../select-attribute/select-option.tsx | 7 +-
3 files changed, 100 insertions(+), 33 deletions(-)
diff --git a/web/components/custom-attributes/attribute-forms/select-attribute/option-form.tsx b/web/components/custom-attributes/attribute-forms/select-attribute/option-form.tsx
index 2ad0c1147..ef032ab3e 100644
--- a/web/components/custom-attributes/attribute-forms/select-attribute/option-form.tsx
+++ b/web/components/custom-attributes/attribute-forms/select-attribute/option-form.tsx
@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useEffect, useState } from "react";
import { useRouter } from "next/router";
@@ -13,13 +13,20 @@ import { PrimaryButton } from "components/ui";
import { ICustomAttribute } from "types";
type Props = {
+ data: ICustomAttribute | null;
objectId: string;
+ onSubmit?: () => void;
parentId: string;
};
-export const OptionForm: React.FC = observer(({ objectId, parentId }) => {
- const [optionName, setOptionName] = useState("");
- const [optionColor, setOptionColor] = useState("#000000");
+export const OptionForm: React.FC = observer((props) => {
+ const { data, objectId, onSubmit, parentId } = props;
+
+ const [option, setOption] = useState>({
+ display_name: "",
+ color: "#000000",
+ });
+ const [isEditing, setIsEditing] = useState(false);
const router = useRouter();
const { workspaceSlug } = router.query;
@@ -29,47 +36,90 @@ export const OptionForm: React.FC = observer(({ objectId, parentId }) =>
const handleCreateOption = async () => {
if (!workspaceSlug) return;
- if (!optionName || optionName === "") return;
+ if (option.display_name === "") return;
const payload: Partial = {
- color: optionColor,
- display_name: optionName,
+ color: option.color,
+ display_name: option.display_name,
type: "option",
};
- await customAttributes
- .createAttributeOption(workspaceSlug.toString(), objectId, {
- ...payload,
- parent: parentId,
- })
- .then(() => {
- setOptionName("");
- setOptionColor("#000000");
- });
+ await customAttributes.createAttributeOption(workspaceSlug.toString(), objectId, {
+ ...payload,
+ parent: parentId,
+ });
};
+ const handleUpdateOption = async () => {
+ if (!workspaceSlug) return;
+
+ if (option.display_name === "" || !option.parent || !option.id) return;
+
+ setIsEditing(true);
+
+ const payload: Partial = {
+ color: option.color,
+ display_name: option.display_name,
+ };
+
+ await customAttributes
+ .updateAttributeOption(workspaceSlug.toString(), objectId, option.parent, option.id, payload)
+ .finally(() => setIsEditing(false));
+ };
+
+ const handleFormSubmit = async () => {
+ if (data) await handleUpdateOption();
+ else await handleCreateOption();
+
+ setOption({
+ display_name: "",
+ color: "#000000",
+ });
+
+ if (onSubmit) onSubmit();
+ };
+
+ useEffect(() => {
+ if (!data) return;
+
+ setOption({ ...data });
+ }, [data]);
+
return (
- {/* 🚀 */}
setOptionName(e.target.value)}
+ value={option.display_name}
+ onChange={(e) => setOption((prev) => ({ ...prev, display_name: e.target.value }))}
placeholder="Enter new option"
/>
- setOptionColor(val)} selectedColor={optionColor} />
+ setOption((prev) => ({ ...prev, color: val }))}
+ selectedColor={option.color ?? "#000000"}
+ />
-
- {customAttributes.createAttributeOptionLoader ? "Adding..." : "Add"}
-
+ {data ? (
+
+ {isEditing ? "Updating..." : "Update"}
+
+ ) : (
+
+ {customAttributes.createAttributeOptionLoader ? "Adding..." : "Add"}
+
+ )}
);
diff --git a/web/components/custom-attributes/attribute-forms/select-attribute/select-attribute-form.tsx b/web/components/custom-attributes/attribute-forms/select-attribute/select-attribute-form.tsx
index 94504efb0..5873732f8 100644
--- a/web/components/custom-attributes/attribute-forms/select-attribute/select-attribute-form.tsx
+++ b/web/components/custom-attributes/attribute-forms/select-attribute/select-attribute-form.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useState } from "react";
// mobx
import { observer } from "mobx-react-lite";
@@ -7,9 +7,13 @@ import { useMobxStore } from "lib/mobx/store-provider";
import { Controller } from "react-hook-form";
// components
import { FormComponentProps, Input, OptionForm, SelectOption } from "components/custom-attributes";
+// types
+import { ICustomAttribute } from "types";
export const SelectAttributeForm: React.FC = observer(
({ control, multiple = false, objectId = "", watch }) => {
+ const [optionToEdit, setOptionToEdit] = useState(null);
+
const { customAttributes: customAttributesStore } = useMobxStore();
const { entityAttributes } = customAttributesStore;
@@ -28,11 +32,21 @@ export const SelectAttributeForm: React.FCOptions
{options?.map((option) => (
-
+ setOptionToEdit(option)}
+ objectId={objectId}
+ option={option}
+ />
))}
-
+ setOptionToEdit(null)}
+ parentId={watch("id") ?? ""}
+ />
diff --git a/web/components/custom-attributes/attribute-forms/select-attribute/select-option.tsx b/web/components/custom-attributes/attribute-forms/select-attribute/select-option.tsx
index d4f704abb..a41dffbb8 100644
--- a/web/components/custom-attributes/attribute-forms/select-attribute/select-option.tsx
+++ b/web/components/custom-attributes/attribute-forms/select-attribute/select-option.tsx
@@ -11,11 +11,14 @@ import { MoreHorizontal } from "lucide-react";
import { ICustomAttribute } from "types";
type Props = {
+ handleEditOption: () => void;
objectId: string;
option: ICustomAttribute;
};
-export const SelectOption: React.FC = observer(({ objectId, option }) => {
+export const SelectOption: React.FC = observer((props) => {
+ const { handleEditOption, objectId, option } = props;
+
const router = useRouter();
const { workspaceSlug } = router.query;
@@ -82,7 +85,7 @@ export const SelectOption: React.FC = observer(({ objectId, option }) =>
}
>
- Edit
+ Edit
Delete