|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { useDashboardStore } from "@/lib/store/useDashboardStore"; |
| 5 | +import { DashboardDetail } from "@/lib/types"; |
| 6 | +import { fetchDashboard, putDashboard } from "@/lib/apis/dashboardsApi"; |
| 7 | +import ColorPalette, { |
| 8 | + ColorCode, |
| 9 | +} from "@/components/common/color-palette/ColorPalette"; |
3 | 10 | import Input from "@/components/common/input/Input"; |
4 | 11 | import Button from "@/components/common/button/Button"; |
5 | 12 |
|
6 | | -export default function DashboardEditSection() { |
| 13 | +export default function DashboardEditSection({ |
| 14 | + id, |
| 15 | + token, |
| 16 | +}: { |
| 17 | + id: number; |
| 18 | + token: string; |
| 19 | +}) { |
| 20 | + const [data, setData] = useState<DashboardDetail | null>(null); |
| 21 | + const [dashboardName, setDashboardName] = useState(""); |
| 22 | + const [selectedColor, setSelectedColor] = useState<ColorCode | "">(""); |
| 23 | + const [isFormValid, setIsFormValid] = useState(false); |
| 24 | + const setDashboardId = useDashboardStore((state) => state.setDashboardId); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const getData = async () => { |
| 28 | + const data = await fetchDashboard({ |
| 29 | + token, |
| 30 | + id: String(id), |
| 31 | + }); |
| 32 | + setData(data); |
| 33 | + setDashboardName(data.title); |
| 34 | + setSelectedColor(data.color); |
| 35 | + }; |
| 36 | + |
| 37 | + getData(); |
| 38 | + }, []); |
| 39 | + |
| 40 | + const onColorSelect = (color: ColorCode | "") => { |
| 41 | + setSelectedColor(() => { |
| 42 | + return color; |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + const trimmedValue = dashboardName.trim(); |
| 48 | + const isValid = trimmedValue !== "" && selectedColor !== ""; |
| 49 | + setIsFormValid(isValid); |
| 50 | + }, [dashboardName, selectedColor]); |
| 51 | + |
| 52 | + const onDashboardNameChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 53 | + setDashboardName(e.target.value); |
| 54 | + }; |
| 55 | + |
| 56 | + const editDashboard = async () => { |
| 57 | + await putDashboard({ |
| 58 | + token, |
| 59 | + title: dashboardName, |
| 60 | + color: selectedColor, |
| 61 | + id, |
| 62 | + }); |
| 63 | + |
| 64 | + window.location.replace(`/dashboard/${id}`); |
| 65 | + setDashboardId(String(id)); |
| 66 | + }; |
| 67 | + |
| 68 | + if (!data) return; |
| 69 | + |
7 | 70 | return ( |
8 | 71 | <div className="w-full p-4 rounded-lg bg-white tablet:p-6"> |
9 | 72 | <div className="flex flex-col gap-10 tablet:gap-6"> |
10 | | - <div className="font-bold text-2lg text-gray-800 tablet:text-2xl"> |
11 | | - 대시보드 이름 |
| 73 | + <div className="font-bold text-xl text-gray-800 tablet:text-2xl"> |
| 74 | + {data.title} |
12 | 75 | </div> |
13 | | - <div className="flex flex-col gap-6"> |
| 76 | + <div className="flex flex-col gap-8 tablet:gap-10"> |
14 | 77 | <div className="flex flex-col gap-4"> |
15 | | - <Input label="대시보드 이름" /> |
| 78 | + <Input |
| 79 | + label="대시보드 이름" |
| 80 | + value={dashboardName} |
| 81 | + placeholder="대시보드 이름을 입력하세요" |
| 82 | + onChange={onDashboardNameChange} |
| 83 | + /> |
| 84 | + <ColorPalette |
| 85 | + onSelect={onColorSelect} |
| 86 | + selectedColor={selectedColor} |
| 87 | + /> |
16 | 88 | </div> |
17 | | - <Button variant="purple">변경</Button> |
| 89 | + <Button |
| 90 | + variant="purple" |
| 91 | + onClick={editDashboard} |
| 92 | + disabled={!isFormValid} |
| 93 | + > |
| 94 | + 변경 |
| 95 | + </Button> |
18 | 96 | </div> |
19 | 97 | </div> |
20 | 98 | </div> |
|
0 commit comments