From 0e1f33736095b8b460acb84f50d690dc4c619761 Mon Sep 17 00:00:00 2001 From: Marcin-Ramotowski Date: Sun, 13 Apr 2025 18:38:23 +0200 Subject: [PATCH] Added change password view --- frontend/src/App.tsx | 4 +- frontend/src/pages/ChangePassword.tsx | 64 +++++++++++++++++++++++++++ frontend/src/pages/Profile.tsx | 2 +- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 frontend/src/pages/ChangePassword.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 1176c85..09e9c1e 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -2,6 +2,7 @@ import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; import Login from "./pages/Login"; import Tasks from "./pages/Tasks"; import Profile from "./pages/Profile"; +import ChangePassword from "./pages/ChangePassword"; const App = () => { return ( @@ -9,7 +10,8 @@ const App = () => { } /> } /> - } /> + } /> + } /> ); diff --git a/frontend/src/pages/ChangePassword.tsx b/frontend/src/pages/ChangePassword.tsx new file mode 100644 index 0000000..76631bb --- /dev/null +++ b/frontend/src/pages/ChangePassword.tsx @@ -0,0 +1,64 @@ +import { useState } from "react"; +import { useNavigate } from "react-router-dom"; +import api from "../api/api"; +import Cookies from "js-cookie" + +const ChangePassword = () => { + const id = Cookies.get("user_id") + const navigate = useNavigate(); + if (!id) { + navigate("/login") + } + const [newPassword, setNewPassword] = useState(""); + const [error, setError] = useState(""); + const [success, setSuccess] = useState(""); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setError(""); + setSuccess(""); + + try { + await api.patch(`/users/${id}`, { + password: newPassword, + }); + + setSuccess("Hasło zostało zmienione."); + setNewPassword(""); + + setTimeout(() => navigate("/profile"), 2000); // krótka pauza, żeby pokazać sukces + } catch (err: any) { + console.error(err); + setError(err.response?.data?.message || "Błąd zmiany hasła."); + } + }; + + return ( +
+

Zmień hasło

+ +
+ setNewPassword(e.target.value)} + required + className="border p-2 rounded" + /> + + + + {error &&

{error}

} + {success &&

{success}

} +
+
+ ); +}; + +export default ChangePassword; diff --git a/frontend/src/pages/Profile.tsx b/frontend/src/pages/Profile.tsx index 50e2fa4..7b42f75 100644 --- a/frontend/src/pages/Profile.tsx +++ b/frontend/src/pages/Profile.tsx @@ -57,7 +57,7 @@ const Profile = () => {