summaryrefslogtreecommitdiff
path: root/src/lib/auth/components/SwitchAccount.jsx
blob: da0ff068a5983e22d619979a7356dbff3407610f (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
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';
import FormBisnis from '~/modules/register/components/FormBisnis.tsx';
import RegistrasiBisnis from '~/modules/register/components/RegistrasiBisnis.tsx';
import { Radio, RadioGroup, Stack, Divider, Button } from '@chakra-ui/react';
import { useRegisterStore } from '~/modules/register/stores/useRegisterStore.ts';
const SwitchAccount = () => {
  const auth = useAuth();
  const [isOpen, setIsOpen] = useState(true);
  const toggle = () => setIsOpen(!isOpen);
  const [isPKP, setIsPKP] = useState(true);
  const [isTerdaftar, setIsTerdaftar] = useState(false);
  const [isChecked, setIsChecked] = useState(false);
  const [selectedValueBisnis, setSelectedValueBisnis] = useState('false');
  const [selectedValue, setSelectedValue] = useState('PKP');
  const { register, setValue, handleSubmit } = useForm({
    defaultValues: {
      email: '',
      name: '',
      phone: '',
      password: '',
    },
  });

  const { form, isCheckedTNC, isValidCaptcha, errors, validate, updateForm } =
    useRegisterStore();
  console.log('form', form);
  useEffect(() => {
    const loadProfile = async () => {
      const dataProfile = await addressApi({ id: auth.partnerId });
      setValue('email', dataProfile?.email);
      setValue('name', dataProfile?.name);
      setValue('phone', dataProfile?.phone);
    };
    if (auth) loadProfile();
  }, [auth, setValue]);

  useEffect(() => {
    if (selectedValue === 'PKP') {
      updateForm('is_pkp', 'true');
      validate();
    } else {
      updateForm('is_pkp', 'false');
      validate();
    }
  }, [selectedValue]);

  useEffect(() => {
    if (isTerdaftar) {
      updateForm('is_terdaftar', 'true');
      validate();
    } else {
      updateForm('is_terdaftar', 'false');
      validate();
    }
  }, [isTerdaftar]);

  const handleChangeBisnis = (value) => {
    setSelectedValueBisnis(value);
    if (value === 'true') {
      validate();
      setIsTerdaftar(true);
    } else {
      validate();
      setIsTerdaftar(false);
    }
  };
  const handleChange = (value) => {
    setSelectedValue(value);
    if (value === 'PKP') {
      validate();
      setIsPKP(true);
    } else {
      validate();
      setIsPKP(false);
      setIsPKP(false);
    }
  };
  return (
    <>
      <button
        type='button'
        onClick={toggle}
        className='p-4 flex items-center text-left w-full'
      >
        <div className='flex flex-row items-center bg-slate-50'>
          <div className='flex font-semibold mr-2'>Informasi Bisnis</div>
          <div className='text-red-500 text-xs'>
            *Perubahan akun tidak dapat diubah kembali
          </div>
        </div>
        <div className='ml-auto p-2 bg-gray_r-3 rounded'>
          {!isOpen && <ChevronDownIcon className='w-6' />}
          {isOpen && <ChevronUpIcon className='w-6' />}
        </div>
      </button>

      {isOpen && (
        <div className='p-4'>
          <div>
            <p className='text-black font-bold mb-2'>
              Bisnis Terdaftar di Indoteknik?
            </p>
            <RadioGroup
              onChange={handleChangeBisnis}
              value={selectedValueBisnis}
            >
              <Stack direction='row'>
                <Radio colorScheme='red' value='true'>
                  Sudah Terdaftar
                </Radio>
                <Radio colorScheme='red' value='false' className='ml-2'>
                  Belum Terdaftar
                </Radio>
              </Stack>
            </RadioGroup>
          </div>
          <div className='mt-4'>
            <p className='text-black font-bold mb-2'>Tipe Bisnis</p>
            <RadioGroup onChange={handleChange} value={selectedValue}>
              <Stack direction='row' className='font-bold'>
                <Radio colorScheme='red' value='PKP'>
                  PKP
                </Radio>
                <Radio colorScheme='red' value='Non-PKP' className='ml-4'>
                  Non-PKP
                </Radio>
              </Stack>
            </RadioGroup>
          </div>
          <FormBisnis type='profil' required={isTerdaftar} isPKP={isPKP} />
        </div>
      )}
    </>
  );
};

export default SwitchAccount;