From f5477dc98ce5da703ff27b4a05d5ba59e4c5298e Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 26 Aug 2024 13:19:23 +0700 Subject: change v1 to v2 API --- src/lib/auth/api/registerApi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib') diff --git a/src/lib/auth/api/registerApi.js b/src/lib/auth/api/registerApi.js index f9413f8c..580960d4 100644 --- a/src/lib/auth/api/registerApi.js +++ b/src/lib/auth/api/registerApi.js @@ -1,7 +1,7 @@ import odooApi from '@/core/api/odooApi' const registerApi = async ({ ...data }) => { - const dataRegister = await odooApi('POST', '/api/v1/user/register', data) + const dataRegister = await odooApi('POST', '/api/v2/user/register', data) return dataRegister } -- cgit v1.2.3 From 6e02e43d6df8b73a61ab59502abe941aeb83f799 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Mon, 26 Aug 2024 14:32:45 +0700 Subject: change v2 to v1 api --- src/lib/auth/api/registerApi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib') diff --git a/src/lib/auth/api/registerApi.js b/src/lib/auth/api/registerApi.js index 580960d4..f9413f8c 100644 --- a/src/lib/auth/api/registerApi.js +++ b/src/lib/auth/api/registerApi.js @@ -1,7 +1,7 @@ import odooApi from '@/core/api/odooApi' const registerApi = async ({ ...data }) => { - const dataRegister = await odooApi('POST', '/api/v2/user/register', data) + const dataRegister = await odooApi('POST', '/api/v1/user/register', data) return dataRegister } -- cgit v1.2.3 From 555de668c83b44679af9ef34f3624a55b372cfce Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 4 Sep 2024 16:07:38 +0700 Subject: update new register edit data --- src/lib/auth/components/CompanyProfile.jsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 2faede9b..a4c25e40 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -20,6 +20,7 @@ const CompanyProfile = () => { npwp: '' } }) + console.log("auth",auth) const [industries, setIndustries] = useState([]) useEffect(() => { @@ -42,11 +43,13 @@ const CompanyProfile = () => { useEffect(() => { const loadProfile = async () => { const dataProfile = await addressApi({ id: auth.parentId }) + console.log("dataProfile",dataProfile) 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) } if (auth) loadProfile() }, [auth, setValue]) @@ -54,9 +57,11 @@ const CompanyProfile = () => { const onSubmitHandler = async (values) => { const data = { ...values, + id_user:auth.partnerId, company_type_id: values.companyType, industry_id: values.industry, - tax_name: values.taxName + tax_name: values.taxName, + alamat_lengkap_text:values.alamat_wajib_pajak } const isUpdated = await odooApi('PUT', `/api/v1/partner/${auth.parentId}`, data) if (isUpdated?.id) { @@ -117,6 +122,9 @@ const CompanyProfile = () => {
+
+ +
+ + + {isOpen && ( -
+ { + e.preventDefault() + setChangeConfirmation(true) + }}>
-- cgit v1.2.3 From 4d9200650d232e4302f7e01df96751c3f9412693 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 6 Sep 2024 16:20:51 +0700 Subject: update data api --- src/lib/auth/components/CompanyProfile.jsx | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 581c32b0..e37decc9 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -59,6 +59,7 @@ const CompanyProfile = () => { if(changeConfirmation){ const data = { ...values, + id_user:auth.partnerId, company_type_id: values.companyType, industry_id: values.industry, tax_name: values.taxName, -- cgit v1.2.3 From 499954a8be814850103eece2dbd0306d6246989d Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 12 Sep 2024 15:12:42 +0700 Subject: update new register --- src/lib/auth/components/CompanyProfile.jsx | 179 ++++++++++++++++++----------- 1 file changed, 113 insertions(+), 66 deletions(-) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index e37decc9..fc3d149b 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -1,18 +1,18 @@ -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 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'; const CompanyProfile = () => { - const [changeConfirmation, setChangeConfirmation] = useState(false) - const auth = useAuth() - const [isOpen, setIsOpen] = useState(false) - const toggle = () => setIsOpen(!isOpen) + const [changeConfirmation, setChangeConfirmation] = useState(false); + const auth = useAuth(); + const [isOpen, setIsOpen] = useState(false); + const toggle = () => setIsOpen(!isOpen); const { register, setValue, control, handleSubmit } = useForm({ defaultValues: { industry: '', @@ -20,64 +20,78 @@ const CompanyProfile = () => { name: '', taxName: '', npwp: '', - alamat_wajib_pajak:'', - } - }) + alamat_wajib_pajak: '', + }, + }); - const [industries, setIndustries] = useState([]) + 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 dataIndustries = await odooApi('GET', '/api/v1/partner/industry'); + setIndustries( + dataIndustries?.map((o) => ({ value: o.id, label: o.name })) + ); + }; + loadIndustries(); + }, []); - const [companyTypes, setCompanyTypes] = useState([]) + 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() - }, []) + 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.parentId }) - 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) - } - if (auth) loadProfile() - }, [auth, setValue]) + const dataProfile = await addressApi({ id: auth.parentId }); + console.log('dataProfile', dataProfile); + 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); + }; + if (auth) loadProfile(); + }, [auth, setValue]); const onSubmitHandler = async (values) => { - if(changeConfirmation){ + if (changeConfirmation) { const data = { ...values, - id_user:auth.partnerId, + id_user: auth.partnerId, company_type_id: values.companyType, industry_id: values.industry, tax_name: values.taxName, - alamat_lengkap_text:values.alamat_wajib_pajak - } - const isUpdated = await odooApi('PUT', `/api/v1/partner/${auth.parentId}`, data) + alamat_lengkap_text: values.alamat_wajib_pajak, + street: values.alamat_bisnis, + }; + const isUpdated = await odooApi( + 'PUT', + `/api/v1/partner/${auth.parentId}`, + data + ); if (isUpdated?.id) { - toast.success('Berhasil mengubah profil', { duration: 1500 }) - return + toast.success('Berhasil mengubah profil', { duration: 1500 }); + return; } - toast.error('Terjadi kesalahan internal') + toast.error('Terjadi kesalahan internal'); } - } + }; const handleConfirmSubmit = () => { - setChangeConfirmation(false) - handleSubmit(onSubmitHandler)() - } + setChangeConfirmation(false); + handleSubmit(onSubmitHandler)(); + }; return ( <> @@ -106,11 +120,16 @@ const CompanyProfile = () => {
- {isOpen && ( - { - e.preventDefault() - setChangeConfirmation(true) - }}> + { + e.preventDefault(); + setChangeConfirmation(true); + }} + >
} + render={(props) => ( + + )} />
@@ -139,7 +163,9 @@ const CompanyProfile = () => { } + render={(props) => ( + + )} />
@@ -153,14 +179,35 @@ const CompanyProfile = () => {
- +
- - -
- + +
+
+ + +
+
+ +
- ) -} + ); +}; const validationSchema = Yup.object().shape({ type: Yup.string().required('Harus di-pilih'), @@ -228,14 +267,14 @@ const validationSchema = Yup.object().shape({ street: Yup.string().required('Harus di-isi'), zip: Yup.string().required('Harus di-isi'), city: Yup.string().required('Harus di-pilih'), - district: Yup.string().required('Harus di-pilih') -}) + district: Yup.string().required('Harus di-pilih'), +}); const types = [ { value: 'contact', label: 'Contact Address' }, { value: 'invoice', label: 'Invoice Address' }, { value: 'delivery', label: 'Delivery Address' }, - { value: 'other', label: 'Other Address' } -] + { value: 'other', label: 'Other Address' }, +]; -export default EditAddress +export default EditAddress; diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 70713bdf..f1577ca1 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -21,9 +21,10 @@ const CompanyProfile = () => { taxName: '', npwp: '', alamat_wajib_pajak: '', + alamat_bisnis: '', }, }); - + console.log('auth', auth); const [industries, setIndustries] = useState([]); useEffect(() => { const loadIndustries = async () => { @@ -74,6 +75,7 @@ const CompanyProfile = () => { alamat_lengkap_text: values.alamat_wajib_pajak, street: values.alamat_bisnis, }; + console.log('data', data); const isUpdated = await odooApi( 'PUT', `/api/v1/partner/${auth.parentId}`, -- cgit v1.2.3 From 7d5f7d239231cdfc439a57930e315586b206a0e5 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 17 Sep 2024 15:53:06 +0700 Subject: update new register --- src/lib/address/components/Addresses.jsx | 135 +++++++++---- src/lib/address/components/EditAddress.jsx | 311 ++++++++++++++++------------- src/lib/auth/components/CompanyProfile.jsx | 2 - 3 files changed, 275 insertions(+), 173 deletions(-) (limited to 'src/lib') diff --git a/src/lib/address/components/Addresses.jsx b/src/lib/address/components/Addresses.jsx index a610d371..a8879607 100644 --- a/src/lib/address/components/Addresses.jsx +++ b/src/lib/address/components/Addresses.jsx @@ -1,34 +1,72 @@ -import Link from '@/core/components/elements/Link/Link' -import Spinner from '@/core/components/elements/Spinner/Spinner' -import useAuth from '@/core/hooks/useAuth' -import { getItemAddress, updateItemAddress } from '@/core/utils/address' -import { useRouter } from 'next/router' -import useAddresses from '../hooks/useAddresses' -import MobileView from '@/core/components/views/MobileView' -import DesktopView from '@/core/components/views/DesktopView' -import Menu from '@/lib/auth/components/Menu' +import { useState } from 'react'; +import Link from '@/core/components/elements/Link/Link'; +import Spinner from '@/core/components/elements/Spinner/Spinner'; +import useAuth from '@/core/hooks/useAuth'; +import { getItemAddress, updateItemAddress } from '@/core/utils/address'; +import { useRouter } from 'next/router'; +import useAddresses from '../hooks/useAddresses'; +import MobileView from '@/core/components/views/MobileView'; +import DesktopView from '@/core/components/views/DesktopView'; +import Menu from '@/lib/auth/components/Menu'; +import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; const Addresses = () => { - const router = useRouter() - const { select = null } = router.query - const { addresses } = useAddresses() - const selectedAddress = getItemAddress(select || '') + const router = useRouter(); + const { select = null } = router.query; + const { addresses } = useAddresses(); + const selectedAddress = getItemAddress(select || ''); + const [changeConfirmation, setChangeConfirmation] = useState(false); + const [selectedForChange, setSelectedForChange] = useState(null); // State baru untuk simpan alamat yang akan diubah + const changeSelectedAddress = (id) => { - if (!select) return - updateItemAddress(select, id) - router.back() - } + if (!select) return; + updateItemAddress(select, id); + router.back(); + }; + + const handleConfirmSubmit = () => { + setChangeConfirmation(false); + if (selectedForChange) { + router.push(`/my/address/${selectedForChange}/edit`); + } + }; if (addresses.isLoading) { return (
- ) + ); } return ( <> + setChangeConfirmation(false)} // Menutup popup + title='Ubah alamat Bisnis' + > +
+ Anda akan mengubah alamat utama bisnis? +
+
+ + +
+
+
@@ -37,7 +75,10 @@ const Addresses = () => {
{addresses.data?.map((address, index) => { - const type = address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address' + const type = + address.type.charAt(0).toUpperCase() + + address.type.slice(1) + + ' Address'; return ( { type={type} changeSelectedAddress={changeSelectedAddress} selectedAddress={selectedAddress} + setChangeConfirmation={setChangeConfirmation} // Memanggil popup + setSelectedForChange={setSelectedForChange} // Simpan id address yang akan diubah select={select} /> - ) + ); })}
@@ -72,7 +115,9 @@ const Addresses = () => {
{addresses.data?.map((address, index) => { const type = - address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address' + address.type.charAt(0).toUpperCase() + + address.type.slice(1) + + ' Address'; return ( { type={type} changeSelectedAddress={changeSelectedAddress} selectedAddress={selectedAddress} + setChangeConfirmation={setChangeConfirmation} + setSelectedForChange={setSelectedForChange} select={select} /> - ) + ); })}
- ) -} + ); +}; -const AddressCard = ({ address, selectedAddress, changeSelectedAddress, type, select }) => { - const auth = useAuth() +const AddressCard = ({ + address, + selectedAddress, + changeSelectedAddress, + type, + select, + setChangeConfirmation, + setSelectedForChange, +}) => { + const auth = useAuth(); return (
-
changeSelectedAddress(address.id)} className={select && 'cursor-pointer'}> +
changeSelectedAddress(address.id)} + className={select && 'cursor-pointer'} + >
{type}
- {auth?.partnerId == address.id &&
Utama
} + {auth?.partnerId == address.id && ( +
Utama
+ )}

{address.name}

- {address.mobile &&

{address.mobile}

} + {address.mobile && ( +

{address.mobile}

+ )}

{address.street}

- { + setSelectedForChange(address.id); // Set alamat yang dipilih + setChangeConfirmation(true); // Tampilkan popup konfirmasi + }} className='btn-light bg-white mt-3 w-full !text-gray_r-11' > Ubah Alamat - +
- ) -} + ); +}; -export default Addresses +export default Addresses; diff --git a/src/lib/address/components/EditAddress.jsx b/src/lib/address/components/EditAddress.jsx index 7a30a3d8..626709db 100644 --- a/src/lib/address/components/EditAddress.jsx +++ b/src/lib/address/components/EditAddress.jsx @@ -6,13 +6,16 @@ import cityApi from '../api/cityApi'; import { Controller, useForm } from 'react-hook-form'; import districtApi from '../api/districtApi'; import subDistrictApi from '../api/subDistrictApi'; +import addressApi from '@/lib/address/api/addressApi'; import editAddressApi from '../api/editAddressApi'; import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'; import { toast } from 'react-hot-toast'; import Menu from '@/lib/auth/components/Menu'; +import useAuth from '@/core/hooks/useAuth'; +import odooApi from '@/core/api/odooApi'; const EditAddress = ({ id, defaultValues }) => { - console.log('defaultValues', defaultValues); + const auth = useAuth(); const router = useRouter(); const { register, @@ -31,6 +34,20 @@ const EditAddress = ({ id, defaultValues }) => { const [districts, setDistricts] = useState([]); const [subDistricts, setSubDistricts] = useState([]); + useEffect(() => { + const loadProfile = async () => { + const dataProfile = await addressApi({ id: auth.parentId }); + 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); + }; + if (auth) loadProfile(); + }, [auth, setValue]); + useEffect(() => { const loadCities = async () => { let dataCities = await cityApi(); @@ -92,170 +109,192 @@ const EditAddress = ({ id, defaultValues }) => { const data = { ...values, phone: values.mobile, - street: values.street, city_id: values.city, district_id: values.district, sub_district_id: values.subDistrict, }; - console.log('data', data); + const dataAlamat = { + ...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.street, + }; + + // const address = await editAddressApi({ id, data }); + const isUpdated = await odooApi( + 'PUT', + `/api/v1/partner/${auth.parentId}`, + dataAlamat + ); - const address = await editAddressApi({ id, data }); - if (address?.id) { + if (isUpdated?.id) { + // if (address?.id && isUpdated?.id) { toast.success('Berhasil mengubah alamat'); router.back(); + } else { + toast.error('Terjadi kesalahan internal'); + router.back(); } }; return ( -
-
- -
-
-

- Ubah Alamat -

-
-
-
- - ( - - )} - /> -
- {errors.type?.message} + <> +
+
+ +
+
+
+

+ Ubah Alamat +

+ {auth?.partnerId == id &&
Utama
} +
+ +
+
+ + ( + + )} + /> +
+ {errors.type?.message} +
-
-
- - -
- {errors.name?.message} +
+ + +
+ {errors.name?.message} +
-
-
- - -
- {errors.email?.message} +
+ + +
+ {errors.email?.message} +
-
-
- - -
- {errors.mobile?.message} +
+ + +
+ {errors.mobile?.message} +
-
-
- - -
- {errors.street?.message} +
+ + +
+ {errors.street?.message} +
-
-
- - -
- {errors.zip?.message} +
+ + +
+ {errors.zip?.message} +
-
-
- - ( - - )} - /> -
- {errors.city?.message} +
+ + ( + + )} + /> +
+ {errors.city?.message} +
-
-
- - ( - - )} - /> -
- {errors.district?.message} +
+ + ( + + )} + /> +
+ {errors.district?.message} +
-
-
- - ( - - )} - /> +
+ + ( + + )} + /> +
-
- - + + +
-
+ ); }; diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index f1577ca1..d4cf8657 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -24,7 +24,6 @@ const CompanyProfile = () => { alamat_bisnis: '', }, }); - console.log('auth', auth); const [industries, setIndustries] = useState([]); useEffect(() => { const loadIndustries = async () => { @@ -75,7 +74,6 @@ const CompanyProfile = () => { alamat_lengkap_text: values.alamat_wajib_pajak, street: values.alamat_bisnis, }; - console.log('data', data); const isUpdated = await odooApi( 'PUT', `/api/v1/partner/${auth.parentId}`, -- cgit v1.2.3 From dc3f5a3196a41c676f328aa0804af516d39e69f9 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 17 Sep 2024 16:23:41 +0700 Subject: update new register ubah alamat --- src/lib/address/components/Addresses.jsx | 9 +++++-- src/lib/address/components/EditAddress.jsx | 40 ++++++++++++++++-------------- 2 files changed, 28 insertions(+), 21 deletions(-) (limited to 'src/lib') diff --git a/src/lib/address/components/Addresses.jsx b/src/lib/address/components/Addresses.jsx index a8879607..9ca617ae 100644 --- a/src/lib/address/components/Addresses.jsx +++ b/src/lib/address/components/Addresses.jsx @@ -149,6 +149,7 @@ const AddressCard = ({ setSelectedForChange, }) => { const auth = useAuth(); + const router = useRouter(); return (
-- cgit v1.2.3 From 81f06d02bef8701be568e7418828d925925c5b12 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 10:58:20 +0700 Subject: update new register disebled email --- src/lib/address/components/CreateAddress.jsx | 205 +++++++++++++++++---------- src/lib/address/components/EditAddress.jsx | 2 +- 2 files changed, 132 insertions(+), 75 deletions(-) (limited to 'src/lib') diff --git a/src/lib/address/components/CreateAddress.jsx b/src/lib/address/components/CreateAddress.jsx index 86519147..e315affe 100644 --- a/src/lib/address/components/CreateAddress.jsx +++ b/src/lib/address/components/CreateAddress.jsx @@ -1,76 +1,101 @@ -import HookFormSelect from '@/core/components/elements/Select/HookFormSelect' -import useAuth from '@/core/hooks/useAuth' -import { useRouter } from 'next/router' -import { Controller, useForm } from 'react-hook-form' -import * as Yup from 'yup' -import cityApi from '../api/cityApi' -import districtApi from '../api/districtApi' -import subDistrictApi from '../api/subDistrictApi' -import { useEffect, useState } from 'react' -import createAddressApi from '../api/createAddressApi' -import { toast } from 'react-hot-toast' -import { yupResolver } from '@hookform/resolvers/yup' -import Menu from '@/lib/auth/components/Menu' +import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'; +import useAuth from '@/core/hooks/useAuth'; +import { useRouter } from 'next/router'; +import { Controller, useForm } from 'react-hook-form'; +import * as Yup from 'yup'; +import cityApi from '../api/cityApi'; +import districtApi from '../api/districtApi'; +import subDistrictApi from '../api/subDistrictApi'; +import { useEffect, useState } from 'react'; +import createAddressApi from '../api/createAddressApi'; +import { toast } from 'react-hot-toast'; +import { yupResolver } from '@hookform/resolvers/yup'; +import Menu from '@/lib/auth/components/Menu'; +import useAddresses from '../hooks/useAddresses'; const CreateAddress = () => { - const auth = useAuth() - const router = useRouter() + const auth = useAuth(); + const router = useRouter(); const { register, formState: { errors }, handleSubmit, watch, setValue, - control + control, } = useForm({ resolver: yupResolver(validationSchema), - defaultValues - }) - - const [cities, setCities] = useState([]) - const [districts, setDistricts] = useState([]) - const [subDistricts, setSubDistricts] = useState([]) + defaultValues, + }); + const { addresses = [] } = useAddresses(); // Ensure addresses is an array + const [cities, setCities] = useState([]); + const [districts, setDistricts] = useState([]); + const [subDistricts, setSubDistricts] = useState([]); + const [filteredTypes, setFilteredTypes] = useState(types); // State to manage filtered types useEffect(() => { const loadCities = async () => { - let dataCities = await cityApi() - dataCities = dataCities.map((city) => ({ value: city.id, label: city.name })) - setCities(dataCities) + let dataCities = await cityApi(); + dataCities = dataCities.map((city) => ({ + value: city.id, + label: city.name, + })); + setCities(dataCities); + }; + loadCities(); + }, []); + + useEffect(() => { + if (addresses) { + let hasContactAddress = false; + + for (let i = 0; i < addresses?.data?.length; i++) { + if (addresses.data[i].type === 'contact') { + hasContactAddress = true; + break; + } + } + if (hasContactAddress) { + setFilteredTypes(types.filter((type) => type.value !== 'contact')); + } else { + setFilteredTypes(types); + } } - loadCities() - }, []) + }, [auth]); - const watchCity = watch('city') + const watchCity = watch('city'); useEffect(() => { - setValue('district', '') + setValue('district', ''); if (watchCity) { const loadDistricts = async () => { - let dataDistricts = await districtApi({ cityId: watchCity }) + let dataDistricts = await districtApi({ cityId: watchCity }); dataDistricts = dataDistricts.map((district) => ({ value: district.id, - label: district.name - })) - setDistricts(dataDistricts) - } - loadDistricts() + label: district.name, + })); + setDistricts(dataDistricts); + }; + loadDistricts(); } - }, [watchCity, setValue]) + }, [watchCity, setValue]); - const watchDistrict = watch('district') + const watchDistrict = watch('district'); useEffect(() => { - setValue('subDistrict', '') + setValue('subDistrict', ''); if (watchDistrict) { const loadSubDistricts = async () => { - let dataSubDistricts = await subDistrictApi({ districtId: watchDistrict }) + let dataSubDistricts = await subDistrictApi({ + districtId: watchDistrict, + }); dataSubDistricts = dataSubDistricts.map((district) => ({ value: district.id, - label: district.name - })) - setSubDistricts(dataSubDistricts) - } - loadSubDistricts() + label: district.name, + })); + setSubDistricts(dataSubDistricts); + }; + loadSubDistricts(); } - }, [watchDistrict, setValue]) + }, [watchDistrict, setValue]); const onSubmitHandler = async (values) => { const data = { @@ -78,15 +103,15 @@ const CreateAddress = () => { city_id: values.city, district_id: values.district, sub_district_id: values.subDistrict, - parent_id: auth.partnerId - } + parent_id: auth.partnerId, + }; - const address = await createAddressApi({ data }) + const address = await createAddressApi({ data }); if (address?.id) { - toast.success('Berhasil menambahkan alamat') - router.back() + toast.success('Berhasil menambahkan alamat'); + router.back(); } - } + }; return (
@@ -102,10 +127,16 @@ const CreateAddress = () => { name='type' control={control} render={(props) => ( - + )} /> -
{errors.type?.message}
+
+ {errors.type?.message} +
@@ -116,7 +147,9 @@ const CreateAddress = () => { type='text' className='form-input' /> -
{errors.name?.message}
+
+ {errors.name?.message} +
@@ -127,7 +160,9 @@ const CreateAddress = () => { type='email' className='form-input' /> -
{errors.email?.message}
+
+ {errors.email?.message} +
@@ -138,7 +173,9 @@ const CreateAddress = () => { type='tel' className='form-input' /> -
{errors.mobile?.message}
+
+ {errors.mobile?.message} +
@@ -149,7 +186,9 @@ const CreateAddress = () => { type='text' className='form-input' /> -
{errors.street?.message}
+
+ {errors.street?.message} +
@@ -160,7 +199,9 @@ const CreateAddress = () => { type='number' className='form-input' /> -
{errors.zip?.message}
+
+ {errors.zip?.message} +
@@ -168,9 +209,13 @@ const CreateAddress = () => { } + render={(props) => ( + + )} /> -
{errors.city?.message}
+
+ {errors.city?.message} +
@@ -179,10 +224,16 @@ const CreateAddress = () => { name='district' control={control} render={(props) => ( - + )} /> -
{errors.district?.message}
+
+ {errors.district?.message} +
@@ -191,31 +242,37 @@ const CreateAddress = () => { name='subDistrict' control={control} render={(props) => ( - + )} />
-
- ) -} + ); +}; const validationSchema = Yup.object().shape({ type: Yup.string().required('Harus di-pilih'), name: Yup.string().min(3, 'Minimal 3 karakter').required('Harus di-isi'), - // email: Yup.string().email('Format harus seperti contoh@email.com').required('Harus di-isi'), mobile: Yup.string().required('Harus di-isi'), street: Yup.string().required('Harus di-isi'), zip: Yup.string().required('Harus di-isi'), city: Yup.string().required('Harus di-pilih'), - district: Yup.string().required('Harus di-pilih') -}) + district: Yup.string().required('Harus di-pilih'), +}); const defaultValues = { type: '', @@ -226,14 +283,14 @@ const defaultValues = { city: '', district: '', subDistrict: '', - zip: '' -} + zip: '', +}; const types = [ { value: 'contact', label: 'Contact Address' }, { value: 'invoice', label: 'Invoice Address' }, { value: 'delivery', label: 'Delivery Address' }, - { value: 'other', label: 'Other Address' } -] + { value: 'other', label: 'Other Address' }, +]; -export default CreateAddress +export default CreateAddress; diff --git a/src/lib/address/components/EditAddress.jsx b/src/lib/address/components/EditAddress.jsx index 580aaeb8..590a0bc0 100644 --- a/src/lib/address/components/EditAddress.jsx +++ b/src/lib/address/components/EditAddress.jsx @@ -29,7 +29,6 @@ const EditAddress = ({ id, defaultValues }) => { resolver: yupResolver(validationSchema), defaultValues, }); - const [cities, setCities] = useState([]); const [districts, setDistricts] = useState([]); const [subDistricts, setSubDistricts] = useState([]); @@ -195,6 +194,7 @@ const EditAddress = ({ id, defaultValues }) => { placeholder='johndoe@example.com' type='email' className='form-input' + disabled={auth?.partnerId == id && true} />
{errors.email?.message} -- cgit v1.2.3 From 7fb2329475fe2a671127251370436c56f79d24d8 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 13:45:57 +0700 Subject: update error edit alamat --- src/lib/address/components/EditAddress.jsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/lib') diff --git a/src/lib/address/components/EditAddress.jsx b/src/lib/address/components/EditAddress.jsx index 590a0bc0..ff6b1f12 100644 --- a/src/lib/address/components/EditAddress.jsx +++ b/src/lib/address/components/EditAddress.jsx @@ -42,6 +42,7 @@ const EditAddress = ({ id, defaultValues }) => { setValue('npwp', dataProfile.npwp); setValue('alamat_wajib_pajak', dataProfile.alamatWajibPajak); setValue('alamat_bisnis', dataProfile.alamatBisnis); + setValue('business_name', dataProfile.name); }; if (auth) loadProfile(); }, [auth, setValue]); @@ -102,7 +103,6 @@ const EditAddress = ({ id, defaultValues }) => { loadSubDistricts(); } }, [watchDistrict, setValue, getValues]); - const onSubmitHandler = async (values) => { const data = { ...values, @@ -116,13 +116,15 @@ const EditAddress = ({ id, defaultValues }) => { let isUpdated = true; if (auth?.partnerId == id) { dataAlamat = { - ...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.street, + business_name: values.business_name, + name: values.business_name, + npwp: values.npwp, }; isUpdated = await odooApi( 'PUT', @@ -132,7 +134,7 @@ const EditAddress = ({ id, defaultValues }) => { } // if (isUpdated?.id) { - if (address?.id) { + if (address?.id && isUpdated?.id) { toast.success('Berhasil mengubah alamat'); router.back(); } else { -- cgit v1.2.3 From ee88c2f776f5dbacfacb78d3a2f04155e0d93a15 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 14:37:00 +0700 Subject: add error handling --- src/lib/auth/components/CompanyProfile.jsx | 86 +++++++++++++----------------- 1 file changed, 38 insertions(+), 48 deletions(-) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index ee9c1f58..6d5790b4 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -7,62 +7,25 @@ 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'; const CompanyProfile = () => { const [changeConfirmation, setChangeConfirmation] = useState(false); const auth = useAuth(); const [isOpen, setIsOpen] = useState(false); const toggle = () => setIsOpen(!isOpen); - const { register, setValue, control, handleSubmit } = useForm({ - defaultValues: { - industry: '', - companyType: '', - name: '', - taxName: '', - npwp: '', - alamat_wajib_pajak: '', - alamat_bisnis: '', - }, + const { + register, + formState: { errors }, + setValue, + control, + handleSubmit, + } = useForm({ + resolver: yupResolver(validationSchema), + defaultValues, }); - const formatNpwp = (value) => { - try { - const cleaned = ('' + value).replace(/\D/g, ''); - let match; - if (cleaned.length <= 15) { - match = cleaned.match( - /(\d{0,2})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/ - ); - } else { - match = cleaned.match( - /(\d{0,3})?(\d{0,3})?(\d{0,3})?(\d{0,1})?(\d{0,3})?(\d{0,3})$/ - ); - } - - if (match) { - return [ - match[1], - match[2] ? '.' : '', - match[2], - match[3] ? '.' : '', - match[3], - match[4] ? '.' : '', - match[4], - match[5] ? '-' : '', - match[5], - match[6] ? '.' : '', - match[6], - ].join(''); - } - - // If match is null, return the original cleaned string or handle as needed - return cleaned; - } catch (error) { - // Handle error or return a default value - console.error('Error formatting NPWP:', error); - return value; - } - }; const [industries, setIndustries] = useState([]); useEffect(() => { const loadIndustries = async () => { @@ -91,6 +54,7 @@ const CompanyProfile = () => { useEffect(() => { const loadProfile = async () => { const dataProfile = await addressApi({ id: auth.parentId }); + console.log('dataProfile', dataProfile); setValue('name', dataProfile.name); setValue('industry', dataProfile.industryId); setValue('companyType', dataProfile.companyTypeId); @@ -103,6 +67,7 @@ const CompanyProfile = () => { }, [auth, setValue]); const onSubmitHandler = async (values) => { + console.log('values', values); if (changeConfirmation) { const data = { ...values, @@ -222,6 +187,9 @@ const CompanyProfile = () => { type='text' className='form-input mt-3' /> +
+ {errors.taxName?.message} +
@@ -230,6 +198,9 @@ const CompanyProfile = () => { type='text' className='form-input mt-3' /> +
+ {errors.alamat_wajib_pajak?.message} +
@@ -238,6 +209,9 @@ const CompanyProfile = () => { type='text' className='form-input mt-3' /> +
+ {errors.alamat_bisnis?.message} +
@@ -259,3 +233,19 @@ const CompanyProfile = () => { }; export default CompanyProfile; + +const validationSchema = Yup.object().shape({ + alamat_bisnis: Yup.string().required('Harus di-isi'), + alamat_wajib_pajak: Yup.string().required('Harus di-isi'), + taxName: Yup.string().required('Harus di-isi'), +}); + +const defaultValues = { + industry: '', + companyType: '', + name: '', + taxName: '', + npwp: '', + alamat_wajib_pajak: '', + alamat_bisnis: '', +}; -- cgit v1.2.3 From 5b0a6e9c8660c5cc427a66bd2540206513225075 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 14:41:18 +0700 Subject: delete console log --- src/lib/auth/components/CompanyProfile.jsx | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 6d5790b4..80b424dc 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -54,7 +54,6 @@ const CompanyProfile = () => { useEffect(() => { const loadProfile = async () => { const dataProfile = await addressApi({ id: auth.parentId }); - console.log('dataProfile', dataProfile); setValue('name', dataProfile.name); setValue('industry', dataProfile.industryId); setValue('companyType', dataProfile.companyTypeId); @@ -67,7 +66,6 @@ const CompanyProfile = () => { }, [auth, setValue]); const onSubmitHandler = async (values) => { - console.log('values', values); if (changeConfirmation) { const data = { ...values, -- cgit v1.2.3 From 09378e8389ad7dcca805e9dde10e26cf097ad97c Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 14:50:01 +0700 Subject: update new register --- src/lib/auth/components/CompanyProfile.jsx | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 80b424dc..9e1dd1f9 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -236,6 +236,9 @@ const validationSchema = Yup.object().shape({ alamat_bisnis: Yup.string().required('Harus di-isi'), alamat_wajib_pajak: Yup.string().required('Harus di-isi'), taxName: Yup.string().required('Harus di-isi'), + npwp: Yup.string().required('Harus di-isi'), + industry: Yup.string().required('Harus di-pilih'), + companyType: Yup.string().required('Harus di-pilih'), }); const defaultValues = { -- cgit v1.2.3 From e7082aec2592932c8f27e4d191476b69aac7475b Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 20 Sep 2024 14:53:52 +0700 Subject: update --- src/lib/auth/components/CompanyProfile.jsx | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/lib') diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 9e1dd1f9..606f56ea 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -176,6 +176,9 @@ const CompanyProfile = () => { className='form-input' placeholder='Cth: Indoteknik Dotcom Gemilang' /> +
+ {errors.name?.message} +
@@ -219,6 +222,9 @@ const CompanyProfile = () => { className='form-input mt-3' maxLength={16} /> +
+ {errors.npwp?.message} +
Badan Usaha
@@ -168,6 +171,9 @@ const CompanyProfile = () => { )} /> +
+ {errors.companyType?.message} +