From 9ca305de9ecf2662e2fa3aef5d9b366e50f6b206 Mon Sep 17 00:00:00 2001 From: monoid Date: Fri, 3 May 2024 01:25:03 +0900 Subject: [PATCH] refactor: extract component --- src/client/components/menu/AddMenuItem.tsx | 89 +++++++ src/client/components/menu/MenuInput.tsx | 63 +++++ src/client/components/menu/MenuItem.tsx | 111 +++++++++ src/client/lib/currencyFormat.tsx | 6 + src/client/lib/onlyNumber.tsx | 3 + src/client/pages/Menu.tsx | 257 +-------------------- 6 files changed, 275 insertions(+), 254 deletions(-) create mode 100644 src/client/components/menu/AddMenuItem.tsx create mode 100644 src/client/components/menu/MenuInput.tsx create mode 100644 src/client/components/menu/MenuItem.tsx create mode 100644 src/client/lib/currencyFormat.tsx create mode 100644 src/client/lib/onlyNumber.tsx diff --git a/src/client/components/menu/AddMenuItem.tsx b/src/client/components/menu/AddMenuItem.tsx new file mode 100644 index 0000000..9c26488 --- /dev/null +++ b/src/client/components/menu/AddMenuItem.tsx @@ -0,0 +1,89 @@ +import { Button } from "@/components/ui/button"; +import { + Drawer, + DrawerContent, + DrawerDescription, + DrawerFooter, + DrawerHeader, + DrawerTitle, + DrawerTrigger, +} from "@/components/ui/drawer"; +import { Input } from "@/components/ui/input"; +import { useAddMenuItem } from "@/hooks/useMenu"; +import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; +import { MenuItemColumns } from "src/db"; +import { MenuInput } from "./MenuInput.tsx"; + +export function AddMenuItem() { + const queryClient = useQueryClient(); + const { mutate, isPending } = useAddMenuItem(queryClient); + const [drawerOpen, setDrawerOpen] = useState(false); + const [data, setData] = useState({ + name: "", + category: "", + HOT: null, + COLD: null, + price: null, + }); + + return ( + + +
+

메뉴 추가

+
+
+ + + 메뉴 추가 + 메뉴를 추가합니다. + +
+
+
메뉴 이름
+ + setData({ + ...data, + name: e.target.value, + })} + /> +
+ { + setData({ + ...data, + ...c, + }); + }} + /> +
+ + + +
+
+ ); +} diff --git a/src/client/components/menu/MenuInput.tsx b/src/client/components/menu/MenuInput.tsx new file mode 100644 index 0000000..b49b0fb --- /dev/null +++ b/src/client/components/menu/MenuInput.tsx @@ -0,0 +1,63 @@ +import { Input } from "@/components/ui/input"; +import { cn } from "@/lib/utils"; +import { UpdateMenuItemType } from "src/schema"; +import { onlyNumber } from "../../lib/onlyNumber.tsx"; + +export function MenuInput({ + data, + onChange, + className = "", +}: { + data: UpdateMenuItemType; + onChange: (data: UpdateMenuItemType) => void; + className?: string; +}) { + return ( +
+
+
카테고리
+ + onChange({ + ...data, + category: e.target.value, + })} + /> +
+
+
Hot 가격
+ + onChange({ + ...data, + HOT: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, + })} + /> +
+
+
Ice 가격
+ + onChange({ + ...data, + COLD: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, + })} + /> +
+
+
그냥 가격
+ + onChange({ + ...data, + price: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, + })} + /> +
+
+ ); +} diff --git a/src/client/components/menu/MenuItem.tsx b/src/client/components/menu/MenuItem.tsx new file mode 100644 index 0000000..5c6563d --- /dev/null +++ b/src/client/components/menu/MenuItem.tsx @@ -0,0 +1,111 @@ +import { Button } from "@/components/ui/button"; +import { + Drawer, + DrawerContent, + DrawerDescription, + DrawerFooter, + DrawerHeader, + DrawerTitle, + DrawerTrigger, +} from "@/components/ui/drawer"; +import { useDeleteMenuItem, useUpdateMenuItem } from "@/hooks/useMenu"; +import { TrashIcon } from "@radix-ui/react-icons"; +import { useQueryClient } from "@tanstack/react-query"; +import { useState } from "react"; +import { MenuItemColumns } from "src/db"; +import { UpdateMenuItemType } from "src/schema"; +import { currencyFormat } from "../../lib/currencyFormat.tsx"; +import { MenuInput } from "./MenuInput.tsx"; + +export function MenuItem({ + item, +}: { + item: MenuItemColumns; +}) { + const [drawerOpen, setDrawerOpen] = useState(false); + const [deleteDrawerOpen, setDeleteDrawerOpen] = useState(false); + const queryClient = useQueryClient(); + const { mutate: mutateUpdate, isPending: isUpdatePending } = useUpdateMenuItem(queryClient); + const { mutate: mutateDelete, isPending: isDeletePending } = useDeleteMenuItem(queryClient); + const [data, setData] = useState({ + category: item.category, + HOT: item.HOT, + COLD: item.COLD, + price: item.price, + }); + + return ( + <> + + + + {item.name} 삭제 + 메뉴를 삭제합니다. + + + + + + + + +
+ + {item.category} +

{item.name}

+ {item.HOT &&

HOT {currencyFormat(item.HOT * 1000)}

} + {item.COLD &&

COLD {currencyFormat(item.COLD * 1000)}

} + {item.price &&

{currencyFormat(item.price * 1000)}

} +
+
+ + + {item.name} 수정 + 메뉴를 수정합니다. + + + + + + +
+ + ); +} diff --git a/src/client/lib/currencyFormat.tsx b/src/client/lib/currencyFormat.tsx new file mode 100644 index 0000000..818e331 --- /dev/null +++ b/src/client/lib/currencyFormat.tsx @@ -0,0 +1,6 @@ +export function currencyFormat(price: number) { + return price.toLocaleString("ko-KR", { + style: "currency", + currency: "KRW", + }); +} diff --git a/src/client/lib/onlyNumber.tsx b/src/client/lib/onlyNumber.tsx new file mode 100644 index 0000000..d998575 --- /dev/null +++ b/src/client/lib/onlyNumber.tsx @@ -0,0 +1,3 @@ +export function onlyNumber(str: string) { + return str.replace(/[^0-9]/g, ""); +} diff --git a/src/client/pages/Menu.tsx b/src/client/pages/Menu.tsx index 4e594e0..44aa73a 100644 --- a/src/client/pages/Menu.tsx +++ b/src/client/pages/Menu.tsx @@ -1,260 +1,9 @@ -import { Button } from "@/components/ui/button"; -import { - Drawer, - DrawerContent, - DrawerDescription, - DrawerFooter, - DrawerHeader, - DrawerTitle, - DrawerTrigger, -} from "@/components/ui/drawer"; -import { Input } from "@/components/ui/input"; -import { useAddMenuItem, useDeleteMenuItem, useMenu, useUpdateMenuItem } from "@/hooks/useMenu"; -import { cn } from "@/lib/utils"; -import { TrashIcon } from "@radix-ui/react-icons"; -import { useQueryClient } from "@tanstack/react-query"; -import { useState } from "react"; -import { MenuItemColumns } from "src/db"; -import { UpdateMenuItemType } from "src/schema"; +import { useMenu } from "@/hooks/useMenu"; +import { AddMenuItem } from "../components/menu/AddMenuItem"; +import { MenuItem } from "../components/menu/MenuItem"; import ErrorMessage from "./ErrorPage"; import LoadingPage from "./Loading"; -function currencyFormat(price: number) { - return price.toLocaleString("ko-KR", { - style: "currency", - currency: "KRW", - }); -} - -function onlyNumber(str: string) { - return str.replace(/[^0-9]/g, ""); -} - -function MenuInput({ - data, - onChange, - className = "", -}: { - data: UpdateMenuItemType; - onChange: (data: UpdateMenuItemType) => void; - className?: string; -}) { - return ( -
-
-
카테고리
- - onChange({ - ...data, - category: e.target.value, - })} - /> -
-
-
Hot 가격
- - onChange({ - ...data, - HOT: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, - })} - /> -
-
-
Ice 가격
- - onChange({ - ...data, - COLD: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, - })} - /> -
-
-
그냥 가격
- - onChange({ - ...data, - price: e.target.value ? parseInt(onlyNumber(e.target.value)) / 1000 : null, - })} - /> -
-
- ); -} - -function MenuItem({ - item, -}: { - item: MenuItemColumns; -}) { - const [drawerOpen, setDrawerOpen] = useState(false); - const [deleteDrawerOpen, setDeleteDrawerOpen] = useState(false); - const queryClient = useQueryClient(); - const { mutate: mutateUpdate, isPending: isUpdatePending } = useUpdateMenuItem(queryClient); - const { mutate: mutateDelete, isPending: isDeletePending } = useDeleteMenuItem(queryClient); - const [data, setData] = useState({ - category: item.category, - HOT: item.HOT, - COLD: item.COLD, - price: item.price, - }); - - return ( - <> - - - - {item.name} 삭제 - 메뉴를 삭제합니다. - - - - - - - - -
- - {item.category} -

{item.name}

- {item.HOT &&

HOT {currencyFormat(item.HOT * 1000)}

} - {item.COLD &&

COLD {currencyFormat(item.COLD * 1000)}

} - {item.price &&

{currencyFormat(item.price * 1000)}

} -
-
- - - {item.name} 수정 - 메뉴를 수정합니다. - - - - - - -
- - ); -} - -function AddMenuItem() { - const queryClient = useQueryClient(); - const { mutate, isPending } = useAddMenuItem(queryClient); - const [drawerOpen, setDrawerOpen] = useState(false); - const [data, setData] = useState({ - name: "", - category: "", - HOT: null, - COLD: null, - price: null, - }); - - return ( - - -
-

메뉴 추가

-
-
- - - 메뉴 추가 - 메뉴를 추가합니다. - -
-
-
메뉴 이름
- - setData({ - ...data, - name: e.target.value, - })} - /> -
- { - setData({ - ...data, - ...c, - }); - }} - /> -
- - - -
-
- ); -} - export default function MenuPage() { const { data: menuItems, status } = useMenu(); if (status === "pending") {