summaryrefslogtreecommitdiff
path: root/src2/pages/my/profile.js
blob: 9789125902ebf434464e926a8334a6c88da9f664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 (
    <WithAuth>
      <Layout>
        <AppBar title="Akun Saya" />
        
        <form onSubmit={update} className="w-full px-4">
          { auth && (
            <>
              <label className="form-label mt-4 mb-2">Email</label>
              <input 
                type="text"
                className="form-input" 
                placeholder="johndoe@gmail.com"
                name="email"
                value={auth.email}
                onChange={handleInput}
                disabled={true}
              />

              <label className="form-label mt-4 mb-2">Nama Lengkap</label>
              <input 
                type="text"
                className="form-input" 
                placeholder="John Doe"
                name="name"
                value={auth.name}
                onChange={handleInput}
                disabled={!editMode}
              />

              <label className="form-label mt-4 mb-2">No Telepon</label>
              <input 
                type="tel"
                className="form-input" 
                placeholder="08xxxxxxxx"
                name="phone"
                value={auth.phone}
                onChange={handleInput}
                disabled={!editMode}
              />

              <label className="form-label mt-4 mb-2">No Handphone</label>
              <input 
                type="tel"
                className="form-input" 
                placeholder="08xxxxxxxx"
                name="mobile"
                value={auth.mobile}
                onChange={handleInput}
                disabled={!editMode}
              />

              <label className="form-label mt-4 mb-2">Kata Sandi</label>
              <input 
                type="password"
                className="form-input" 
                placeholder="••••••••"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                disabled={!editMode}
              />
            </>
          ) }

          { editMode && (
            <div className="flex gap-x-3 mt-6">
              <button 
                type="button" 
                className="btn-light flex-1 float-right"
                onClick={cancelEdit}
              >
                Batal
              </button>
              <button type="submit" className="btn-yellow flex-1 float-right">Simpan</button>
            </div>
          ) }

          { !editMode && (
            <button 
              type="button" 
              className="btn-light float-right mt-6 w-full"
              onClick={() => setEditMode(true)}
            >
              Ubah Profil
            </button>
          ) }
        </form>
      </Layout>
    </WithAuth>
  );
}