From 5183c88d6595e31d7f87c605ad261786c0cbea40 Mon Sep 17 00:00:00 2001 From: callmestiles Date: Tue, 30 Jul 2024 16:04:39 +0300 Subject: [PATCH 1/6] fix: fix error on layout --- src/app/dashboard/(user-dashboard)/settings/layout.tsx | 4 +--- .../dashboard/(user-dashboard)/settings/notification/page.tsx | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/app/dashboard/(user-dashboard)/settings/layout.tsx b/src/app/dashboard/(user-dashboard)/settings/layout.tsx index acdd55aab..265eaa2fb 100644 --- a/src/app/dashboard/(user-dashboard)/settings/layout.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/layout.tsx @@ -9,9 +9,7 @@ const layout: FC = ({ children }) => { return (
-
- {children} -
+
{children}
); }; diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index 44a5baa95..c38db2cd2 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -25,7 +25,7 @@ const NotificationPage = () => { }; return ( -
+

Notification

From bf526d6174c3ba860a4248fbce295dde2611c50e Mon Sep 17 00:00:00 2001 From: callmestiles Date: Wed, 31 Jul 2024 11:31:24 +0300 Subject: [PATCH 2/6] fix: fix some errors in the text --- .../dashboard/(user-dashboard)/settings/notification/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index c38db2cd2..12fec3f80 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -56,7 +56,7 @@ const NotificationPage = () => { onToggle={handleToggleSwitch} /> Date: Wed, 31 Jul 2024 12:36:30 +0300 Subject: [PATCH 3/6] merge from upstream/dev --- .../settings/notification/page.tsx | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index 0e87189a5..c327e9bb3 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -9,6 +9,38 @@ import NotificationHeader from "./_components/header"; import { NotificationSwitchBox } from "./_components/notification-switch-box"; import { useNotificationStore } from "./action/notification-store"; +// import { notificationSettingsProperties } from "./types/notification-settings.types"; + +// Helper function to save notification settings +// const saveNotificationSettings = async ( +// settings: notificationSettingsProperties, +// ) => { +// const baseUrl = process.env.NEXT_PUBLIC_API_URL; +// const endpoint = "/api/v1/settings/notification-settings"; +// const url = `${baseUrl}${endpoint}`; + +// try { +// const response = await fetch(url, { +// method: "POST", +// headers: { +// "Content-Type": "application/json", +// }, +// body: JSON.stringify(settings), +// }); + +// if (!response.ok) { +// throw new Error("Failed to save settings"); +// } + +// const data = await response.json(); // Await the parsed JSON response +// // console.log("response:", data); +// return data; +// } catch (error) { +// console.error("Error saving settings:", error); +// throw error; +// } +// }; + const NotificationPage = () => { const { settings, updateSettings } = useNotificationStore(); const [isOpen, setOpen] = useState(false); @@ -17,8 +49,15 @@ const NotificationPage = () => { updateSettings({ [name]: !settings[name] }); }; - const handleSaveChanges = () => { - setOpen(true); + const handleSaveChanges = async () => { + try { + // await saveNotificationSettings(settings); + // console.log("Settings saved:", settings); + setOpen(true); + } catch { + // console.error("Failed to save settings:", error); + // Handle error, e.g., show a notification to the user + } }; return ( From c55eb88f07e10471fbb46b646aa25499808dcaca Mon Sep 17 00:00:00 2001 From: callmestiles Date: Wed, 31 Jul 2024 21:59:17 +0300 Subject: [PATCH 4/6] feat: start implementing the backend --- .../settings/notification/page.tsx | 96 ++++++++++++------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index c327e9bb3..5dd764d59 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -1,45 +1,53 @@ "use client"; import { Check, ChevronLeft } from "lucide-react"; -import { useState } from "react"; +import { getSession } from "next-auth/react"; +import { useState, useEffect +} from "react"; import CustomButton from "~/components/common/common-button/common-button"; import NotificationSettingSavedModal from "~/components/common/modals/notification-settings-saved"; import NotificationHeader from "./_components/header"; import { NotificationSwitchBox } from "./_components/notification-switch-box"; +import { getApiUrl } from "~/utils/getApiUrl"; +import { useToast } from "~/components/ui/use-toast"; import { useNotificationStore } from "./action/notification-store"; +import { notificationSettingsProperties } from "./types/notification-settings.types"; -// import { notificationSettingsProperties } from "./types/notification-settings.types"; - -// Helper function to save notification settings -// const saveNotificationSettings = async ( -// settings: notificationSettingsProperties, -// ) => { -// const baseUrl = process.env.NEXT_PUBLIC_API_URL; -// const endpoint = "/api/v1/settings/notification-settings"; -// const url = `${baseUrl}${endpoint}`; - -// try { -// const response = await fetch(url, { -// method: "POST", -// headers: { -// "Content-Type": "application/json", -// }, -// body: JSON.stringify(settings), -// }); - -// if (!response.ok) { -// throw new Error("Failed to save settings"); -// } - -// const data = await response.json(); // Await the parsed JSON response -// // console.log("response:", data); -// return data; -// } catch (error) { -// console.error("Error saving settings:", error); -// throw error; -// } -// }; + + + +const saveNotificationSettings = async ( + settings: notificationSettingsProperties, +) => { + const baseUrl = process.env.NEXT_PUBLIC_API_URL; + const endpoint = "/api/v1/settings/notification-settings"; + const url = `${baseUrl}${endpoint}`; + console.log(`url: ${url}`); + + try { + // const session = await getSession(); + // const token = session.access_token; + + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(settings), + }); + + if (!response.ok) { + throw new Error("Failed to save settings"); + } + + const data = await response.json(); + return data; + } catch (error) { + console.error("Error saving settings:", error); + throw error; + } +}; const NotificationPage = () => { const { settings, updateSettings } = useNotificationStore(); @@ -51,16 +59,28 @@ const NotificationPage = () => { const handleSaveChanges = async () => { try { - // await saveNotificationSettings(settings); - // console.log("Settings saved:", settings); + const session = await getSession(); + console.log("Session:", session); // Log the session object + await saveNotificationSettings(settings); setOpen(true); - } catch { - // console.error("Failed to save settings:", error); - // Handle error, e.g., show a notification to the user + } catch (error) { + console.log(error); } }; + // const handleSaveChanges = async () => { + // try { + // await saveNotificationSettings(settings); + // // console.log("Settings saved:", settings); + // setOpen(true); + // } catch { + // // console.error("Failed to save settings:", error); + // // Handle error, e.g., show a notification to the user + // } + // }; + return ( + <>
@@ -177,6 +197,8 @@ const NotificationPage = () => { onClose={() => setOpen(false)} />
+ + ); }; From 912debefa1982df8634891bc5147532cada75c76 Mon Sep 17 00:00:00 2001 From: callmestiles Date: Thu, 1 Aug 2024 03:13:47 +0300 Subject: [PATCH 5/6] feat: implement database --- .../settings/notification/page.tsx | 54 ++++++------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index 5dd764d59..e23ca57b2 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -2,50 +2,46 @@ import { Check, ChevronLeft } from "lucide-react"; import { getSession } from "next-auth/react"; -import { useState, useEffect -} from "react"; +import { useState } from "react"; +import axios from "axios"; import CustomButton from "~/components/common/common-button/common-button"; import NotificationSettingSavedModal from "~/components/common/modals/notification-settings-saved"; import NotificationHeader from "./_components/header"; import { NotificationSwitchBox } from "./_components/notification-switch-box"; -import { getApiUrl } from "~/utils/getApiUrl"; -import { useToast } from "~/components/ui/use-toast"; + import { useNotificationStore } from "./action/notification-store"; import { notificationSettingsProperties } from "./types/notification-settings.types"; - - - const saveNotificationSettings = async ( settings: notificationSettingsProperties, ) => { const baseUrl = process.env.NEXT_PUBLIC_API_URL; const endpoint = "/api/v1/settings/notification-settings"; const url = `${baseUrl}${endpoint}`; - console.log(`url: ${url}`); try { - // const session = await getSession(); - // const token = session.access_token; + const session = await getSession(); + const token = session?.user?.access_token; - const response = await fetch(url, { - method: "POST", + if (!token) { + throw new Error("Access token is undefined"); + } + + const response = await axios.post(url, settings, { headers: { "Content-Type": "application/json", + Authorization: `Bearer ${token}`, }, - body: JSON.stringify(settings), }); - if (!response.ok) { + if (response.status !== 200) { throw new Error("Failed to save settings"); } - - const data = await response.json(); + const data = response.data; return data; - } catch (error) { - console.error("Error saving settings:", error); - throw error; + } catch { + throw new Error(`Failed to save settings`); } }; @@ -59,28 +55,14 @@ const NotificationPage = () => { const handleSaveChanges = async () => { try { - const session = await getSession(); - console.log("Session:", session); // Log the session object await saveNotificationSettings(settings); setOpen(true); - } catch (error) { - console.log(error); + } catch { + throw new Error("Failed to save settings"); } }; - // const handleSaveChanges = async () => { - // try { - // await saveNotificationSettings(settings); - // // console.log("Settings saved:", settings); - // setOpen(true); - // } catch { - // // console.error("Failed to save settings:", error); - // // Handle error, e.g., show a notification to the user - // } - // }; - return ( - <>
@@ -197,8 +179,6 @@ const NotificationPage = () => { onClose={() => setOpen(false)} />
- - ); }; From 7696d98c1fcb102bbe9fb8b103dd072154d3a3cc Mon Sep 17 00:00:00 2001 From: callmestiles Date: Thu, 1 Aug 2024 11:18:29 +0300 Subject: [PATCH 6/6] fix: fix minor issues --- .../UnreadNotificationCard.tsx | 4 +- .../{action => _action}/notification-store.ts | 2 +- .../{action => _action}/notification.ts | 0 .../_components/notification-switch-box.tsx | 2 +- .../notification-settings.types.ts | 0 .../settings/notification/page.tsx | 80 +++++++++++-------- 6 files changed, 51 insertions(+), 37 deletions(-) rename src/app/dashboard/(user-dashboard)/settings/notification/{action => _action}/notification-store.ts (91%) rename src/app/dashboard/(user-dashboard)/settings/notification/{action => _action}/notification.ts (100%) rename src/app/dashboard/(user-dashboard)/settings/notification/{types => _types}/notification-settings.types.ts (100%) diff --git a/src/app/dashboard/(admin)/_components/unread-notification-card/UnreadNotificationCard.tsx b/src/app/dashboard/(admin)/_components/unread-notification-card/UnreadNotificationCard.tsx index f6454f204..ab078e562 100644 --- a/src/app/dashboard/(admin)/_components/unread-notification-card/UnreadNotificationCard.tsx +++ b/src/app/dashboard/(admin)/_components/unread-notification-card/UnreadNotificationCard.tsx @@ -3,8 +3,8 @@ import { BellRing } from "lucide-react"; import { FC } from "react"; -import { useNotificationStore } from "~/app/dashboard/(user-dashboard)/settings/notification/action/notification-store"; -import { notificationSettingsProperties } from "~/app/dashboard/(user-dashboard)/settings/notification/types/notification-settings.types"; +import { useNotificationStore } from "~/app/dashboard/(user-dashboard)/settings/notification/_action/notification-store"; +import { notificationSettingsProperties } from "~/app/dashboard/(user-dashboard)/settings/notification/_types/notification-settings.types"; import CustomButton from "~/components/common/common-button/common-button"; import { Card, diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/action/notification-store.ts b/src/app/dashboard/(user-dashboard)/settings/notification/_action/notification-store.ts similarity index 91% rename from src/app/dashboard/(user-dashboard)/settings/notification/action/notification-store.ts rename to src/app/dashboard/(user-dashboard)/settings/notification/_action/notification-store.ts index 2f6a8d1f4..8c84698e3 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/action/notification-store.ts +++ b/src/app/dashboard/(user-dashboard)/settings/notification/_action/notification-store.ts @@ -1,6 +1,6 @@ import create from "zustand"; -import { notificationSettingsProperties } from "../types/notification-settings.types"; +import { notificationSettingsProperties } from "../_types/notification-settings.types"; // Define the Zustand store interface NotificationStore { diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/action/notification.ts b/src/app/dashboard/(user-dashboard)/settings/notification/_action/notification.ts similarity index 100% rename from src/app/dashboard/(user-dashboard)/settings/notification/action/notification.ts rename to src/app/dashboard/(user-dashboard)/settings/notification/_action/notification.ts diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/_components/notification-switch-box.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/_components/notification-switch-box.tsx index 2e5c5de9d..4a7271842 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/_components/notification-switch-box.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/_components/notification-switch-box.tsx @@ -1,5 +1,5 @@ import { Switch } from "~/components/ui/switch"; -import { notificationSettingsProperties } from "../types/notification-settings.types"; +import { notificationSettingsProperties } from "../_types/notification-settings.types"; interface IProperties { title: string; diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/types/notification-settings.types.ts b/src/app/dashboard/(user-dashboard)/settings/notification/_types/notification-settings.types.ts similarity index 100% rename from src/app/dashboard/(user-dashboard)/settings/notification/types/notification-settings.types.ts rename to src/app/dashboard/(user-dashboard)/settings/notification/_types/notification-settings.types.ts diff --git a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx index e23ca57b2..9e9fa9ecc 100644 --- a/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx +++ b/src/app/dashboard/(user-dashboard)/settings/notification/page.tsx @@ -1,53 +1,67 @@ "use client"; +import axios from "axios"; import { Check, ChevronLeft } from "lucide-react"; import { getSession } from "next-auth/react"; import { useState } from "react"; -import axios from "axios"; import CustomButton from "~/components/common/common-button/common-button"; import NotificationSettingSavedModal from "~/components/common/modals/notification-settings-saved"; +import { useToast } from "~/components/ui/use-toast"; +import { getApiUrl } from "~/utils/getApiUrl"; +import { useNotificationStore } from "./_action/notification-store"; import NotificationHeader from "./_components/header"; import { NotificationSwitchBox } from "./_components/notification-switch-box"; +import { notificationSettingsProperties } from "./_types/notification-settings.types"; -import { useNotificationStore } from "./action/notification-store"; -import { notificationSettingsProperties } from "./types/notification-settings.types"; +const NotificationPage = () => { + const { settings, updateSettings } = useNotificationStore(); + const [isOpen, setOpen] = useState(false); + const { toast } = useToast(); -const saveNotificationSettings = async ( - settings: notificationSettingsProperties, -) => { - const baseUrl = process.env.NEXT_PUBLIC_API_URL; - const endpoint = "/api/v1/settings/notification-settings"; - const url = `${baseUrl}${endpoint}`; + const saveNotificationSettings = async ( + settings: notificationSettingsProperties, + ) => { + const baseUrl = await getApiUrl(); + const endpoint = "/api/v1/settings/notification-settings"; + const url = `${baseUrl}${endpoint}`; - try { - const session = await getSession(); - const token = session?.user?.access_token; + try { + const session = await getSession(); + const token = session?.user?.access_token; - if (!token) { - throw new Error("Access token is undefined"); - } + if (!token) { + toast({ + title: "Error", + description: "No access token", + variant: "destructive", + }); + } - const response = await axios.post(url, settings, { - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }, - }); + const response = await axios.post(url, settings, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); - if (response.status !== 200) { - throw new Error("Failed to save settings"); + if (response.status !== 200) { + toast({ + title: "Error", + description: "Request failed", + variant: "destructive", + }); + } + const data = response.data; + return data; + } catch { + toast({ + title: "Error", + description: "Failed to save settings", + variant: "destructive", + }); } - const data = response.data; - return data; - } catch { - throw new Error(`Failed to save settings`); - } -}; - -const NotificationPage = () => { - const { settings, updateSettings } = useNotificationStore(); - const [isOpen, setOpen] = useState(false); + }; const handleToggleSwitch = (name: keyof typeof settings) => { updateSettings({ [name]: !settings[name] });