diff --git a/frontend/app/dashboard/page.tsx b/frontend/app/dashboard/page.tsx index 96f3a3f..5e3a5e6 100644 --- a/frontend/app/dashboard/page.tsx +++ b/frontend/app/dashboard/page.tsx @@ -4,14 +4,7 @@ import Link from "next/link"; import { useState, useEffect } from "react"; import { Input } from "@/components/ui/input"; import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert"; -import { - AlertCircle, - OctagonAlert, - Cake, - Gift, - BookOpen, - Coffee, -} from "lucide-react"; +import { AlertCircle, OctagonAlert, Cake, Gift, BookOpen } from "lucide-react"; import { GitHubLogoIcon } from "@radix-ui/react-icons"; import { Select, @@ -50,26 +43,56 @@ export default function Dashboard() { const { email, token, logout, setAuthInfo } = useAuth(); const router = useRouter(); + const [userData, setUserData] = useState({ + email: "", + reminderTime: "", + timeZone: "", + telegramApiKey: "", + telegramUser: "", + newPassword: "", + }); + useEffect(() => { const storedEmail = localStorage.getItem("email"); const storedToken = localStorage.getItem("token"); if (!email && storedEmail && storedToken) { setAuthInfo(storedEmail, storedToken); - localStorage.setItem("email", storedEmail); } else if (!email || !token) { router.push("/"); } }, [email, token, router, setAuthInfo]); - const [userData, setUserData] = useState({ - email: email || "", - reminderTime: "", - timeZone: "", - telegramApiKey: "", - telegramUser: "", - newPassword: "", - }); + useEffect(() => { + setUserData((prevData) => ({ + ...prevData, + email: email || "", + })); + }, [email]); + + useEffect(() => { + const fetchUserData = async () => { + try { + const response = await getUserData(token); + setUserData((prevData) => ({ + ...prevData, + reminderTime: response.reminder_time, + timeZone: response.timezone, + telegramApiKey: response.telegram_bot_api_key, + telegramUser: response.telegram_user_id, + })); + setTimeZone(response.timezone); + if (response.birthdays) { + setBirthdays(response.birthdays); + } + } catch (error) { + console.error("Error fetching user data", error); + } + }; + if (token) { + fetchUserData(); + } + }, [token]); const [name, setName] = useState(""); const [date, setDate] = useState(""); @@ -89,6 +112,7 @@ export default function Dashboard() { const [isTimezoneDisabled, setIsTimezoneDisabled] = useState(true); const [isReminderTimeDisabled, setIsReminderTimeDisabled] = useState(true); const [timeZone, setTimeZone] = useState(userData.timeZone); + const [searchTerm, setSearchTerm] = useState(""); const [editIndex, setEditIndex] = useState(null); const [deleteIndex, setDeleteIndex] = useState(null); const [confirmDeleteUser, setConfirmDeleteUser] = useState(false); @@ -280,8 +304,9 @@ export default function Dashboard() { setDate(e.target.value); }; - const timeZones = Intl.supportedValuesOf("timeZone"); - + const filteredTimeZones = Intl.supportedValuesOf("timeZone").filter((zone) => + zone.toLowerCase().includes(searchTerm.toLowerCase()) + ); const handleTimezoneCheckboxChange = () => { setIsTimezoneDisabled(!isTimezoneDisabled); }; @@ -382,15 +407,25 @@ export default function Dashboard() {
Time Zone: setSearchTerm(e.target.value)} + placeholder="Search time zones" + className="w-full p-2 border rounded" + /> +
+ {filteredTimeZones.map((zone) => ( {zone} diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index cc7a464..535de37 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -42,10 +42,10 @@ export default function Home() { const [loginSuccess, setLoginSuccess] = useState(null); const [reminderTime, setReminderTime] = useState(""); const [timeZone, setTimeZone] = useState(""); + const [searchTerm, setSearchTerm] = useState(""); const [telegramApiKey, setTelegramApiKey] = useState(""); const [telegramUser, setTelegramUser] = useState(""); const [copySuccess, setCopySuccess] = useState(""); - const [isTimezoneDisabled, setIsTimezoneDisabled] = useState(true); const [registerSuccess, setRegisterSuccess] = useState(null); const [registerError, setRegisterError] = useState(null); @@ -87,7 +87,7 @@ export default function Home() { if (isValid) { try { const response = await loginUser({ email, password }); - setAuthInfo(email, password); + setAuthInfo(email, response.token); setLoginSuccess(true); router.push("/dashboard"); } catch (error) { @@ -115,6 +115,7 @@ export default function Home() { console.error("Error generating password", error); } }; + const handleCopyClick = () => { navigator.clipboard.writeText(password).then( () => { @@ -146,8 +147,8 @@ export default function Home() { const response = await registerUser(userData); if (response.token) { localStorage.setItem("token", response.token); + localStorage.setItem("email", email); - const loginResponse = await loginUser({ email, password }); setAuthInfo(email, response.token); setRegisterSuccess(true); @@ -171,7 +172,9 @@ export default function Home() { } }; - const timeZones = Intl.supportedValuesOf("timeZone"); + const filteredTimeZones = Intl.supportedValuesOf("timeZone").filter((zone) => + zone.toLowerCase().includes(searchTerm.toLowerCase()) + ); return (
@@ -347,10 +350,21 @@ export default function Home() { setSearchTerm(e.target.value)} + placeholder="Search time zones" + className="w-full p-2 border rounded" + /> + + {filteredTimeZones.map((zone) => ( {zone} @@ -358,7 +372,7 @@ export default function Home() { - + {" "}

-
-
- - Daniel - - - Fernando - -
); diff --git a/frontend/lib/api/apiService.js b/frontend/lib/api/apiService.js index 50fc36b..e830b72 100644 --- a/frontend/lib/api/apiService.js +++ b/frontend/lib/api/apiService.js @@ -4,7 +4,8 @@ const prefix = "/api"; // Create an Axios instance const api = axios.create({ - // baseURL: "http://localhost:3000", - UNCOMMENT THIS LINE FOR LOCAL TESTING + // UNCOMMENT THE LINE BELOW FOR LOCAL TESTING + // baseURL: "http://localhost:8418", headers: { "Content-Type": "application/json", }, @@ -46,6 +47,7 @@ export const loginUser = async (userData) => { const response = await api.post(prefix + "/login", userData); const { token, ...rest } = response.data; localStorage.setItem("token", token); + localStorage.setItem("email", userData.email); return { token, ...rest }; } catch (error) { console.error("Error logging in", error);