1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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<void>;
lng: string;
}
export function FaqManager({ initialData, onSave, lng }: FaqManagerProps) {
const [categories, setCategories] = useState<FaqCategory[]>(initialData);
const [activeTab, setActiveTab] = useState<string>(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 (
<div className="space-y-6">
<div className="flex justify-between items-center">
<div className="flex items-center space-x-2">
<Button
variant={isSettingsView ? "default" : "outline"}
onClick={() => setIsSettingsView(true)}
>
<Settings className="w-4 h-4 mr-2" />
Category Settings
</Button>
<Button
variant={!isSettingsView ? "default" : "outline"}
onClick={() => setIsSettingsView(false)}
>
FAQ Management
</Button>
</div>
<Button onClick={() => setShowSaveDialog(true)}>
<Save className="w-4 h-4 mr-2" />
Save Changes
</Button>
</div>
{isSettingsView ? (
<div className="grid gap-4">
{categories.map((category, index) => (
<Card key={index} className="p-4">
<div className="flex space-x-4 items-center">
<Input
value={category.label}
onChange={(e) => updateCategory(index, e.target.value)}
className="flex-1"
placeholder="Category Name"
/>
<Button variant="destructive" size="icon" onClick={() => removeCategory(index)}>
<Trash className="w-4 h-4" />
</Button>
</div>
</Card>
))}
<Button onClick={addCategory} variant="outline" className="w-full">
<Plus className="w-4 h-4 mr-2" />
Add Category
</Button>
</div>
) : (
<Tabs value={activeTab} onValueChange={setActiveTab} className="space-y-4">
<TabsList>
{categories.map((category) => (
<TabsTrigger key={category.label} value={category.label}>
{category.label}
</TabsTrigger>
))}
</TabsList>
{categories.map((category, categoryIndex) => (
<TabsContent key={category.label} value={category.label} className="space-y-4">
{category.items.map((item, itemIndex) => (
<Card key={itemIndex}>
<CardContent className="space-y-2 pt-6">
<div className="flex justify-between items-start">
<Input
value={item.title}
onChange={(e) => updateItem(categoryIndex, itemIndex, 'title', e.target.value)}
className="flex-1 mr-2"
placeholder="Question"
/>
<Button variant="destructive" size="icon" onClick={() => removeItem(categoryIndex, itemIndex)}>
<Trash className="w-4 h-4" />
</Button>
</div>
<Textarea
value={item.content.join('\n')}
onChange={(e) => updateItem(categoryIndex, itemIndex, 'content', e.target.value)}
placeholder="Answer (separate lines with Enter)"
rows={3}
/>
</CardContent>
</Card>
))}
<Button variant="outline" onClick={() => addItem(categoryIndex)} className="w-full">
<Plus className="w-4 h-4 mr-2" />
Add FAQ Item
</Button>
</TabsContent>
))}
</Tabs>
)}
<Dialog open={showSaveDialog} onOpenChange={setShowSaveDialog}>
<DialogContent>
<DialogHeader>
<DialogTitle>Save Changes</DialogTitle>
<DialogDescription>
Are you sure you want to save the changes? This will update the FAQ data.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => setShowSaveDialog(false)}>
Cancel
</Button>
<Button onClick={handleSave}>
Save
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}
|