summaryrefslogtreecommitdiff
path: root/src/lib/auth/components/PersonalProfile.jsx
blob: 3053255da942b3197beba247108e2ed592a69f1b (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
import useAuth from '@/core/hooks/useAuth';
import { setAuth } from '@/core/utils/auth';
import addressApi from '@/lib/address/api/addressApi';
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import editPersonalProfileApi from '../api/editPersonalProfileApi';

const PersonalProfile = () => {
  const auth = useAuth();
  const [isOpen, setIsOpen] = useState(true);
  const toggle = () => setIsOpen(!isOpen);
  const { register, setValue, handleSubmit } = useForm({
    defaultValues: {
      email: '',
      name: '',
      mobile: '',
      password: '',
    },
  });

  useEffect(() => {
    const loadProfile = async () => {
      const dataProfile = await addressApi({
        id: auth.partnerId ? auth.partnerId : auth.partner_id,
      });
      setValue('email', dataProfile?.email);
      setValue('name', dataProfile?.name);
      setValue('mobile', dataProfile?.mobile);
    };
    if (auth) loadProfile();
  }, [auth, setValue]);

  const onSubmitHandler = async (values) => {
    let data = values;
    if (!values.password) delete data.password;
    const isUpdated = await editPersonalProfileApi({ data });

    if (isUpdated?.user) {
      setAuth(isUpdated.user);
      setValue('password', '');
      toast.success('Berhasil mengubah profil', { duration: 1500 });
      return;
    }
    toast.error('Terjadi kesalahan internal');
  };

  return (
    <>
      <div type='button' className='p-4 flex items-center text-left w-full'>
        <div>
          <div className='font-semibold mb-2'>Informasi Akun</div>
          <div className='text-gray_r-11'>
            Dibawah ini adalah data diri yang anda masukan, periksa kembali data
            diri anda
          </div>
        </div>
      </div>

      <form
        className='p-4 border-t border-gray_r-6'
        onSubmit={handleSubmit(onSubmitHandler)}
      >
        <div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
          <div>
            <label>Email</label>
            <input
              {...register('email')}
              type='text'
              disabled
              className='form-input mt-3'
            />
          </div>
          <div>
            <label>Nama Lengkap</label>
            <input
              {...register('name')}
              type='text'
              className='form-input mt-3'
            />
          </div>
          <div>
            <label>No. Handphone</label>
            <input
              {...register('mobile')}
              type='tel'
              className='form-input mt-3'
            />
          </div>
          <div>
            <label>Kata Sandi</label>
            <input
              {...register('password')}
              type='password'
              className='form-input mt-3'
              placeholder='Isi jika ingin mengubah kata sandi'
            />
          </div>
        </div>
        <button
          type='submit'
          className='btn-yellow w-full sm:w-fit sm:ml-auto mt-6 max-w-28'
        >
          Simpan
        </button>
      </form>
    </>
  );
};

export default PersonalProfile;