From aaaf07b69f4ffc9396fdaa8c4835f3d08ba821ab Mon Sep 17 00:00:00 2001 From: Irori235 Date: Thu, 1 Jun 2023 17:07:35 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E3=82=AB=E3=83=86=E3=82=B4?= =?UTF-8?q?=E3=83=AA=E3=83=BC=E3=82=92=E5=86=8D=E5=88=86=E9=A1=9E=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/AddItemForm.tsx | 69 +++++++------------ src/components/EditAction.tsx | 44 ++++++++++++ src/components/RenameCategory.tsx | 58 ++++++++++++++++ .../foundation/Dropdown/Dropdown.tsx | 56 +++++++++++++++ src/components/foundation/Modal/Modal.tsx | 66 ++++++++++++++++++ src/pages/BudgetPage.tsx | 47 ++++++++----- 6 files changed, 280 insertions(+), 60 deletions(-) create mode 100644 src/components/EditAction.tsx create mode 100644 src/components/RenameCategory.tsx create mode 100644 src/components/foundation/Dropdown/Dropdown.tsx create mode 100644 src/components/foundation/Modal/Modal.tsx diff --git a/src/components/AddItemForm.tsx b/src/components/AddItemForm.tsx index a3efbe5..76decf1 100644 --- a/src/components/AddItemForm.tsx +++ b/src/components/AddItemForm.tsx @@ -4,6 +4,7 @@ import Input from "./foundation/Input/Input"; import Button from "./foundation/Button/Button"; import Toast from "./foundation/Toast/Toast"; import { Transition } from "@headlessui/react"; +import Modal from "./foundation/Modal/Modal"; interface AddItemFormProps { budget: Budget | null; @@ -95,50 +96,32 @@ const AddItemForm: React.FC = ({ - -
-
-
-
- -
- -
- - - - -
-
+ + +
+ + + +
- +
); }; diff --git a/src/components/EditAction.tsx b/src/components/EditAction.tsx new file mode 100644 index 0000000..c905890 --- /dev/null +++ b/src/components/EditAction.tsx @@ -0,0 +1,44 @@ +import React, { useState } from "react"; +import Dropdown from "./foundation/Dropdown/Dropdown"; +import Modal from "./foundation/Modal/Modal"; +import Input from "./foundation/Input/Input"; +import Button from "./foundation/Button/Button"; +import Toast from "./foundation/Toast/Toast"; + +import RenameCategory from "./RenameCategory"; + +interface EditActionProps { + handleRenameCategory: (newCategoryName: string) => void; + handleDeleteItems: () => void; +} + +const EditAction: React.FC = ({ + handleRenameCategory, + handleDeleteItems, +}) => { + const [isModalOpen, setIsModalOpen] = useState(false); + + const dropdownOptions = [ + { + label: "Rename", + action: () => setIsModalOpen(true), + }, + { + label: "Delete", + action: handleDeleteItems, + }, + ]; + + return ( + <> + + + + ); +}; + +export default EditAction; diff --git a/src/components/RenameCategory.tsx b/src/components/RenameCategory.tsx new file mode 100644 index 0000000..8b81305 --- /dev/null +++ b/src/components/RenameCategory.tsx @@ -0,0 +1,58 @@ +import React, { useState } from "react"; +import Dropdown from "./foundation/Dropdown/Dropdown"; +import Modal from "./foundation/Modal/Modal"; +import Input from "./foundation/Input/Input"; +import Button from "./foundation/Button/Button"; +import Toast from "./foundation/Toast/Toast"; + +interface RenameCategoryProps { + isOpen: boolean; + setIsOpen: React.Dispatch>; + handleRenameCategory: (newCategoryName: string) => void; +} + +const RenameCategory: React.FC = ({ + isOpen, + setIsOpen, + handleRenameCategory, +}) => { + const [categoryName, setCategoryName] = useState(""); + const [showToast, setShowToast] = useState(false); + + const triggerToast = () => { + setShowToast(true); + setTimeout(() => { + setShowToast(false); + }, 3000); + }; + + const onClickUpdateCategory = () => { + if (categoryName) { + handleRenameCategory(categoryName); + setCategoryName(""); + setIsOpen(false); + } else { + triggerToast(); + } + }; + + return ( + + +
+ + +
+
+ ); +}; + +export default RenameCategory; diff --git a/src/components/foundation/Dropdown/Dropdown.tsx b/src/components/foundation/Dropdown/Dropdown.tsx new file mode 100644 index 0000000..96114fd --- /dev/null +++ b/src/components/foundation/Dropdown/Dropdown.tsx @@ -0,0 +1,56 @@ +import React from "react"; +import { Menu } from "@headlessui/react"; + +interface DropdownProps { + options: { label: string; action: () => void }[]; +} + +const Dropdown: React.FC = ({ options }) => { + return ( +
+ + {({ open }) => ( + <> + + + + + + {open && ( + + {options.map((option, index) => ( + + {({ active }) => ( + + {option.label} + + )} + + ))} + + )} + + )} + +
+ ); +}; + +export default Dropdown; diff --git a/src/components/foundation/Modal/Modal.tsx b/src/components/foundation/Modal/Modal.tsx new file mode 100644 index 0000000..0eea60e --- /dev/null +++ b/src/components/foundation/Modal/Modal.tsx @@ -0,0 +1,66 @@ +import React from "react"; +import { Dialog, Transition } from "@headlessui/react"; + +interface ModalProps { + isOpen: boolean; + setIsOpen: (value: boolean) => void; + children: React.ReactNode; +} + +const Modal: React.FC = ({ isOpen, setIsOpen, children }) => { + return ( + + +
+ + + + + + + +
+
+
+ +
+ {children} +
+
+
+
+
+
+ ); +}; + +export default Modal; diff --git a/src/pages/BudgetPage.tsx b/src/pages/BudgetPage.tsx index 8862aab..e5d5da7 100644 --- a/src/pages/BudgetPage.tsx +++ b/src/pages/BudgetPage.tsx @@ -11,6 +11,7 @@ import Button from "../components/foundation/Button/Button"; import { PureComponent as ExitIcon } from "react"; import EditMonthBudget from "../components/EditMonthBudget"; import Spiner from "../components/foundation/Spiner/Spiner"; +import EditAction from "../components/EditAction"; const BudgetPage: React.FC = () => { const [user, loading] = useAuthState(auth); @@ -72,7 +73,31 @@ const BudgetPage: React.FC = () => { } }; - const onClickDelete = () => { + const handleRenameCategory = (newCategoryName: string) => { + if (budget) { + const newBudget: Budget = { + ...budget, + categories: budget.categories.map((c) => { + return { + ...c, + items: c.items.filter((i) => !selectedItems.includes(i)), + }; + }), + }; + + const newCategories: Category = { + name: newCategoryName, + items: selectedItems, + }; + + newBudget.categories.push(newCategories); + + onUpdateBudget(newBudget); + setSelectedItems([]); + } + }; + + const handleDeleteItems = () => { if (budget) { const newBudget: Budget = { ...budget, @@ -133,22 +158,10 @@ const BudgetPage: React.FC = () => {
- +