mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +00:00
refactor(FE-formik-usage): Refactor MarketingFilter form values and
handlers
This commit is contained in:
@@ -10,6 +10,10 @@ import SelectInput, {
|
|||||||
useSelect,
|
useSelect,
|
||||||
} from '@/components/input/SelectInput';
|
} from '@/components/input/SelectInput';
|
||||||
import { MARKETING_APPROVAL_LINE } from '@/config/approval-line';
|
import { MARKETING_APPROVAL_LINE } from '@/config/approval-line';
|
||||||
|
import {
|
||||||
|
MarketingFilterFormValues,
|
||||||
|
MarketingFilterSchema,
|
||||||
|
} from '@/components/pages/marketing/filter/MarketingFilter';
|
||||||
import { MarketingFilter } from '@/types/api/marketing/marketing';
|
import { MarketingFilter } from '@/types/api/marketing/marketing';
|
||||||
import SelectInputCheckbox from '@/components/input/SelectInputCheckbox';
|
import SelectInputCheckbox from '@/components/input/SelectInputCheckbox';
|
||||||
import { MarketingApi } from '@/services/api/marketing/marketing';
|
import { MarketingApi } from '@/services/api/marketing/marketing';
|
||||||
@@ -70,8 +74,8 @@ const MarketingFilterModal = ({
|
|||||||
limit: 'limit',
|
limit: 'limit',
|
||||||
});
|
});
|
||||||
|
|
||||||
const uniqueCustomersOptions = useMemo(() => {
|
const salesCustomerOptions = useMemo(() => {
|
||||||
const seen = new Set();
|
const seen = new Set<string | number>();
|
||||||
return customersOptions.filter((customer) => {
|
return customersOptions.filter((customer) => {
|
||||||
if (seen.has(customer.value)) return false;
|
if (seen.has(customer.value)) return false;
|
||||||
seen.add(customer.value);
|
seen.add(customer.value);
|
||||||
@@ -87,23 +91,19 @@ const MarketingFilterModal = ({
|
|||||||
{ value: 'DITOLAK', label: 'Ditolak' },
|
{ value: 'DITOLAK', label: 'Ditolak' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const formik = useFormik<{
|
const formik = useFormik<MarketingFilterFormValues>({
|
||||||
product_ids: OptionType[];
|
|
||||||
status: OptionType | null;
|
|
||||||
customer_id: OptionType | null;
|
|
||||||
}>({
|
|
||||||
initialValues: {
|
initialValues: {
|
||||||
product_ids: [],
|
product_ids: [],
|
||||||
status: null,
|
status: null,
|
||||||
customer_id: null,
|
customer: null,
|
||||||
},
|
},
|
||||||
|
validationSchema: MarketingFilterSchema,
|
||||||
|
|
||||||
onSubmit: async (values) => {
|
onSubmit: async (values) => {
|
||||||
const formattedValues = {
|
const formattedValues: MarketingFilter = {
|
||||||
...values,
|
|
||||||
product_ids: values.product_ids.map((item) => Number(item.value)),
|
product_ids: values.product_ids.map((item) => Number(item.value)),
|
||||||
status: values.status?.value.toString() || '',
|
status: values.status?.value.toString() || '',
|
||||||
customer_id: Number(values.customer_id?.value),
|
customer_id: Number(values.customer?.value),
|
||||||
};
|
};
|
||||||
|
|
||||||
onSubmit?.(formattedValues);
|
onSubmit?.(formattedValues);
|
||||||
@@ -121,7 +121,10 @@ const MarketingFilterModal = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const customerChangeHandler = (val: OptionType | OptionType[] | null) => {
|
const customerChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
formik.setFieldValue('customer_id', val as OptionType);
|
formik.setFieldValue(
|
||||||
|
'customer',
|
||||||
|
!Array.isArray(val) ? (val as OptionType<number> | null) : null
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const statusChangeHandler = (val: OptionType | OptionType[] | null) => {
|
const statusChangeHandler = (val: OptionType | OptionType[] | null) => {
|
||||||
@@ -187,9 +190,9 @@ const MarketingFilterModal = ({
|
|||||||
label='Customer'
|
label='Customer'
|
||||||
isClearable
|
isClearable
|
||||||
placeholder='Pilih customer'
|
placeholder='Pilih customer'
|
||||||
options={uniqueCustomersOptions}
|
options={salesCustomerOptions}
|
||||||
isLoading={isLoadingCustomersOptions}
|
isLoading={isLoadingCustomersOptions}
|
||||||
value={formik.values.customer_id}
|
value={formik.values.customer}
|
||||||
onChange={customerChangeHandler}
|
onChange={customerChangeHandler}
|
||||||
onInputChange={setCustomersInputValue}
|
onInputChange={setCustomersInputValue}
|
||||||
onMenuScrollToBottom={loadMoreCustomers}
|
onMenuScrollToBottom={loadMoreCustomers}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { array, mixed, object } from 'yup';
|
||||||
|
import { OptionType } from '@/components/input/SelectInput';
|
||||||
|
|
||||||
|
export const MarketingFilterSchema = object({
|
||||||
|
product_ids: array().of(mixed<OptionType<number>>().required()).required(),
|
||||||
|
status: mixed<OptionType<string>>().nullable(),
|
||||||
|
customer: mixed<OptionType<number>>().nullable(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type MarketingFilterFormValues = {
|
||||||
|
product_ids: OptionType<number>[];
|
||||||
|
status: OptionType<string> | null;
|
||||||
|
customer: OptionType<number> | null;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user