From ab39764b288b4d60923cc8cc6146ccdc1b4bfbac Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 11 Sep 2024 14:01:53 +0700
Subject: <iman> update switch acc

---
 src/lib/auth/components/SwitchAccount.jsx | 49 +++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 3 deletions(-)

(limited to 'src/lib/auth/components/SwitchAccount.jsx')

diff --git a/src/lib/auth/components/SwitchAccount.jsx b/src/lib/auth/components/SwitchAccount.jsx
index da0ff068..71571bd7 100644
--- a/src/lib/auth/components/SwitchAccount.jsx
+++ b/src/lib/auth/components/SwitchAccount.jsx
@@ -2,7 +2,7 @@ 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 { useEffect, useState, useMemo } from 'react';
 import { useForm } from 'react-hook-form';
 import { toast } from 'react-hot-toast';
 import editPersonalProfileApi from '../api/editPersonalProfileApi';
@@ -10,6 +10,9 @@ 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';
+import { registerUser } from '~/services/auth';
+import { useMutation } from 'react-query';
+import { isValid } from 'zod';
 const SwitchAccount = () => {
   const auth = useAuth();
   const [isOpen, setIsOpen] = useState(true);
@@ -27,10 +30,14 @@ const SwitchAccount = () => {
       password: '',
     },
   });
-
+  const mutation = useMutation({
+    mutationFn: (data) => registerUser(data),
+  });
+  const [notValid, setNotValid] = useState(false);
   const { form, isCheckedTNC, isValidCaptcha, errors, validate, updateForm } =
     useRegisterStore();
   console.log('form', form);
+  const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]);
   useEffect(() => {
     const loadProfile = async () => {
       const dataProfile = await addressApi({ id: auth.partnerId });
@@ -82,6 +89,28 @@ const SwitchAccount = () => {
       setIsPKP(false);
     }
   };
+
+  const onSubmitHandler = async (values) => {
+    let data = values;
+    console.log('data', data);
+    if (!isFormValid) {
+      setNotValid(true);
+      return;
+    } else {
+      setNotValid(false);
+    }
+    // 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 (
     <>
       <button
@@ -134,9 +163,23 @@ const SwitchAccount = () => {
               </Stack>
             </RadioGroup>
           </div>
-          <FormBisnis type='profil' required={isTerdaftar} isPKP={isPKP} />
+          <FormBisnis
+            type='profil'
+            required={isTerdaftar}
+            isPKP={isPKP}
+            chekValid={notValid}
+          />
         </div>
       )}
+      <div className='flex justify-end mb-4'>
+        <button
+          type='submit'
+          onClick={onSubmitHandler}
+          className='btn-yellow w-full sm:w-fit sm:ml-auto mt-6'
+        >
+          {mutation.isLoading ? 'Loading...' : 'Simpan Perubahan'}
+        </button>
+      </div>
     </>
   );
 };
-- 
cgit v1.2.3

