From c6345c34f61bc955a279d557526e43faef996aca Mon Sep 17 00:00:00 2001 From: Anand Suthar Date: Thu, 11 Jul 2024 17:03:10 +0530 Subject: [PATCH] refcator : added helper methods for updating profile --- app/lib/update-profile/index.ts | 7 ++++++ app/lib/update-profile/update-address.tsx | 25 +++++++++++++++++++ app/lib/update-profile/update-personal.tsx | 25 +++++++++++++++++++ app/lib/update-profile/update-security.tsx | 28 ++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 app/lib/update-profile/index.ts create mode 100644 app/lib/update-profile/update-address.tsx create mode 100644 app/lib/update-profile/update-personal.tsx create mode 100644 app/lib/update-profile/update-security.tsx diff --git a/app/lib/update-profile/index.ts b/app/lib/update-profile/index.ts new file mode 100644 index 0000000..afd7bf0 --- /dev/null +++ b/app/lib/update-profile/index.ts @@ -0,0 +1,7 @@ +"use server"; + +import updateAddress from "./update-address"; +import updatePersonal from "./update-personal"; +import updateSecurity from "./update-security"; + +export { updateAddress, updatePersonal, updateSecurity }; diff --git a/app/lib/update-profile/update-address.tsx b/app/lib/update-profile/update-address.tsx new file mode 100644 index 0000000..186f9e0 --- /dev/null +++ b/app/lib/update-profile/update-address.tsx @@ -0,0 +1,25 @@ +import getBaseUrl from "@utils/getBaseUrl"; +import { getSessionToken } from "../sessions/sessionUtils"; + +export default async function updateAddress(filteredFields: any) { + const session = getSessionToken(); + const serverUrl = getBaseUrl(); + + const headers = { + Authorization: `Bearer ${session}`, + }; + + try { + const response = await fetch(`${serverUrl}/api/update-profile/address`, { + method: "PUT", + headers, + body: JSON.stringify(filteredFields), + }); + + const success = await response.json(); + + return success; + } catch (error) { + console.error("Error updating address information:", error); + } +} diff --git a/app/lib/update-profile/update-personal.tsx b/app/lib/update-profile/update-personal.tsx new file mode 100644 index 0000000..81cb87f --- /dev/null +++ b/app/lib/update-profile/update-personal.tsx @@ -0,0 +1,25 @@ +import getBaseUrl from "@utils/getBaseUrl"; +import { getSessionToken } from "../sessions/sessionUtils"; + +export default async function updatePersonal(filteredFields: any) { + const session = getSessionToken(); + const serverUrl = getBaseUrl(); + + const headers = { + Authorization: `Bearer ${session}`, + }; + + try { + const response = await fetch(`${serverUrl}/api/update-profile/personal`, { + method: "PUT", + headers, + body: JSON.stringify(filteredFields), + }); + + const success = await response.json(); + + return success; + } catch (error) { + console.error("Error updating personal information:", error); + } +} diff --git a/app/lib/update-profile/update-security.tsx b/app/lib/update-profile/update-security.tsx new file mode 100644 index 0000000..dfc3314 --- /dev/null +++ b/app/lib/update-profile/update-security.tsx @@ -0,0 +1,28 @@ +import getBaseUrl from "@utils/getBaseUrl"; +import { getSessionToken } from "../sessions/sessionUtils"; + +export default async function updateSecurity(password: string) { + const session = getSessionToken(); + const serverUrl = getBaseUrl(); + + const headers = { + Authorization: `Bearer ${session}`, + }; + + try { + const response = await fetch( + `${serverUrl}/api/update-profile/reset-password`, + { + method: "PUT", + headers, + body: JSON.stringify({ password }), + } + ); + + const success = await response.json(); + + return success; + } catch (error) { + console.error("Error updating password :", error); + } +}