mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-33): create customers table and details
This commit is contained in:
@@ -2,7 +2,7 @@ import CustomerForm from "@/components/pages/master-data/customer/form/CustomerF
|
|||||||
|
|
||||||
const AddNonstock = () => {
|
const AddNonstock = () => {
|
||||||
return (
|
return (
|
||||||
<section>
|
<section className="w-full p-4 flex flex-row justify-center">
|
||||||
<CustomerForm/>
|
<CustomerForm/>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
import useSWR from 'swr';
|
||||||
|
import { CustomerApi } from '@/services/api/master-data';
|
||||||
|
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
||||||
|
import CustomerForm from '@/components/pages/master-data/customer/form/CustomerForm';
|
||||||
|
|
||||||
|
const CustomerEdit = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
const costumerId = searchParams.get('customerId');
|
||||||
|
|
||||||
|
const { data: costumer, isLoading: isLoadingCostumer } = useSWR(
|
||||||
|
costumerId,
|
||||||
|
(id: number) => CustomerApi.getSingle(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!costumerId) {
|
||||||
|
router.back();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='w-full flex flex-row justify-center items-center p-4'>
|
||||||
|
<span className='loading loading-spinner loading-xl' />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isLoadingCostumer && (!costumer || isResponseError(costumer))) {
|
||||||
|
router.replace('/404');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='w-full p-4 flex flex-row justify-center'>
|
||||||
|
{isLoadingCostumer && (
|
||||||
|
<span className='loading loading-spinner loading-xl' />
|
||||||
|
)}
|
||||||
|
{!isLoadingCostumer && isResponseSuccess(costumer) && (
|
||||||
|
<CustomerForm formType='edit' initialValues={costumer.data} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CustomerEdit;
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
|
import useSWR from "swr";
|
||||||
|
import { CustomerApi } from '@/services/api/master-data';
|
||||||
|
import { isResponseError, isResponseSuccess } from "@/lib/api-helper";
|
||||||
|
import CustomerForm from "@/components/pages/master-data/customer/form/CustomerForm";
|
||||||
|
|
||||||
|
const CustomerDetail = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
const costumerId = searchParams.get("customerId");
|
||||||
|
|
||||||
|
const { data: costumer, isLoading: isLoadingCostumer } = useSWR(
|
||||||
|
costumerId,
|
||||||
|
(id: number) => CustomerApi.getSingle(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
if(!costumerId){
|
||||||
|
router.back();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="w-full flex flex-row justify-center items-center p-4">
|
||||||
|
<span className="loading loading-spinner loading-xl" />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!isLoadingCostumer && (!costumer || isResponseError(costumer))){
|
||||||
|
router.replace("/404");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="w-full p-4 flex flex-row justify-center">
|
||||||
|
{isLoadingCostumer && <span className="loading loading-spinner loading-xl" />}
|
||||||
|
{!isLoadingCostumer && isResponseSuccess(costumer) && (
|
||||||
|
<CustomerForm formType="detail" initialValues={costumer.data} />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CustomerDetail;
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
'use client'
|
'use client';
|
||||||
|
|
||||||
import Button from "@/components/Button";
|
import Button from '@/components/Button';
|
||||||
import DebouncedTextInput from "@/components/input/DebouncedTextInput";
|
import DebouncedTextInput from '@/components/input/DebouncedTextInput';
|
||||||
import { useModal } from "@/components/Modal";
|
import SelectInput, { OptionType } from '@/components/input/SelectInput';
|
||||||
import ConfirmationModal from "@/components/modal/ConfirmationModal";
|
import { useModal } from '@/components/Modal';
|
||||||
import Table from "@/components/Table";
|
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
||||||
import RowCollapseOptions from "@/components/table/RowCollapseOptions";
|
import Table from '@/components/Table';
|
||||||
import RowDropdownOptions from "@/components/table/RowDropdownOptions";
|
import RowCollapseOptions from '@/components/table/RowCollapseOptions';
|
||||||
import { isResponseSuccess } from "@/lib/api-helper";
|
import RowDropdownOptions from '@/components/table/RowDropdownOptions';
|
||||||
import { cn } from "@/lib/helper";
|
import { ROWS_OPTIONS } from '@/config/constant';
|
||||||
import { CustomerApi } from "@/services/api/master-data";
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
import { useTableFilter } from "@/services/hooks/useTableFilter";
|
import { cn } from '@/lib/helper';
|
||||||
import { Customer } from "@/types/api/master-data/customer";
|
import { CustomerApi } from '@/services/api/master-data';
|
||||||
import { Icon } from "@iconify/react";
|
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||||
|
import { Customer } from '@/types/api/master-data/customer';
|
||||||
|
import { Icon } from '@iconify/react';
|
||||||
import {
|
import {
|
||||||
CellContext,
|
CellContext,
|
||||||
ColumnDef,
|
ColumnDef,
|
||||||
ColumnSort,
|
ColumnSort,
|
||||||
SortingState,
|
SortingState,
|
||||||
} from "@tanstack/react-table";
|
} from '@tanstack/react-table';
|
||||||
import { useState } from "react";
|
import { useState } from 'react';
|
||||||
import useSWR from "swr";
|
import toast from 'react-hot-toast';
|
||||||
|
import useSWR from 'swr';
|
||||||
|
|
||||||
const RowOptionsMenu = ({
|
const RowOptionsMenu = ({
|
||||||
type = 'dropdown',
|
type = 'dropdown',
|
||||||
props,
|
props,
|
||||||
deleteClickHandler,
|
deleteClickHandler,
|
||||||
} : {
|
}: {
|
||||||
type: 'dropdown' | 'collapse',
|
type: 'dropdown' | 'collapse';
|
||||||
props: CellContext<Customer, unknown>;
|
props: CellContext<Customer, unknown>;
|
||||||
deleteClickHandler: () => void;
|
deleteClickHandler: () => void;
|
||||||
}) => {
|
}) => {
|
||||||
@@ -37,25 +40,33 @@ const RowOptionsMenu = ({
|
|||||||
className={cn(
|
className={cn(
|
||||||
{
|
{
|
||||||
'dropdown-content': type === 'dropdown',
|
'dropdown-content': type === 'dropdown',
|
||||||
'mt-2': type === 'collapse'
|
'mt-2': type === 'collapse',
|
||||||
},
|
},
|
||||||
'p-2.5 mr-2 flex flex-col gap-1 bg-base-100 rounded-box z-10 border border-black/10 shadow'
|
'p-2.5 mr-2 flex flex-col gap-1 bg-base-100 rounded-box z-10 border border-black/10 shadow'
|
||||||
)
|
)}
|
||||||
}
|
>
|
||||||
>
|
|
||||||
<Button
|
<Button
|
||||||
|
href={`/master-data/customer/detail/?customerId=${props.row.original.id}`}
|
||||||
|
variant='ghost'
|
||||||
|
color='primary'
|
||||||
className='justify-start text-sm'
|
className='justify-start text-sm'
|
||||||
>
|
>
|
||||||
<Icon icon='mdi:eye-outline' width={16} height={16}/>
|
<Icon icon='mdi:eye-outline' width={16} height={16} />
|
||||||
Detail
|
Detail
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
className='justify-start text-sm'
|
className='justify-start text-sm'
|
||||||
|
href={`/master-data/customer/detail/edit/?customerId=${props.row.original.id}`}
|
||||||
|
variant='ghost'
|
||||||
|
color='warning'
|
||||||
>
|
>
|
||||||
<Icon icon='material-symbols:edit-outline' width={16} height={16} />
|
<Icon icon='material-symbols:edit-outline' width={16} height={16} />
|
||||||
Edit
|
Edit
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
|
onClick={deleteClickHandler}
|
||||||
|
variant='ghost'
|
||||||
|
color='error'
|
||||||
className='text-error hover:text-inherit'
|
className='text-error hover:text-inherit'
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
@@ -64,10 +75,11 @@ const RowOptionsMenu = ({
|
|||||||
height={16}
|
height={16}
|
||||||
className='justify-start text-sm'
|
className='justify-start text-sm'
|
||||||
/>
|
/>
|
||||||
|
Delete
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
const CustomersTable = () => {
|
const CustomersTable = () => {
|
||||||
const {
|
const {
|
||||||
@@ -76,31 +88,34 @@ const CustomersTable = () => {
|
|||||||
setPage,
|
setPage,
|
||||||
setPageSize,
|
setPageSize,
|
||||||
toQueryString: getTableFilterQueryString,
|
toQueryString: getTableFilterQueryString,
|
||||||
} = useTableFilter ({
|
} = useTableFilter({
|
||||||
initial: { search: '', nameSort: '', picSort: ''},
|
initial: { search: '', nameSort: '', picSort: '' },
|
||||||
paramMap: {
|
paramMap: {
|
||||||
page: 'page',
|
page: 'page',
|
||||||
pageSize: 'limit',
|
pageSize: 'limit',
|
||||||
nameSort: 'sort_name',
|
nameSort: 'sort_name',
|
||||||
picSort: 'sort_pic',
|
picSort: 'sort_pic',
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fetch Data
|
||||||
const {
|
const {
|
||||||
data: customers,
|
data: customers,
|
||||||
isLoading,
|
isLoading,
|
||||||
mutate: refreshCustomers
|
mutate: refreshCustomers,
|
||||||
} = useSWR(
|
} = useSWR(
|
||||||
`${CustomerApi.basePath}${getTableFilterQueryString()}`,
|
`${CustomerApi.basePath}${getTableFilterQueryString()}`,
|
||||||
CustomerApi.getAllFetcher
|
CustomerApi.getAllFetcher
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// State
|
||||||
const deleteModal = useModal();
|
const deleteModal = useModal();
|
||||||
|
const [selectedCustomer, setSelectedCustomer] = useState<
|
||||||
const [selectedCustomer, setSelectedCustomer] = useState<Customer | undefined>(undefined);
|
Customer | undefined
|
||||||
|
>(undefined);
|
||||||
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
||||||
|
|
||||||
|
// Columns Definition
|
||||||
const customersColumns: ColumnDef<Customer>[] = [
|
const customersColumns: ColumnDef<Customer>[] = [
|
||||||
{
|
{
|
||||||
header: '#',
|
header: '#',
|
||||||
@@ -173,6 +188,24 @@ const CustomersTable = () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Handler
|
||||||
|
const confirmationModalDeleteClickHandler = async () => {
|
||||||
|
setIsDeleteLoading(true);
|
||||||
|
|
||||||
|
await CustomerApi.delete(selectedCustomer?.id as number);
|
||||||
|
refreshCustomers();
|
||||||
|
|
||||||
|
deleteModal.closeModal();
|
||||||
|
toast.success('Successfully delete Customer!');
|
||||||
|
setIsDeleteLoading(false);
|
||||||
|
};
|
||||||
|
const searchChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
updateFilter('search', e.target.value);
|
||||||
|
};
|
||||||
|
const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
|
const newVal = val as OptionType;
|
||||||
|
setPageSize(newVal.value as number);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -190,11 +223,22 @@ const CustomersTable = () => {
|
|||||||
name='search'
|
name='search'
|
||||||
placeholder='Cari Kandang'
|
placeholder='Cari Kandang'
|
||||||
value={tableFilterState.search}
|
value={tableFilterState.search}
|
||||||
|
onChange={searchChangeHandler}
|
||||||
className={{ wrapper: 'sm:max-w-3xs' }}
|
className={{ wrapper: 'sm:max-w-3xs' }}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='flex flex-row justify-end'>
|
<div className='flex flex-row justify-end'>
|
||||||
|
<SelectInput
|
||||||
|
label='Baris'
|
||||||
|
options={ROWS_OPTIONS}
|
||||||
|
value={{
|
||||||
|
label: String(tableFilterState.pageSize),
|
||||||
|
value: tableFilterState.pageSize,
|
||||||
|
}}
|
||||||
|
onChange={pageSizeChangeHandler}
|
||||||
|
className={{ wrapper: 'max-w-28' }}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -236,10 +280,11 @@ const CustomersTable = () => {
|
|||||||
text: 'Ya',
|
text: 'Ya',
|
||||||
color: 'error',
|
color: 'error',
|
||||||
isLoading: isDeleteLoading,
|
isLoading: isDeleteLoading,
|
||||||
|
onClick: confirmationModalDeleteClickHandler,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default CustomersTable;
|
export default CustomersTable;
|
||||||
@@ -1,24 +1,29 @@
|
|||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
|
|
||||||
export const CustomerFormSchema = Yup.object({
|
export const CustomerFormSchema = Yup.object({
|
||||||
name: Yup.string()
|
name: Yup.string().required('Nama wajib diisi!'),
|
||||||
.required('Nama wajib diisi!'),
|
|
||||||
|
|
||||||
picId: Yup.number()
|
picId: Yup.number().min(1, 'PIC wajib diisi!').required('PIC wajib diisi!'),
|
||||||
.min(1, 'PIC wajib diisi!')
|
|
||||||
.required('PIC wajib diisi!'),
|
|
||||||
|
|
||||||
pic: Yup.object({
|
pic: Yup.object({
|
||||||
value: Yup.number().min(1).required(),
|
value: Yup.number().min(1).required(),
|
||||||
label: Yup.string().required(),
|
label: Yup.string().required(),
|
||||||
}).nullable(),
|
}).required('PIC wajib diisi!'),
|
||||||
|
|
||||||
type: Yup.string()
|
type: Yup.object({
|
||||||
.oneOf(['INDIVIDUAL', 'BISNIS'], 'Tipe harus INDIVIDUAL atau BISNIS')
|
value: Yup.string().required(),
|
||||||
|
label: Yup.string().required(),
|
||||||
|
})
|
||||||
|
.oneOf(
|
||||||
|
[
|
||||||
|
{ value: 'INDIVIDUAL', label: 'INDIVIDUAL' },
|
||||||
|
{ value: 'BISNIS', label: 'BISNIS' },
|
||||||
|
],
|
||||||
|
'Tipe harus INDIVIDUAL atau BISNIS'
|
||||||
|
)
|
||||||
.required('Tipe wajib diisi!'),
|
.required('Tipe wajib diisi!'),
|
||||||
|
|
||||||
address: Yup.string()
|
address: Yup.string().required('Alamat wajib diisi!'),
|
||||||
.required('Alamat wajib diisi!'),
|
|
||||||
|
|
||||||
phone: Yup.string()
|
phone: Yup.string()
|
||||||
.matches(/^[0-9]+$/, 'Nomor telepon hanya boleh berisi angka!')
|
.matches(/^[0-9]+$/, 'Nomor telepon hanya boleh berisi angka!')
|
||||||
|
|||||||
@@ -1,190 +1,205 @@
|
|||||||
'use client'
|
'use client';
|
||||||
|
|
||||||
import { useModal } from "@/components/Modal";
|
import { useModal } from '@/components/Modal';
|
||||||
import { isResponseError, isResponseSuccess } from "@/lib/api-helper";
|
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
||||||
import { CustomerApi } from "@/services/api/master-data";
|
import { CustomerApi } from '@/services/api/master-data';
|
||||||
import { CreateCustomerPayload, Customer, UpdateCustomerPayload } from "@/types/api/master-data/customer";
|
import {
|
||||||
import { useRouter } from "next/navigation";
|
CreateCustomerPayload,
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
Customer,
|
||||||
import toast from "react-hot-toast";
|
UpdateCustomerPayload,
|
||||||
import { CustomerFormValues } from "./CustomerForm.schema";
|
} from '@/types/api/master-data/customer';
|
||||||
import { useFormik } from "formik";
|
import { useRouter } from 'next/navigation';
|
||||||
import Button from "@/components/Button";
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import { Icon } from "@iconify/react";
|
import toast from 'react-hot-toast';
|
||||||
import TextInput from "@/components/input/TextInput";
|
import { CustomerFormValues } from './CustomerForm.schema';
|
||||||
import { cn } from "@/lib/helper";
|
import { useFormik } from 'formik';
|
||||||
import ConfirmationModal from "@/components/modal/ConfirmationModal";
|
import Button from '@/components/Button';
|
||||||
import TextArea from "@/components/input/TextArea";
|
import { Icon } from '@iconify/react';
|
||||||
import SelectInput, { OptionType } from "@/components/input/SelectInput";
|
import TextInput from '@/components/input/TextInput';
|
||||||
import useSWR from "swr";
|
import { cn } from '@/lib/helper';
|
||||||
import { UserApi } from "@/services/api/user";
|
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
||||||
import { CUSTOMER_TYPE_OPTIONS } from "@/config/constant";
|
import TextArea from '@/components/input/TextArea';
|
||||||
|
import SelectInput, { OptionType } from '@/components/input/SelectInput';
|
||||||
|
import useSWR from 'swr';
|
||||||
|
import { UserApi } from '@/services/api/user';
|
||||||
|
import { CUSTOMER_TYPE_OPTIONS } from '@/config/constant';
|
||||||
|
|
||||||
interface CustomerFormProps {
|
interface CustomerFormProps {
|
||||||
formType?: 'add' | 'edit' | 'detail';
|
formType?: 'add' | 'edit' | 'detail';
|
||||||
initialValues?: Customer;
|
initialValues?: Customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) => {
|
const CustomerForm = ({
|
||||||
// Setup Kebutuhan Form
|
formType = 'add',
|
||||||
const router = useRouter();
|
initialValues,
|
||||||
const deleteModal = useModal();
|
}: CustomerFormProps) => {
|
||||||
|
// Setup Kebutuhan Form
|
||||||
|
const router = useRouter();
|
||||||
|
const deleteModal = useModal();
|
||||||
|
|
||||||
// Setup State
|
// Setup State
|
||||||
const [customerFormErrorMessage, setCustomerFormErrorMessage] = useState('');
|
const [customerFormErrorMessage, setCustomerFormErrorMessage] = useState('');
|
||||||
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
||||||
const [picSelectInputValue, setPicSelectInputValue] = useState('');
|
const [picSelectInputValue, setPicSelectInputValue] = useState('');
|
||||||
const [typeSelectInputValue, setTypeSelectInputValue] = useState('');
|
const [typeSelectInputValue, setTypeSelectInputValue] = useState('');
|
||||||
|
|
||||||
// Fetch Data
|
// Fetch Data
|
||||||
const picUrl = `${UserApi.basePath}?${new URLSearchParams({
|
const picUrl = `${UserApi.basePath}?${new URLSearchParams({
|
||||||
search: picSelectInputValue ?? '',
|
search: picSelectInputValue ?? '',
|
||||||
})}`;
|
})}`;
|
||||||
|
|
||||||
const { data: pic, isLoading: isLoadingPic } = useSWR(
|
const { data: pic, isLoading: isLoadingPic } = useSWR(
|
||||||
picUrl,
|
picUrl,
|
||||||
UserApi.getAllFetcher
|
UserApi.getAllFetcher
|
||||||
);
|
);
|
||||||
|
|
||||||
// -- Options data mapping
|
// -- Options data mapping
|
||||||
const picOptions = isResponseSuccess(pic)
|
const picOptions = isResponseSuccess(pic)
|
||||||
? pic?.data.map((area) => ({
|
? pic?.data.map((area) => ({
|
||||||
value: area.id,
|
value: area.id,
|
||||||
label: area.name,
|
label: area.name,
|
||||||
}))
|
}))
|
||||||
: [];
|
: [];
|
||||||
const typeOptions = CUSTOMER_TYPE_OPTIONS;
|
const typeOptions = CUSTOMER_TYPE_OPTIONS;
|
||||||
|
|
||||||
|
// Handler Event
|
||||||
|
const createCustomerHandler = useCallback(
|
||||||
|
async (payload: CreateCustomerPayload) => {
|
||||||
|
const createCustomerRes = await CustomerApi.create(payload);
|
||||||
|
|
||||||
// Handler Event
|
if (isResponseError(createCustomerRes)) {
|
||||||
const createCustomerHandler = useCallback(
|
setCustomerFormErrorMessage(createCustomerRes.message);
|
||||||
async (payload : CreateCustomerPayload) => {
|
return;
|
||||||
const createCustomerRes = await CustomerApi.create(payload);
|
}
|
||||||
|
|
||||||
if(isResponseError(createCustomerRes)){
|
toast.success(createCustomerRes?.message as string);
|
||||||
setCustomerFormErrorMessage(createCustomerRes.message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success(createCustomerRes?.message as string);
|
|
||||||
router.push('/master-data/customer');
|
|
||||||
},
|
|
||||||
[router]
|
|
||||||
);
|
|
||||||
const updateCustomerHandler = useCallback(
|
|
||||||
async (customerId : number, payload : UpdateCustomerPayload) => {
|
|
||||||
const updateCustomerRes = await CustomerApi.update(customerId, payload);
|
|
||||||
|
|
||||||
if(isResponseError(updateCustomerRes)){
|
|
||||||
setCustomerFormErrorMessage(updateCustomerRes.message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success(updateCustomerRes?.message as string);
|
|
||||||
router.push('/master-data/customer');
|
|
||||||
},
|
|
||||||
[router]
|
|
||||||
);
|
|
||||||
|
|
||||||
const deleteCustomerHandler = () => {
|
|
||||||
deleteModal.openModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
const confirmationModalDeleteclickHandler = async () => {
|
|
||||||
setIsDeleteLoading(true);
|
|
||||||
|
|
||||||
await CustomerApi.delete(initialValues?.id as number);
|
|
||||||
|
|
||||||
deleteModal.closeModal();
|
|
||||||
setIsDeleteLoading(false);
|
|
||||||
router.push('/master-data/customer');
|
router.push('/master-data/customer');
|
||||||
|
},
|
||||||
|
[router]
|
||||||
|
);
|
||||||
|
const updateCustomerHandler = useCallback(
|
||||||
|
async (customerId: number, payload: UpdateCustomerPayload) => {
|
||||||
|
const updateCustomerRes = await CustomerApi.update(customerId, payload);
|
||||||
|
|
||||||
|
if (isResponseError(updateCustomerRes)) {
|
||||||
|
setCustomerFormErrorMessage(updateCustomerRes.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success(updateCustomerRes?.message as string);
|
||||||
|
router.push('/master-data/customer');
|
||||||
|
},
|
||||||
|
[router]
|
||||||
|
);
|
||||||
|
|
||||||
|
const deleteCustomerHandler = () => {
|
||||||
|
deleteModal.openModal();
|
||||||
|
};
|
||||||
|
|
||||||
|
const confirmationModalDeleteclickHandler = async () => {
|
||||||
|
setIsDeleteLoading(true);
|
||||||
|
|
||||||
|
await CustomerApi.delete(initialValues?.id as number);
|
||||||
|
|
||||||
|
deleteModal.closeModal();
|
||||||
|
setIsDeleteLoading(false);
|
||||||
|
router.push('/master-data/customer');
|
||||||
|
};
|
||||||
|
|
||||||
|
// -- Option Handler
|
||||||
|
const picChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
|
formik.setFieldTouched('pic', true);
|
||||||
|
formik.setFieldValue('pic', val);
|
||||||
|
|
||||||
|
formik.setFieldTouched('picId', true);
|
||||||
|
formik.setFieldValue('picId', (val as OptionType)?.value);
|
||||||
|
};
|
||||||
|
const typeChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
|
formik.setFieldTouched('type', true);
|
||||||
|
formik.setFieldValue('type', val);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Utils Functions
|
||||||
|
const normalizeType = (type?: string | { value: string; label: string }) => {
|
||||||
|
if (!type) return CUSTOMER_TYPE_OPTIONS[0];
|
||||||
|
return typeof type === 'string' ? { value: type, label: type } : type;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Memo untuk simpan input sebelumnya
|
||||||
|
const formikInitialValues = useMemo<CustomerFormValues>(() => {
|
||||||
|
return {
|
||||||
|
name: initialValues?.name ?? '',
|
||||||
|
email: initialValues?.email ?? '',
|
||||||
|
phone: initialValues?.phone ?? '',
|
||||||
|
picId: initialValues?.pic?.id ?? 0,
|
||||||
|
pic: initialValues?.pic
|
||||||
|
? {
|
||||||
|
value: initialValues.pic.id,
|
||||||
|
label: initialValues.pic.name,
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
value: 0,
|
||||||
|
label: '',
|
||||||
|
},
|
||||||
|
type: normalizeType(initialValues?.type),
|
||||||
|
address: initialValues?.address ?? '',
|
||||||
|
account_number: initialValues?.account_number ?? '',
|
||||||
};
|
};
|
||||||
|
}, [initialValues]);
|
||||||
|
|
||||||
// -- Option Handler
|
// Formik
|
||||||
const picChangeHandler = (val: OptionType | OptionType[] | null) => {
|
const formik = useFormik<CustomerFormValues>({
|
||||||
formik.setFieldTouched('pic', true);
|
initialValues: formikInitialValues,
|
||||||
formik.setFieldValue('pic', val);
|
enableReinitialize: true,
|
||||||
|
onSubmit: async (values) => {
|
||||||
|
// reset error message
|
||||||
|
setCustomerFormErrorMessage('');
|
||||||
|
|
||||||
formik.setFieldTouched('picId', true);
|
// create payload
|
||||||
formik.setFieldValue('picId', (val as OptionType)?.value);
|
const payload: CreateCustomerPayload = {
|
||||||
}
|
name: values.name,
|
||||||
const typeChangeHandler = (val: OptionType | OptionType[] | null) => {
|
email: values.email,
|
||||||
formik.setFieldTouched('type', true);
|
phone: values.phone,
|
||||||
formik.setFieldValue('type', val);
|
pic_id: values.picId,
|
||||||
}
|
type: (values.type as OptionType).value as string,
|
||||||
|
address: values.address,
|
||||||
|
account_number: values.account_number,
|
||||||
|
};
|
||||||
|
|
||||||
// Memo untuk simpan input sebelumnya
|
// cek type form yang disubmit
|
||||||
const formikInitialValues = useMemo<CustomerFormValues>(() => {
|
switch (formType) {
|
||||||
return {
|
case 'add':
|
||||||
name: initialValues?.name as string ?? '',
|
await createCustomerHandler(payload);
|
||||||
email: initialValues?.email ?? '',
|
break;
|
||||||
phone: initialValues?.phone ?? '',
|
case 'edit':
|
||||||
picId: initialValues?.pic?.id ?? 0,
|
await updateCustomerHandler(initialValues?.id as number, payload);
|
||||||
pic: initialValues?.pic ? {
|
break;
|
||||||
value: initialValues.pic.id,
|
}
|
||||||
label: initialValues.pic.name,
|
},
|
||||||
} : null,
|
});
|
||||||
type: initialValues?.type ?? 'INDIVIDUAL',
|
|
||||||
address: initialValues?.address ?? '',
|
|
||||||
account_number: initialValues?.account_number ?? '',
|
|
||||||
};
|
|
||||||
}, [initialValues]);
|
|
||||||
|
|
||||||
// Formik
|
const { setValues: formikSetValues } = formik;
|
||||||
const formik = useFormik<CustomerFormValues>({
|
|
||||||
initialValues: formikInitialValues,
|
|
||||||
enableReinitialize: true,
|
|
||||||
onSubmit: async (values) => {
|
|
||||||
// reset error message
|
|
||||||
setCustomerFormErrorMessage('');
|
|
||||||
|
|
||||||
// create payload
|
// Initialize Formik
|
||||||
const payload: CreateCustomerPayload = {
|
useEffect(() => {
|
||||||
name: values.name,
|
formikSetValues(formikInitialValues);
|
||||||
email: values.email,
|
}, [formikSetValues, formikInitialValues]);
|
||||||
phone: values.phone,
|
|
||||||
pic_id: values.picId,
|
|
||||||
type: values.type,
|
|
||||||
address: values.address,
|
|
||||||
account_number: values.account_number,
|
|
||||||
};
|
|
||||||
|
|
||||||
// cek type form yang disubmit
|
// Render
|
||||||
switch (formType) {
|
return (
|
||||||
case 'add':
|
<>
|
||||||
await createCustomerHandler(payload);
|
<section className='w-full max-w-xl'>
|
||||||
break;
|
<header className='flex flex-col gap-4'>
|
||||||
case 'edit':
|
|
||||||
await updateCustomerHandler(initialValues?.id as number, payload);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
const {setValues: formikSetValues} = formik;
|
|
||||||
|
|
||||||
// Initialize Formik
|
|
||||||
useEffect(() => {
|
|
||||||
formikSetValues(formikInitialValues);
|
|
||||||
}, [formikSetValues, formikInitialValues]);
|
|
||||||
|
|
||||||
|
|
||||||
// Render
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<section className="mx-auto my-8 max-w-xl">
|
|
||||||
<header className="flex flex-col gap-4">
|
|
||||||
<Button
|
<Button
|
||||||
href="/master-data/customer"
|
href='/master-data/customer'
|
||||||
variant="link"
|
variant='link'
|
||||||
className="w-fit p-0 text-primary"
|
className='w-fit p-0 text-primary'
|
||||||
>
|
>
|
||||||
<Icon icon='uil:arrow-left' width={24} height={24} />
|
<Icon icon='uil:arrow-left' width={24} height={24} />
|
||||||
Kembali
|
Kembali
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<h1 className="text-2xl font-bold text-center">
|
<h1 className='text-2xl font-bold text-center'>
|
||||||
{formType === 'add' && 'Tambah Customer'}
|
{formType === 'add' && 'Tambah Customer'}
|
||||||
{formType === 'edit' && 'Ubah Customer'}
|
{formType === 'edit' && 'Ubah Customer'}
|
||||||
{formType === 'detail' && 'Detail Customer'}
|
{formType === 'detail' && 'Detail Customer'}
|
||||||
@@ -194,16 +209,16 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
<form
|
<form
|
||||||
onSubmit={formik.handleSubmit}
|
onSubmit={formik.handleSubmit}
|
||||||
onReset={formik.handleReset}
|
onReset={formik.handleReset}
|
||||||
className="w-full mt-8 flex flex-col gap-6"
|
className='w-full mt-8 flex flex-col gap-6'
|
||||||
>
|
>
|
||||||
|
|
||||||
{/* Fields Form */}
|
{/* Fields Form */}
|
||||||
<div className="flex flex-col gap-4">
|
<div className='flex flex-col gap-4'>
|
||||||
|
{formik.values.picId}
|
||||||
<TextInput
|
<TextInput
|
||||||
required
|
required
|
||||||
label="Nama"
|
label='Nama'
|
||||||
name="name"
|
name='name'
|
||||||
placeholder="Masukkan nama customer"
|
placeholder='Masukkan nama customer'
|
||||||
value={formik.values.name}
|
value={formik.values.name}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
@@ -213,7 +228,7 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
/>
|
/>
|
||||||
<SelectInput
|
<SelectInput
|
||||||
required
|
required
|
||||||
placeholder="Pilih PIC"
|
placeholder='Pilih PIC'
|
||||||
label='PIC'
|
label='PIC'
|
||||||
value={formik.values.pic ?? undefined}
|
value={formik.values.pic ?? undefined}
|
||||||
onChange={picChangeHandler}
|
onChange={picChangeHandler}
|
||||||
@@ -228,9 +243,13 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
/>
|
/>
|
||||||
<SelectInput
|
<SelectInput
|
||||||
required
|
required
|
||||||
placeholder="Pilih Tipe"
|
placeholder='Pilih Tipe'
|
||||||
label='Tipe'
|
label='Tipe'
|
||||||
value={typeOptions.find((item) => item.value === formik.values.type) ?? undefined}
|
value={
|
||||||
|
typeOptions.find(
|
||||||
|
(item) => item.value === formik.values.type.value
|
||||||
|
) ?? undefined
|
||||||
|
}
|
||||||
onChange={typeChangeHandler}
|
onChange={typeChangeHandler}
|
||||||
options={typeOptions}
|
options={typeOptions}
|
||||||
onInputChange={setTypeSelectInputValue}
|
onInputChange={setTypeSelectInputValue}
|
||||||
@@ -242,9 +261,9 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
required
|
required
|
||||||
label="Email"
|
label='Email'
|
||||||
name="email"
|
name='email'
|
||||||
placeholder="Masukkan email customer"
|
placeholder='Masukkan email customer'
|
||||||
value={formik.values.email}
|
value={formik.values.email}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
@@ -254,33 +273,36 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
required
|
required
|
||||||
label="Nomor Telepon"
|
label='Nomor Telepon'
|
||||||
name="phone"
|
name='phone'
|
||||||
placeholder="Masukkan nomor telepon customer"
|
placeholder='Masukkan nomor telepon customer'
|
||||||
value={formik.values.phone}
|
value={formik.values.phone}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
isError={formik.touched.phone && Boolean(formik.errors.phone)}
|
isError={formik.touched.phone && Boolean(formik.errors.phone)}
|
||||||
errorMessage={formik.errors.phone}
|
errorMessage={formik.errors.phone}
|
||||||
readOnly={formType === 'detail'}
|
readOnly={formType === 'detail'}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
required
|
required
|
||||||
label="Nomor Rekening"
|
label='Nomor Rekening'
|
||||||
name="account_number"
|
name='account_number'
|
||||||
placeholder="Masukkan nomor rekening customer"
|
placeholder='Masukkan nomor rekening customer'
|
||||||
value={formik.values.account_number}
|
value={formik.values.account_number}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
isError={formik.touched.account_number && Boolean(formik.errors.account_number)}
|
isError={
|
||||||
|
formik.touched.account_number &&
|
||||||
|
Boolean(formik.errors.account_number)
|
||||||
|
}
|
||||||
errorMessage={formik.errors.account_number}
|
errorMessage={formik.errors.account_number}
|
||||||
readOnly={formType === 'detail'}
|
readOnly={formType === 'detail'}
|
||||||
/>
|
/>
|
||||||
<TextArea
|
<TextArea
|
||||||
required
|
required
|
||||||
label="Alamat"
|
label='Alamat'
|
||||||
name="address"
|
name='address'
|
||||||
placeholder="Masukkan alamat customer"
|
placeholder='Masukkan alamat customer'
|
||||||
value={formik.values.address}
|
value={formik.values.address}
|
||||||
onChange={formik.handleChange}
|
onChange={formik.handleChange}
|
||||||
onBlur={formik.handleBlur}
|
onBlur={formik.handleBlur}
|
||||||
@@ -314,7 +336,7 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
<Button
|
<Button
|
||||||
type='button'
|
type='button'
|
||||||
color='warning'
|
color='warning'
|
||||||
href={`/master-data/area/detail/edit/?areaId=${initialValues?.id}`}
|
href={`/master-data/customer/detail/edit/?customerId=${initialValues?.id}`}
|
||||||
className='px-4'
|
className='px-4'
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
@@ -351,6 +373,17 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{customerFormErrorMessage && (
|
||||||
|
<div role='alert' className='alert alert-error'>
|
||||||
|
<Icon
|
||||||
|
icon='material-symbols:error-outline'
|
||||||
|
width={24}
|
||||||
|
height={24}
|
||||||
|
/>
|
||||||
|
<span>{customerFormErrorMessage}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -370,8 +403,8 @@ const CustomerForm = ({formType = 'add', initialValues} : CustomerFormProps) =>
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default CustomerForm;
|
export default CustomerForm;
|
||||||
+2
-2
@@ -5,7 +5,7 @@ export type BaseCustomer = {
|
|||||||
name: string;
|
name: string;
|
||||||
pic_id: number;
|
pic_id: number;
|
||||||
pic: CreatedUser;
|
pic: CreatedUser;
|
||||||
type: 'INDIVIDUAL' | 'BISNIS';
|
type: string;
|
||||||
address: string;
|
address: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
email: string;
|
email: string;
|
||||||
@@ -17,7 +17,7 @@ export type Customer = BaseMetadata & BaseCustomer;
|
|||||||
export type CreateCustomerPayload = {
|
export type CreateCustomerPayload = {
|
||||||
name: string;
|
name: string;
|
||||||
pic_id: number;
|
pic_id: number;
|
||||||
type: 'INDIVIDUAL' | 'BISNIS';
|
type: string;
|
||||||
address: string;
|
address: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
email: string;
|
email: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user