From 7551b0b494774f9e96b9e09adcb865a2cd4a6980 Mon Sep 17 00:00:00 2001 From: headlesdev Date: Sat, 17 May 2025 21:39:10 +0200 Subject: [PATCH] Split Settings Component into 2 cards --- app/dashboard/settings/SettingsPage.tsx | 160 +++++++---------------- components/settings/PasswordSettings.tsx | 74 +++++++++++ components/settings/ProfileSettings.tsx | 86 ++++++++++++ 3 files changed, 208 insertions(+), 112 deletions(-) create mode 100644 components/settings/PasswordSettings.tsx create mode 100644 components/settings/ProfileSettings.tsx diff --git a/app/dashboard/settings/SettingsPage.tsx b/app/dashboard/settings/SettingsPage.tsx index e5a738e..3891db4 100644 --- a/app/dashboard/settings/SettingsPage.tsx +++ b/app/dashboard/settings/SettingsPage.tsx @@ -1,120 +1,56 @@ -"use client"; +// components/SettingsPage.tsx +'use client'; -import Sidebar from "@/components/Sidebar"; -import ErrorToast from "@/components/Error"; -import { useState } from "react"; -import axios from "axios"; -import Cookies from "js-cookie"; +import Sidebar from '@/components/Sidebar'; +import ErrorToast from '@/components/Error'; +import { ProfileSettings } from '@/components/settings/ProfileSettings'; +import { PasswordSettings } from '@/components/settings/PasswordSettings'; +import { useState } from 'react'; interface SettingsPageProps { - username: string; - name: string; - email: string; + username: string; + name: string; + email: string; } -export default function SettingsPage({ username, name, email }: SettingsPageProps) { - const [profileUsername, setProfileUsername] = useState(username); - const [profileName, setProfileName] = useState(name); - const [profileEmail, setProfileEmail] = useState(email); - const [oldPassword, setOldPassword] = useState(""); - const [password, setPassword] = useState(""); - const [passwordConfirm, setPasswordConfirm] = useState(""); +export default function SettingsPage({ username, name, email }: SettingsPageProps) { + const [error, setError] = useState(''); - const [error, setError] = useState(""); + return ( +
+ +
+

Settings

+

Manage your instance settings

- const saveProfile = async () => { - try { - const response = await axios.post("/api/user/change/profile", { token: Cookies.get("token"), username: profileUsername, name: profileName, email: profileEmail }); - if (response.data.message !== "Profile updated successfully") { - setError("Failed to update profile"); - } else { - window.location.reload(); - } - } catch (error: any) { - setError(error.response.data.error); - } - } - - const savePassword = async () => { - if (password !== passwordConfirm) { - setError("Passwords do not match"); - return; - } - if (oldPassword === password) { - setError("Old password and new password cannot be the same"); - return; - } - try { - const response = await axios.post("/api/user/change/password", { token: Cookies.get("token"), old_password: oldPassword, password: password }); - if (response.data.message !== "Password updated successfully") { - setError("Failed to update password"); - } - } catch (error: any) { - setError(error.response.data.error); - } - } - - return ( -
- -
-

Settings

-

Manage your instance settings

- -
- -
-
-
-
-

Profile Settings

-

Manage your profile settings

-
-
- - setProfileUsername(e.target.value)} className="input w-full" /> -
-
- - setProfileName(e.target.value)} className="input w-full" /> -
-
- - setProfileEmail(e.target.value)} className="input w-full" /> -
-
- -
-
-

Password Settings

-

Manage your password

-
-
- - setOldPassword(e.target.value)} className="input w-full" /> -
-
- - setPassword(e.target.value)} className="input w-full" /> -
-
- - setPasswordConfirm(e.target.value)} className="input w-full" /> -
-
- -
-
-
-
- -
-
- setError("")} /> -
- ); +
+ +
+
+
+ + +
+
+
+
+
+ setError('')} /> +
+ ); } \ No newline at end of file diff --git a/components/settings/PasswordSettings.tsx b/components/settings/PasswordSettings.tsx new file mode 100644 index 0000000..070e160 --- /dev/null +++ b/components/settings/PasswordSettings.tsx @@ -0,0 +1,74 @@ +'use client'; + +import { useState } from 'react'; +import axios from 'axios'; +import Cookies from 'js-cookie'; + +export const PasswordSettings = ({ onError }: { onError: (message: string) => void }) => { + const [oldPassword, setOldPassword] = useState(''); + const [password, setPassword] = useState(''); + const [passwordConfirm, setPasswordConfirm] = useState(''); + + const handleSave = async () => { + if (password !== passwordConfirm) return onError('Passwords do not match'); + if (oldPassword === password) return onError('New password must be different'); + + try { + const response = await axios.post('/api/user/change/password', { + token: Cookies.get('token'), + old_password: oldPassword, + password, + }); + + if (response.data.message !== 'Password updated successfully') { + onError('Failed to update password'); + } + } catch (error: any) { + onError(error.response?.data?.error || 'An error occurred'); + } + }; + + return ( +
+

Password Settings

+

Manage your password

+ +
+
+ + setOldPassword(e.target.value)} + className="input w-full" + /> +
+
+ + setPassword(e.target.value)} + className="input w-full" + /> +
+
+ + setPasswordConfirm(e.target.value)} + className="input w-full" + /> +
+
+ + +
+ ); +}; \ No newline at end of file diff --git a/components/settings/ProfileSettings.tsx b/components/settings/ProfileSettings.tsx new file mode 100644 index 0000000..2ae5d04 --- /dev/null +++ b/components/settings/ProfileSettings.tsx @@ -0,0 +1,86 @@ +'use client'; + +import { useState } from 'react'; +import axios from 'axios'; +import Cookies from 'js-cookie'; + +interface ProfileSettingsProps { + initialUsername: string; + initialName: string; + initialEmail: string; + onError: (message: string) => void; +} + +export const ProfileSettings = ({ + initialUsername, + initialName, + initialEmail, + onError, +}: ProfileSettingsProps) => { + const [username, setUsername] = useState(initialUsername); + const [name, setName] = useState(initialName); + const [email, setEmail] = useState(initialEmail); + + const handleSave = async () => { + try { + const response = await axios.post('/api/user/change/profile', { + token: Cookies.get('token'), + username, + name, + email, + }); + + if (response.data.message === 'Profile updated successfully') { + window.location.reload(); + } else { + onError('Failed to update profile'); + } + } catch (error: any) { + onError(error.response?.data?.error || 'An error occurred'); + } + }; + + return ( +
+

Profile Settings

+

Manage your profile settings

+ +
+
+ + setUsername(e.target.value)} + className="input w-full" + /> +
+
+ + setName(e.target.value)} + className="input w-full" + /> +
+
+ + setEmail(e.target.value)} + className="input w-full" + /> +
+
+ + +
+ ); +}; \ No newline at end of file