From 1739c6eb03631e08bfe9d5de3a97acbc0a8566ce Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 13 Jan 2023 15:36:28 +0700 Subject: create "edit" feature and fix "create" feature --- src/helpers/convertToOption.js | 11 ++ src/helpers/formValidation.js | 1 + src/pages/my/address/[id]/edit.js | 239 ++++++++++++++++++++++++++++++++++++ src/pages/my/address/create.js | 248 +++++++++++++++++++------------------- src/pages/my/address/index.js | 33 +++-- 5 files changed, 397 insertions(+), 135 deletions(-) create mode 100644 src/helpers/convertToOption.js create mode 100644 src/pages/my/address/[id]/edit.js (limited to 'src') diff --git a/src/helpers/convertToOption.js b/src/helpers/convertToOption.js new file mode 100644 index 00000000..08fec08f --- /dev/null +++ b/src/helpers/convertToOption.js @@ -0,0 +1,11 @@ +const convertToOption = (data) => { + if (data) { + return { + value: data.id, + label: data.name, + } + } + return null; +}; + +export default convertToOption; \ No newline at end of file diff --git a/src/helpers/formValidation.js b/src/helpers/formValidation.js index 10c36e01..0e83f4cc 100644 --- a/src/helpers/formValidation.js +++ b/src/helpers/formValidation.js @@ -98,6 +98,7 @@ const useFormValidation = ({ initialFormValue = {}, validationScheme = {} }) => handleFormSubmit, handleInputChange, handleSelectChange, + hasChangedInputs, formInputs, formErrors }; diff --git a/src/pages/my/address/[id]/edit.js b/src/pages/my/address/[id]/edit.js new file mode 100644 index 00000000..d2de2815 --- /dev/null +++ b/src/pages/my/address/[id]/edit.js @@ -0,0 +1,239 @@ +import { useRouter } from "next/router"; +import AppBar from "../../../../components/AppBar"; +import Layout from "../../../../components/Layout"; +import WithAuth from "../../../../components/WithAuth"; +import { useAuth } from "../../../../helpers/auth"; +import useFormValidation from "../../../../helpers/formValidation"; +import { useEffect, useState } from "react"; +import apiOdoo from "../../../../helpers/apiOdoo"; +import ReactSelect from "react-select"; +import convertToOption from "../../../../helpers/convertToOption"; +import { toast } from "react-hot-toast"; +import _ from "lodash"; + +const validationScheme = { + type: ['label:Label Alamat', 'required'], + name: ['label:Nama', 'required'], + email: ['label:Email', 'required', 'email'], + mobile: ['label:No. Handphone', 'required', 'maxLength:16'], + street: ['label:Alamat', 'required'], + city: ['label:Kota', 'required'], + zip: ['label:Kode Pos', 'required'] +}; + +const typesSelection = [ + { value: 'contact', label: 'Contact Address' }, + { value: 'invoice', label: 'Invoice Address' }, + { value: 'delivery', label: 'Delivery Address' }, + { value: 'other', label: 'Other Address' }, +]; + +export async function getServerSideProps( context ) { + const { id } = context.query; + const address = await apiOdoo('GET', `/api/v1/partner/${id}/address`); + let initialFormValue = { + type: typesSelection.find((x) => x.value == address.type), + name: address.name, + email: address.email, + mobile: address.mobile, + street: address.street, + zip: address.zip, + city: convertToOption(address.city), + district: convertToOption(address.district), + subDistrict: convertToOption(address.sub_district) + }; + return { props: { id, initialFormValue } }; +} + +export default function EditAddress({ id, initialFormValue }) { + const [auth] = useAuth(); + const router = useRouter(); + + // Master Data + const [cities, setCities] = useState([]); + const [districts, setDistricts] = useState([]); + const [subDistricts, setSubDistricts] = useState([]); + + const { + formInputs, + formErrors, + handleInputChange, + handleSelectChange, + handleFormSubmit, + hasChangedInputs + } = useFormValidation({ validationScheme, initialFormValue }); + + useEffect(() => { + if (cities.length == 0) { + const loadCities = async () => { + let dataCities = await apiOdoo('GET', '/api/v1/city'); + dataCities = dataCities.map((city) => ({ value: city.id, label: city.name })); + setCities(dataCities); + }; + loadCities(); + } + }, [cities]); + + useEffect(() => { + if (hasChangedInputs?.city) handleSelectChange('district', null); + if (formInputs.city) { + const loadDistricts = async () => { + let dataDistricts = await apiOdoo('GET', `/api/v1/district?city_id=${formInputs.city.value}`); + dataDistricts = dataDistricts.map((district) => ({ value: district.id, label: district.name })); + setDistricts(dataDistricts); + }; + loadDistricts(); + } + }, [ formInputs.city, hasChangedInputs.city, handleSelectChange ]); + + useEffect(() => { + if (hasChangedInputs?.district) handleSelectChange('subDistrict', null); + if (formInputs.district) { + const loadSubDistricts = async () => { + let dataSubDistricts = await apiOdoo('GET', `/api/v1/sub_district?district_id=${formInputs.district.value}`); + dataSubDistricts = dataSubDistricts.map((subDistrict) => ({ value: subDistrict.id, label: subDistrict.name })); + setSubDistricts(dataSubDistricts); + }; + loadSubDistricts(); + } + }, [ formInputs.district, hasChangedInputs.district, handleSelectChange ]); + + const onSubmit = async () => { + const parameters = { + ...formInputs, + city_id: formInputs.city?.value, + district_id: formInputs.district?.value, + sub_district_id: formInputs.subDistrict?.value, + type: formInputs.type?.value + }; + + const address = await apiOdoo('PUT', `/api/v1/partner/${id}/address`, parameters); + if (address?.id) { + toast.success('Berhasil mengubah alamat'); + router.back(); + } + } + + return ( + + + + + { !_.isEmpty(formInputs) && ( +
handleFormSubmit(e, onSubmit)}> + + handleSelectChange('type', value)} + value={formInputs?.type} + /> +
{formErrors?.type}
+ + + +
{formErrors?.name}
+ + + +
{formErrors?.email}
+ + + +
{formErrors?.mobile}
+ + + +
{formErrors?.street}
+ + + +
{formErrors?.zip}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={cities} + value={formInputs.city} + onChange={(value) => handleSelectChange('city', value)} + /> +
{formErrors?.city}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={districts} + value={formInputs.district} + onChange={(value) => handleSelectChange('district', value)} + isDisabled={!formInputs.city} + /> + + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={subDistricts} + onChange={(value) => handleSelectChange('subDistrict', value)} + value={formInputs.subDistrict} + isDisabled={!formInputs.district} + /> + + + + ) } +
+
+ ); +} \ No newline at end of file diff --git a/src/pages/my/address/create.js b/src/pages/my/address/create.js index d68d1f76..457429e3 100644 --- a/src/pages/my/address/create.js +++ b/src/pages/my/address/create.js @@ -8,6 +8,7 @@ import { useAuth } from "../../../helpers/auth"; import useFormValidation from "../../../helpers/formValidation"; import { toast } from "react-hot-toast"; import { useRouter } from "next/router"; +import _ from "lodash"; const initialFormValue = { type: null, @@ -31,6 +32,13 @@ const validationScheme = { zip: ['label:Kode Pos', 'required'] }; +const typesSelection = [ + { value: 'contact', label: 'Contact Address' }, + { value: 'invoice', label: 'Invoice Address' }, + { value: 'delivery', label: 'Delivery Address' }, + { value: 'other', label: 'Other Address' }, +]; + export default function CreateAddress() { const [auth] = useAuth(); const router = useRouter(); @@ -85,13 +93,6 @@ export default function CreateAddress() { } }, [ formInputs.district, handleSelectChange ]); - const addressTypes = [ - { value: 'contact', label: 'Contact Address' }, - { value: 'invoice', label: 'Invoice Address' }, - { value: 'delivery', label: 'Delivery Address' }, - { value: 'other', label: 'Other Address' }, - ]; - const onSubmit = async () => { const parameters = { ...formInputs, @@ -99,15 +100,14 @@ export default function CreateAddress() { district_id: formInputs.district?.value, sub_district_id: formInputs.subDistrict?.value, type: formInputs.type?.value, - user_id: auth.id, - partner_id: auth.partner_id, + parent_id: auth.partner_id, }; - const address = await apiOdoo('POST', '/api/v1/partner', parameters); + const address = await apiOdoo('POST', '/api/v1/partner/address', parameters); if (address?.id) { handleFormReset(); toast.success('Berhasil menambahkan alamat'); - router.push('/my/address'); + router.back(); } } @@ -116,118 +116,120 @@ export default function CreateAddress() { -
handleFormSubmit(e, onSubmit)}> - - handleSelectChange('type', value)} - value={formInputs?.type} - /> -
{formErrors?.type}
- - - -
{formErrors?.name}
- - - -
{formErrors?.email}
- - - -
{formErrors?.mobile}
- - - -
{formErrors?.street}
- - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={cities} - value={formInputs.city} - onChange={(value) => handleSelectChange('city', value)} - /> -
{formErrors?.city}
- - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={districts} - value={formInputs.district} - onChange={(value) => handleSelectChange('district', value)} - isDisabled={!formInputs.city} - /> - - - state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} - options={subDistricts} - onChange={(value) => handleSelectChange('subDistrict', value)} - value={formInputs.subDistrict} - isDisabled={!formInputs.district} - /> - - - -
{formErrors?.zip}
- - - + { !_.isEmpty(formInputs) && ( +
handleFormSubmit(e, onSubmit)}> + + handleSelectChange('type', value)} + value={formInputs?.type} + /> +
{formErrors?.type}
+ + + +
{formErrors?.name}
+ + + +
{formErrors?.email}
+ + + +
{formErrors?.mobile}
+ + + +
{formErrors?.street}
+ + + +
{formErrors?.zip}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={cities} + value={formInputs.city} + onChange={(value) => handleSelectChange('city', value)} + /> +
{formErrors?.city}
+ + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={districts} + value={formInputs.district} + onChange={(value) => handleSelectChange('district', value)} + isDisabled={!formInputs.city} + /> + + + state.menuIsOpen || state.isFocused ? '!border-yellow_r-9' : '' }} + options={subDistricts} + onChange={(value) => handleSelectChange('subDistrict', value)} + value={formInputs.subDistrict} + isDisabled={!formInputs.district} + /> + + + + ) }
); diff --git a/src/pages/my/address/index.js b/src/pages/my/address/index.js index b97e21e7..787cfcfa 100644 --- a/src/pages/my/address/index.js +++ b/src/pages/my/address/index.js @@ -48,18 +48,27 @@ export default function Address() {
- { addresses && addresses.map((address, index) => ( -
changeSelectedAddress(address.id)} - > -

{ address.name }

-

{ address.mobile }

-

{ address.street } { address.street2 }

- -
- )) } + { auth && addresses && addresses.map((address, index) => { + let type = address.type.charAt(0).toUpperCase() + address.type.slice(1) + ' Address'; + return ( +
changeSelectedAddress(address.id)} + > +
+
{ type }
+ { auth?.partner_id == address.id && ( +
Utama
+ ) } +
+

{ address.name }

+

{ address.mobile }

+

{ address.street } { address.street2 }

+ Ubah Alamat +
+ ); + }) }
-- cgit v1.2.3