import { useState } from "react"; import { toast } from "react-hot-toast"; import AppBar from "@/components/layouts/AppBar"; import Layout from "@/components/layouts/Layout"; import WithAuth from "@/components/auth/WithAuth"; import apiOdoo from "@/core/utils/apiOdoo"; import { useAuth, setAuth as setAuthCookie, getAuth } from "@/core/utils/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 dataToUpdate = { name: auth.name, phone: auth.phone, mobile: auth.mobile }; 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 }); }; const handleInput = (e) => { let authToUpdate = auth; authToUpdate[e.target.name] = e.target.value; setAuth({ ...authToUpdate }); }; const cancelEdit = () => { setEditMode(false); setAuth(getAuth()); setPassword(''); } return (
{ auth && ( <> setPassword(e.target.value)} disabled={!editMode} /> ) } { editMode && (
) } { !editMode && ( ) }
); }