summaryrefslogtreecommitdiff
path: root/src/lib/auth/components/CompanyProfile.jsx
blob: d404ebe74df909e14dba984abea33cc412f2ef52 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import odooApi from '@/core/api/odooApi';
import HookFormSelect from '@/core/components/elements/Select/HookFormSelect';
import useAuth from '@/core/hooks/useAuth';
import addressApi from '@/lib/address/api/addressApi';
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
import { useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { toast } from 'react-hot-toast';
import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
import { yupResolver } from '@hookform/resolvers/yup';
import * as Yup from 'yup';
import SwitchAccount from '@/lib/auth/components/SwitchAccount';
import { Checkbox } from '@chakra-ui/react';

const CompanyProfile = () => {
  const [changeConfirmation, setChangeConfirmation] = useState(false);
  const [changeType, setChangeType] = useState(false);
  const [isChecked, setIsChecked] = useState(false);
  const [company_type, setCompany_type] = useState('nonpkp');
  const auth = useAuth();
  const [isOpen, setIsOpen] = useState(false);
  const toggle = () => setIsOpen(!isOpen);
  const {
    register,
    formState: { errors },
    setValue,
    control,
    handleSubmit,
  } = useForm({
    resolver: yupResolver(validationSchema),
    defaultValues,
  });

  const [industries, setIndustries] = useState([]);
  useEffect(() => {
    const loadIndustries = async () => {
      const dataIndustries = await odooApi('GET', '/api/v1/partner/industry');
      setIndustries(
        dataIndustries?.map((o) => ({ value: o.id, label: o.name }))
      );
    };
    loadIndustries();
  }, []);

  const [companyTypes, setCompanyTypes] = useState([]);
  useEffect(() => {
    const loadCompanyTypes = async () => {
      const dataCompanyTypes = await odooApi(
        'GET',
        '/api/v1/partner/company_type'
      );
      setCompanyTypes(
        dataCompanyTypes?.map((o) => ({ value: o.id, label: o.name }))
      );
    };
    loadCompanyTypes();
  }, []);

  useEffect(() => {
    const loadProfile = async () => {
      const dataProfile = await addressApi({
        id: auth?.company
          ? auth.parentId
            ? auth.parentId
            : auth.partnerId
          : auth.partnerId,
      });
      setCompany_type(dataProfile?.companyType);
      setValue('name', dataProfile?.name);
      setValue('industry', dataProfile?.industryId);
      setValue('companyType', dataProfile?.companyTypeId);
      setValue('taxName', dataProfile?.taxName);
      setValue('npwp', dataProfile?.npwp);
      setValue('alamat_wajib_pajak', dataProfile?.alamatWajibPajak);
      setValue('alamat_bisnis', dataProfile?.alamatBisnis);
      setValue('company_type', dataProfile?.companyType);
      setValue('email_bisnis', dataProfile.email);
      setValue('mobile_bisnis', dataProfile.mobile);
    };
    if (auth) loadProfile();
  }, [auth, setValue]);

  const onSubmitHandler = async (values) => {
    if (changeConfirmation) {
      const data = {
        ...values,
        id_user: auth.partnerId,
        company_type_id: values.companyType,
        industry_id: values.industry,
        tax_name: values.taxName,
        alamat_lengkap_text: values.alamat_wajib_pajak,
        street: values.alamat_bisnis,
        email: values.email_bisnis,
        mobile: values.mobile_bisnis,
      };
      const isUpdated = await odooApi(
        'PUT',
        `/api/v1/partner/${auth.parentId}`,
        data
      );
      if (isUpdated?.id) {
        toast.success('Berhasil mengubah profil', { duration: 1500 });
        return;
      }
      toast.error('Terjadi kesalahan internal');
    }
  };

  const handleConfirmSubmit = () => {
    setChangeConfirmation(false);
    handleSubmit(onSubmitHandler)();
  };
  const handleConfirmSubmitType = () => {
    setChangeType(false);
    setIsChecked(true);
    setIsOpen(!isOpen);
  };
  const handleChange = async () => {
    if (isChecked) {
      setIsChecked(!isChecked);
      setIsOpen(!isOpen);
    } else {
      setIsChecked(!isChecked);
      setChangeType(true);
    }
  };

  return (
    <>
      <BottomPopup
        active={changeType}
        close={() => setChangeType(false)} // Menutup popup
        title='Ubah type akun'
      >
        <div className='leading-7 text-gray_r-12/80'>
          Anda akan mengubah type akun anda?
        </div>
        <div className='flex mt-6 gap-x-4 md:justify-end'>
          <button
            className='btn-solid-red flex-1 md:flex-none'
            type='button'
            onClick={handleConfirmSubmitType}
          >
            Yakin
          </button>
          <button
            className='btn-light flex-1 md:flex-none'
            type='button'
            onClick={() => setChangeType(false)}
          >
            Batal
          </button>
        </div>
      </BottomPopup>
      <BottomPopup
        active={changeConfirmation}
        close={() => setChangeConfirmation(true)}
        title='Ubah profil Bisnis'
      >
        <div className='leading-7 text-gray_r-12/80'>
          Apakah anda yakin mengubah data bisnis?
        </div>
        <div className='flex mt-6 gap-x-4 md:justify-end'>
          <button
            className='btn-solid-red flex-1 md:flex-none'
            type='button'
            onClick={handleConfirmSubmit}
          >
            Ya, Ubah
          </button>
          <button
            className='btn-light flex-1 md:flex-none'
            type='button'
            onClick={() => setChangeConfirmation(false)}
          >
            Batal
          </button>
        </div>
      </BottomPopup>
      <div className='p-4 flex-row items-center text-left w-full'>
        {company_type === 'nonpkp' && (
          <div className='text-sm mb-2 flex items-center'>
            <Checkbox
              borderColor='gray.600'
              colorScheme='red'
              size='lg'
              isChecked={isChecked}
              onChange={handleChange}
            />
            <p className='ml-2'>Ubah ke akun PKP</p>
          </div>
        )}
        <div>
          <div className='font-semibold mb-2 flex flex-row gap-x-2'>
            <h2>Informasi Usaha</h2>
            <div className='badge-red'>{company_type.toUpperCase()}</div>
          </div>
          <div className='text-gray_r-11'>
            Dibawah ini adalah data usaha yang anda masukkan, periksa kembali
            data usaha anda.
          </div>
        </div>
        {/* <button
          onClick={toggle}
          className='btn-yellow w-full sm:w-fit sm:ml-auto min-w-[92px]'
        >
          Ubah
        </button> */}
      </div>
      {!isOpen && (
        <form
          className='p-4 border-t border-gray_r-6'
          onSubmit={(e) => {
            e.preventDefault();
            setChangeConfirmation(true);
          }}
        >
          <div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
            <div>
              <label className='block mb-3'>Klasifikasi Jenis Usaha</label>
              <Controller
                name='industry'
                control={control}
                render={(props) => (
                  <HookFormSelect {...props} options={industries} />
                )}
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.industry?.message}
              </div>
            </div>
            <div className='flex flex-wrap'>
              <div className='w-full mb-3'>Badan Usaha</div>
              <div className='w-3/12 pr-1'>
                <Controller
                  name='companyType'
                  control={control}
                  render={(props) => (
                    <HookFormSelect {...props} options={companyTypes} />
                  )}
                />
                <div className='text-caption-2 text-danger-500 mt-1'>
                  {errors.companyType?.message}
                </div>
              </div>
              <div className='w-9/12 pl-1'>
                <input
                  {...register('name')}
                  type='text'
                  className='form-input'
                  placeholder='Cth: Indoteknik Dotcom Gemilang'
                />
                <div className='text-caption-2 text-danger-500 mt-1'>
                  {errors.name?.message}
                </div>
              </div>
            </div>
            <div>
              <label>Nama Wajib Pajak</label>
              <input
                {...register('taxName')}
                type='text'
                className='form-input mt-3'
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.taxName?.message}
              </div>
            </div>
            <div>
              <label>Email Bisnis</label>
              <input
                {...register('email_bisnis')}
                type='email'
                className='form-input mt-3'
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.email_bisnis?.message}
              </div>
            </div>
            <div>
              <label>No. Handphone Bisnis</label>
              <input
                {...register('mobile_bisnis')}
                type='tel'
                className='form-input mt-3'
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.mobile_bisnis?.message}
              </div>
            </div>
            <div>
              <label>Alamat Wajib Pajak</label>
              <input
                {...register('alamat_wajib_pajak')}
                type='text'
                className='form-input mt-3'
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.alamat_wajib_pajak?.message}
              </div>
            </div>
            <div>
              <label>Alamat Bisnis</label>
              <input
                {...register('alamat_bisnis')}
                type='text'
                className='form-input mt-3'
              />
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.alamat_bisnis?.message}
              </div>
            </div>
            <div>
              <label>Nomor NPWP</label>
              <input
                {...register('npwp')}
                type='text'
                disabled
                className='form-input mt-3'
                maxLength={16}
              />
              <span className='text-xs opacity-60 text-red-500'>
                *Untuk mengganti NPWP bisa menghubungi kami{' '}
                <a
                  href='https://wa.me/6281717181922' target='_blank' rel='noreferrer'
                  style={{ textDecoration: 'underline' }}
                >
                  disini.
                </a>
              </span>
              <div className='text-caption-2 text-danger-500 mt-1'>
                {errors.npwp?.message}
              </div>
            </div>
          </div>
          <button type='submit' className='btn-yellow w-full mt-6'>
            Simpan
          </button>
        </form>
      )}
      {isOpen && <SwitchAccount company_type={company_type} />}
    </>
  );
};

