From f2dad674f9096ca2423cbbdc0228576f094f064c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 26 Dec 2022 12:01:19 +0700 Subject: Fitur Logout --- src/pages/my/menu.js | 2 +- src/pages/my/profile.js | 55 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 6 deletions(-) (limited to 'src/pages/my') diff --git a/src/pages/my/menu.js b/src/pages/my/menu.js index d86febf9..84de3892 100644 --- a/src/pages/my/menu.js +++ b/src/pages/my/menu.js @@ -35,7 +35,7 @@ const serviceMenus = [ const settingMenus = [ { icon: (),name: 'Daftar Alamat', url: '/my/profile' }, - { icon: (),name: 'Keluar Akun', url: '/my/profile' }, + { icon: (),name: 'Keluar Akun', url: '/logout' }, ]; export default function MyMenu() { diff --git a/src/pages/my/profile.js b/src/pages/my/profile.js index ebbd7f2a..f1d0a701 100644 --- a/src/pages/my/profile.js +++ b/src/pages/my/profile.js @@ -1,27 +1,32 @@ import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import AppBar from "../../components/AppBar"; -import Header from "../../components/Header"; import Layout from "../../components/Layout"; import WithAuth from "../../components/WithAuth"; import apiOdoo from "../../helpers/apiOdoo"; import { useAuth, - setAuth as setAuthCookie + setAuth as setAuthCookie, + getAuth } from "../../helpers/auth"; export default function MyProfile() { const [auth, setAuth] = useAuth(); + const [editMode, setEditMode] = useState(false); + const [password, setPassword] = useState(''); const update = async (e) => { e.preventDefault(); - let update = await apiOdoo('PUT', `/api/v1/user/${auth.id}`, { + let dataToUpdate = { name: auth.name, phone: auth.phone, mobile: auth.mobile, token: auth.token - }); + }; + if (password) dataToUpdate.password = password; + let update = await apiOdoo('PUT', `/api/v1/user/${auth.id}`, dataToUpdate); setAuthCookie(update.user); + cancelEdit(); toast.success('Berhasil mengubah profil', { duration: 1500 }); }; @@ -29,6 +34,12 @@ export default function MyProfile() { let authToUpdate = auth; authToUpdate[e.target.name] = e.target.value; setAuth({ ...authToUpdate }); + }; + + const cancelEdit = () => { + setEditMode(false); + setAuth(getAuth()); + setPassword(''); } return ( @@ -58,6 +69,7 @@ export default function MyProfile() { name="name" value={auth.name} onChange={handleInput} + disabled={!editMode} /> @@ -68,6 +80,7 @@ export default function MyProfile() { name="phone" value={auth.phone} onChange={handleInput} + disabled={!editMode} /> @@ -78,11 +91,43 @@ export default function MyProfile() { name="mobile" value={auth.mobile} onChange={handleInput} + disabled={!editMode} + /> + + + setPassword(e.target.value)} + disabled={!editMode} /> ) } - + { editMode && ( +
+ + +
+ ) } + + { !editMode && ( + + ) } -- cgit v1.2.3