From 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Tue, 25 Mar 2025 15:55:45 +0900 Subject: initial commit --- components/faq/FaqManager.tsx | 192 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 components/faq/FaqManager.tsx (limited to 'components/faq/FaqManager.tsx') diff --git a/components/faq/FaqManager.tsx b/components/faq/FaqManager.tsx new file mode 100644 index 00000000..27755270 --- /dev/null +++ b/components/faq/FaqManager.tsx @@ -0,0 +1,192 @@ +'use client'; + +import { useState } from 'react'; +import { FaqCategory } from './FaqCard'; +import { Button } from '@/components/ui/button'; +import { Card, CardContent } from '@/components/ui/card'; +import { Input } from '@/components/ui/input'; +import { Textarea } from '@/components/ui/textarea'; +import { Plus, Trash, Save, Settings } from 'lucide-react'; +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; +import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { useRouter } from 'next/navigation'; + +interface FaqManagerProps { + initialData: FaqCategory[]; + onSave: (data: FaqCategory[]) => Promise; + lng: string; +} + +export function FaqManager({ initialData, onSave, lng }: FaqManagerProps) { + const [categories, setCategories] = useState(initialData); + const [activeTab, setActiveTab] = useState(categories[0]?.label || ''); + const [isSettingsView, setIsSettingsView] = useState(false); + const [showSaveDialog, setShowSaveDialog] = useState(false); + const router = useRouter(); + + const addCategory = () => { + const newCategory = { + label: 'New Category', + items: [] + }; + setCategories([...categories, newCategory]); + setActiveTab(newCategory.label); + }; + + const addItem = (categoryIndex: number) => { + const newCategories = [...categories]; + newCategories[categoryIndex].items.push({ + title: 'New FAQ Item', + content: ['Enter content here'] + }); + setCategories(newCategories); + }; + + const handleSave = async () => { + await onSave(categories); + setShowSaveDialog(false); + router.push(`/${lng}/evcp/faq`); + }; + + const updateCategory = (index: number, label: string) => { + const newCategories = [...categories]; + newCategories[index] = { ...newCategories[index], label }; + setCategories(newCategories); + if (activeTab === categories[index].label) { + setActiveTab(label); + } + }; + + const updateItem = (categoryIndex: number, itemIndex: number, field: 'title' | 'content', value: string | string[]) => { + const newCategories = [...categories]; + if (field === 'content') { + newCategories[categoryIndex].items[itemIndex][field] = (value as string).split('\n'); + } else { + newCategories[categoryIndex].items[itemIndex][field] = value as string; + } + setCategories(newCategories); + }; + + const removeCategory = (index: number) => { + const newCategories = categories.filter((_, i) => i !== index); + setCategories(newCategories); + setActiveTab(newCategories[0]?.label || ''); + }; + + const removeItem = (categoryIndex: number, itemIndex: number) => { + const newCategories = [...categories]; + newCategories[categoryIndex].items = newCategories[categoryIndex].items.filter((_, i) => i !== itemIndex); + setCategories(newCategories); + }; + + return ( +
+
+
+ + +
+ +
+ + {isSettingsView ? ( +
+ {categories.map((category, index) => ( + +
+ updateCategory(index, e.target.value)} + className="flex-1" + placeholder="Category Name" + /> + +
+
+ ))} + +
+ ) : ( + + + {categories.map((category) => ( + + {category.label} + + ))} + + + {categories.map((category, categoryIndex) => ( + + {category.items.map((item, itemIndex) => ( + + +
+ updateItem(categoryIndex, itemIndex, 'title', e.target.value)} + className="flex-1 mr-2" + placeholder="Question" + /> + +
+