-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsaved.tsx
More file actions
268 lines (248 loc) · 8.16 KB
/
Copy pathsaved.tsx
File metadata and controls
268 lines (248 loc) · 8.16 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"use client";
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import Navbar from "@/components/ui/Navbar";
import { ZButton } from "@/components/ui/Buttons";
import Footer from "@/components/ui/Footer";
import Popup from "@/components/ui/Popup";
import Image from "next/image";
import AlertModal from "@/components/ui/AlertModal";
import axios from "axios";
import Loader from "@/components/ui/Loader";
async function fetchTimetablesByOwner(owner: string) {
const res = await axios.get(
`/api/timetables?owner=${encodeURIComponent(owner)}`
);
return res.data;
}
interface TimetableEntry {
_id: string;
title: string;
isPublic: boolean;
shareId?: string;
slots: {
slot: string;
courseCode: string;
courseName: string;
facultyName: string;
}[];
}
type PopupSlot = {
code: string;
slot: string;
name: string;
};
export default function Saved() {
const router = useRouter();
const { data: session } = useSession();
const userEmail = session?.user?.email;
const [timetables, setTimetables] = useState<TimetableEntry[]>([]);
const [loading, setLoading] = useState(true);
const [showPopup, setShowPopup] = useState(false);
const [popupType, setPopupType] = useState<
"view_tt" | "delete_tt" | "rename_tt" | null
>(null);
const [popupSlots, setPopupSlots] = useState<PopupSlot[]>([]);
const [popupTitle, setPopupTitle] = useState("");
const [selectedTT, setSelectedTT] = useState<TimetableEntry | null>(null);
const [renameValue, setRenameValue] = useState("");
const [alertOpen, setAlertOpen] = useState(false);
const [alertMsg, setAlertMsg] = useState("");
const [publicToggle, setPublicToggle] = useState(false);
useEffect(() => {
if (!userEmail) return;
setLoading(true);
fetchTimetablesByOwner(userEmail)
.then(setTimetables)
.catch(() => setTimetables([]))
.finally(() => setLoading(false));
}, [userEmail]);
function convertSlots(slots: TimetableEntry["slots"]): PopupSlot[] {
return slots.map((s) => ({
code: s.courseCode,
slot: s.slot,
name: s.facultyName,
}));
}
async function handleDelete() {
if (!selectedTT) return;
await axios.delete(`/api/timetables/${selectedTT._id}`);
setTimetables((prev) => prev.filter((t) => t._id !== selectedTT._id));
const savedTimetables = JSON.parse(localStorage.getItem('savedTimetables') || '[]') as { shareId: string }[];
const updatedTimetables = savedTimetables.filter((tt: { shareId: string }) => tt.shareId !== selectedTT.shareId);
localStorage.setItem('savedTimetables', JSON.stringify(updatedTimetables));
closePopup("Timetable has been deleted.");
}
async function handleRename() {
if (!selectedTT) return;
await axios.patch(`/api/timetables/${selectedTT._id}`, {
title: renameValue,
});
setTimetables((prev) =>
prev.map((t) =>
t._id === selectedTT._id ? { ...t, title: renameValue } : t
)
);
closePopup("Timetable has been renamed.");
}
function openView(tt: TimetableEntry) {
setPopupSlots(convertSlots(tt.slots));
setPopupTitle(tt.title);
setSelectedTT(tt);
setPublicToggle(tt.isPublic);
setPopupType("view_tt");
setShowPopup(true);
}
async function handleCopyLink() {
if (!selectedTT) return;
// ensure shareId exists
if (!selectedTT.isPublic) {
await axios.patch(`/api/timetables/${selectedTT._id}`, {
isPublic: true,
});
selectedTT.isPublic = true;
setPublicToggle(true);
setTimetables((prev) =>
prev.map((t) =>
t._id === selectedTT._id ? { ...t, isPublic: true } : t
)
);
}
const { data } = await axios.get(`/api/timetables/${selectedTT._id}`);
const url = `${window.location.origin}/share/${data.shareId}`;
await navigator.clipboard.writeText(url);
setAlertMsg("Link copied!");
setAlertOpen(true);
}
async function handleTogglePublic(state: "on" | "off") {
if (!selectedTT) return;
const isPub = state === "on";
setPublicToggle(isPub);
await axios.patch(`/api/timetables/${selectedTT._id}`, { isPublic: isPub });
setTimetables((prev) =>
prev.map((t) =>
t._id === selectedTT._id ? { ...t, isPublic: isPub } : t
)
);
}
function closePopup(message: string) {
setShowPopup(false);
setSelectedTT(null);
setAlertMsg(message);
setAlertOpen(true);
}
return (
<div className="flex flex-col min-h-screen relative">
<div className="absolute inset-0 -z-10 bg-[#CEE4E5]">
<Image
src="/art/bg_dots.svg"
alt="Background"
fill
priority
className="object-top object-contain"
/>
</div>
<Navbar page="saved" />
<div className="flex-1 flex flex-col items-center">
<h1 className="text-6xl mt-48 mb-16 font-pangolin">Saved Timetables</h1>
<div className="w-5/6 max-w-7xl rounded-[60px] border-4 border-black bg-[#A7D5D7] p-12 mb-24 shadow-[4px_4px_0_0_black]">
<h2 className="text-4xl mb-8 font-pangolin font-light">
All Timetables
</h2>
{loading ? (
<Loader />
) : timetables.length === 0 ? (
<div className="flex flex-col items-center">
<p className="text-3xl mb-6">(No Timetables Found)</p>
<ZButton
onClick={() => router.push("/")}
type="large"
text="Home"
color="purple"
image="/icons/home.svg"
/>
</div>
) : (
<ul className="space-y-4 max-h-[60vh] overflow-y-auto pr-4">
{timetables.map((tt, i) => (
<li
key={tt._id}
className="flex items-center justify-between bg-[#C9E5E6] p-5 rounded-4xl"
>
<span className="text-xl">
{i + 1}. {tt.title}
</span>
<div className="flex gap-2">
<ZButton
type="image"
color="yellow"
image="/icons/eye.svg"
onClick={() => openView(tt)}
/>
<ZButton
type="image"
color="blue"
image="/icons/edit.svg"
onClick={() => {
setSelectedTT(tt);
setRenameValue(tt.title);
setPopupType("rename_tt");
setShowPopup(true);
}}
/>
<ZButton
type="image"
color="red"
image="/icons/trash.svg"
onClick={() => {
setSelectedTT(tt);
setPopupType("delete_tt");
setShowPopup(true);
}}
/>
</div>
</li>
))}
</ul>
)}
</div>
</div>
<Footer />
{showPopup && selectedTT && popupType === "view_tt" && (
<Popup
type="view_tt"
dataTitle={popupTitle}
dataTT={popupSlots}
closeLink={() => setShowPopup(false)}
action={handleCopyLink}
shareEnabledDefault={publicToggle}
shareSwitchAction={handleTogglePublic}
/>
)}
{showPopup && selectedTT && popupType === "delete_tt" && (
<Popup
type="delete_tt"
dataBody={selectedTT.title}
closeLink={() => setShowPopup(false)}
action={handleDelete}
/>
)}
{showPopup && selectedTT && popupType === "rename_tt" && (
<Popup
type="rename_tt"
dataBody={renameValue}
closeLink={() => setShowPopup(false)}
action={handleRename}
onInputChange={setRenameValue}
/>
)}
<AlertModal
open={alertOpen}
message={alertMsg}
onClose={() => setAlertOpen(false)}
color="purple"
/>
</div>
);
}