export default CompanyProfile;

const validationSchema = Yup.object().shape({
  alamat_bisnis: Yup.string().required('Harus di-isi'),
  name: Yup.string().required('Harus di-isi'),
  email_bisnis: Yup.string().required('Harus di-isi'),
  mobile_bisnis: Yup.string().required('Harus di-isi'),
  industry: Yup.string().required('Harus di-pilih'),
  companyType: Yup.string().required('Harus di-pilih'),
  taxName: Yup.string(),
  npwp: Yup.string(),
  alamat_wajib_pajak: Yup.string(),
  company_type: Yup.string(),
  taxName: Yup.string().when('company_type', {
    is: (company_type) => company_type !== 'Non PKP',
    then: Yup.string().required('Harus di-isi'),
    otherwise: Yup.string().notRequired(),
  }),
  npwp: Yup.string().when('company_type', {
    is: (company_type) => company_type !== 'Non PKP',
    then: Yup.string().required('Harus di-isi'),
    otherwise: Yup.string().notRequired(),
  }),
  alamat_wajib_pajak: Yup.string().when('company_type', {
    is: (company_type) => company_type !== 'Non PKP',
    then: Yup.string().required('Harus di-isi'),
    otherwise: Yup.string().notRequired(),
  }),
});

const defaultValues = {
  industry: '',
  companyType: '',
  name: '',
  email_bisnis: '',
  mobile_bisnis: '',
  taxName: '',
  npwp: '',
  alamat_wajib_pajak: '',
  alamat_bisnis: '',
};