diff --git a/src/components/Setting/ChangePassword.astro b/src/components/Setting/ChangePassword.astro deleted file mode 100644 index 2597dc488..000000000 --- a/src/components/Setting/ChangePassword.astro +++ /dev/null @@ -1,53 +0,0 @@ -
-

Password

-

Manage settings for your account passwords

-
-
- - -
-
- - -
-
- - -
- - -
-
diff --git a/src/components/Setting/ChangePasswordForm.tsx b/src/components/Setting/ChangePasswordForm.tsx index 405ba51cf..14fd22fdc 100644 --- a/src/components/Setting/ChangePasswordForm.tsx +++ b/src/components/Setting/ChangePasswordForm.tsx @@ -143,8 +143,9 @@ export default function ChangePasswordForm() { )} - - diff --git a/src/components/Setting/UpdateProfileForm.tsx b/src/components/Setting/UpdateProfileForm.tsx index 5b7d57d4c..7d6fcbb4c 100644 --- a/src/components/Setting/UpdateProfileForm.tsx +++ b/src/components/Setting/UpdateProfileForm.tsx @@ -1,4 +1,6 @@ import { useState } from 'preact/hooks'; +import { TOKEN_COOKIE_NAME } from '../../lib/utils'; +import Cookies from 'js-cookie'; export default function UpdateProfileForm() { const [name, setName] = useState(''); @@ -13,23 +15,49 @@ export default function UpdateProfileForm() { e.preventDefault(); setIsLoading(true); + const headers = new Headers(); + headers.append('Content-Type', 'application/json'); + headers.append( + 'Cookie', + `${TOKEN_COOKIE_NAME}=${Cookies.get(TOKEN_COOKIE_NAME)}` + ); + fetch('http://localhost:8080/v1-update-profile', { method: 'POST', credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, + headers, body: JSON.stringify({ name, github, linkedin, website, }), - }); + }) + .then(async (res) => { + const json = await res.json(); + if (res.ok) { + return json; + } else { + throw new Error(json.message); + } + }) + .then((data) => { + setIsLoading(false); + setName(''); + setGithub(''); + setLinkedin(''); + setWebsite(''); + setError(''); + setSuccessMessage('Profile updated successfully'); + }) + .catch((err) => { + setIsLoading(false); + setError(err.message); + }); }; return ( -
+

Update Profile

Manage settings for your roadmap.sh profile

@@ -47,6 +75,8 @@ export default function UpdateProfileForm() { className="mt-2 block w-full appearance-none rounded-lg border border-gray-300 px-3 py-2 shadow-sm outline-none transition duration-150 ease-in-out placeholder:text-gray-400 focus:ring-2 focus:ring-black focus:ring-offset-1" required placeholder="Arik Chakma" + value={name} + onChange={(e) => setName((e.target as HTMLInputElement).value)} />
@@ -69,19 +99,21 @@ export default function UpdateProfileForm() {
setGithub((e.target as HTMLInputElement).value)} />
setLinkedin((e.target as HTMLInputElement).value)} />
@@ -102,14 +136,50 @@ export default function UpdateProfileForm() { id="website" className="mt-2 block w-full appearance-none rounded-lg border border-gray-300 px-3 py-2 shadow-sm outline-none transition duration-150 ease-in-out placeholder:text-gray-400 focus:ring-2 focus:ring-black focus:ring-offset-1" placeholder="https://arikko.dev" + value={website} + onChange={(e) => setWebsite((e.target as HTMLInputElement).value)} />
+ {error && ( +
+ {error} +
+ )} + {successMessage && ( +
+ {successMessage} +
+ )} +
diff --git a/src/pages/settings/profile.astro b/src/pages/settings/profile.astro index 8c525e133..29f30f89f 100644 --- a/src/pages/settings/profile.astro +++ b/src/pages/settings/profile.astro @@ -1,11 +1,11 @@ --- import SettingSidebar from '../../components/Setting/SettingSidebar.astro'; -import UpdateProfile from '../../components/Setting/UpdateProfile.astro'; +import UpdateProfileForm from '../../components/Setting/UpdateProfileForm'; import SettingLayout from '../../layouts/SettingLayout.astro'; --- - +