summaryrefslogtreecommitdiff
path: root/src/pages/my/profile.js
blob: be1a67b0f8b84169b188b44d76725412664a2255 (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 { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import AppBar from "../../components/AppBar";
import Layout from "../../components/Layout";
import WithAuth from "../../components/WithAuth";
import apiOdoo from "../../helpers/apiOdoo";
import { 
  useAuth,
  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 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?.id && (
            <>
              <label className="form-label mt-4 mb-2">Email</label>
              <input 
                type="text"
                className="form-input bg-gray_r-2" 
                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 bg-gray_r-2" 
                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 bg-gray_r-2" 
                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 bg-gray_r-2" 
                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 bg-gray_r-2" 
                placeholder="••••••••"
                value={password}
                onChange={(e) => setPassword(e.target.value)}
                disabled={!editMode}
              />
            </>
          ) }

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

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