mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +00:00
chore(FE-198): adjust Expense Request Form validation
This commit is contained in:
@@ -3,27 +3,32 @@ import { Expense } from '@/types/api/expense';
|
|||||||
import { formatDate } from '@/lib/helper';
|
import { formatDate } from '@/lib/helper';
|
||||||
|
|
||||||
type ExpenseFormSchemaType = {
|
type ExpenseFormSchemaType = {
|
||||||
|
category?: {
|
||||||
|
value: 'BOP' | 'NON-BOP';
|
||||||
|
label: 'BOP' | 'NON-BOP';
|
||||||
|
};
|
||||||
location?: {
|
location?: {
|
||||||
value: number;
|
value: number;
|
||||||
label: string;
|
label: string;
|
||||||
};
|
};
|
||||||
transaction_date?: string;
|
transaction_date?: string;
|
||||||
kandangs?: { id: number; name: string }[];
|
kandangs?: { id: number; name: string }[];
|
||||||
vendor?: {
|
supplier?: {
|
||||||
value: number;
|
value: number;
|
||||||
label: string;
|
label: string;
|
||||||
};
|
};
|
||||||
existing_documents?: { name: string; url: string }[];
|
existing_documents?: { id: number; name: string; url: string }[];
|
||||||
request_documents?: File[];
|
deleted_documents?: number[];
|
||||||
kandangExpenses: {
|
documents?: File[];
|
||||||
kandangId: number;
|
cost_per_kandangs: {
|
||||||
expenses: {
|
kandang_id: number;
|
||||||
|
cost_items: {
|
||||||
nonstock?: {
|
nonstock?: {
|
||||||
value: number;
|
value: number;
|
||||||
label: string;
|
label: string;
|
||||||
};
|
};
|
||||||
totalQuantity?: number;
|
quantity?: number;
|
||||||
totalExpense?: number;
|
total_cost?: number;
|
||||||
notes?: string;
|
notes?: string;
|
||||||
}[];
|
}[];
|
||||||
}[];
|
}[];
|
||||||
@@ -31,6 +36,11 @@ type ExpenseFormSchemaType = {
|
|||||||
|
|
||||||
export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
||||||
Yup.object({
|
Yup.object({
|
||||||
|
category: Yup.object({
|
||||||
|
value: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
||||||
|
label: Yup.string().oneOf(['BOP', 'NON-BOP']).required(),
|
||||||
|
}).required('Kategori wajib diisi!'),
|
||||||
|
|
||||||
location: Yup.object({
|
location: Yup.object({
|
||||||
value: Yup.number().min(1).required(),
|
value: Yup.number().min(1).required(),
|
||||||
label: Yup.string().required(),
|
label: Yup.string().required(),
|
||||||
@@ -47,35 +57,36 @@ export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
|||||||
.min(1, 'Kandang wajib dipilih!')
|
.min(1, 'Kandang wajib dipilih!')
|
||||||
.required('Kandang wajib dipilih!'),
|
.required('Kandang wajib dipilih!'),
|
||||||
|
|
||||||
vendor: Yup.object({
|
supplier: Yup.object({
|
||||||
value: Yup.number().min(1).required(),
|
value: Yup.number().min(1).required(),
|
||||||
label: Yup.string().required(),
|
label: Yup.string().required(),
|
||||||
}).required('Vendor wajib diisi!'),
|
}).required('Vendor wajib diisi!'),
|
||||||
|
|
||||||
existing_documents: Yup.array().of(
|
existing_documents: Yup.array().of(
|
||||||
Yup.object({
|
Yup.object({
|
||||||
|
id: Yup.number().required(),
|
||||||
name: Yup.string().required(),
|
name: Yup.string().required(),
|
||||||
url: Yup.string().required(),
|
url: Yup.string().required(),
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
|
|
||||||
request_documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
deleted_documents: Yup.array().of(Yup.number().required()).optional(),
|
||||||
|
|
||||||
kandangExpenses: Yup.array()
|
documents: Yup.array().of(Yup.mixed<File>().required()).optional(),
|
||||||
|
|
||||||
|
cost_per_kandangs: Yup.array()
|
||||||
.of(
|
.of(
|
||||||
Yup.object({
|
Yup.object({
|
||||||
kandangId: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
kandang_id: Yup.number().min(1, 'Wajib memilih kandang!').required(),
|
||||||
expenses: Yup.array()
|
cost_items: Yup.array()
|
||||||
.of(
|
.of(
|
||||||
Yup.object({
|
Yup.object({
|
||||||
nonstock: Yup.object({
|
nonstock: Yup.object({
|
||||||
value: Yup.number().min(1).required(),
|
value: Yup.number().min(1).required(),
|
||||||
label: Yup.string().required(),
|
label: Yup.string().required(),
|
||||||
}).required('Nonstock wajib diisi!'),
|
}).required('Nonstock wajib diisi!'),
|
||||||
totalQuantity: Yup.number().required(
|
quantity: Yup.number().required('Total kuantitas wajib diisi!'),
|
||||||
'Total kuantitas wajib diisi!'
|
total_cost: Yup.number().required('Total biaya wajib diisi!'),
|
||||||
),
|
|
||||||
totalExpense: Yup.number().required('Total biaya wajib diisi!'),
|
|
||||||
notes: Yup.string(),
|
notes: Yup.string(),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -90,7 +101,7 @@ export const ExpenseRequestFormSchema: Yup.ObjectSchema<ExpenseFormSchemaType> =
|
|||||||
export const UpdateExpenseRequestFormSchema = ExpenseRequestFormSchema;
|
export const UpdateExpenseRequestFormSchema = ExpenseRequestFormSchema;
|
||||||
|
|
||||||
export const UploadRequestDocumentsFormSchema = Yup.object({
|
export const UploadRequestDocumentsFormSchema = Yup.object({
|
||||||
request_documents: Yup.array().of(Yup.mixed<File>().required()).required(),
|
documents: Yup.array().of(Yup.mixed<File>().required()).required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type ExpenseRequestFormValues = Yup.InferType<
|
export type ExpenseRequestFormValues = Yup.InferType<
|
||||||
@@ -105,39 +116,52 @@ export const getExpenseFormInitialValues = (
|
|||||||
initialValues?: Expense
|
initialValues?: Expense
|
||||||
): ExpenseRequestFormValues => {
|
): ExpenseRequestFormValues => {
|
||||||
return {
|
return {
|
||||||
|
category: initialValues?.category
|
||||||
|
? {
|
||||||
|
value: initialValues.category,
|
||||||
|
label: initialValues.category,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
location: initialValues?.location
|
location: initialValues?.location
|
||||||
? {
|
? {
|
||||||
value: initialValues.location.id,
|
value: initialValues.location.id,
|
||||||
label: initialValues.location.name,
|
label: initialValues.location.name,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
transaction_date: initialValues?.transaction_date
|
transaction_date: initialValues?.expense_date
|
||||||
? formatDate(initialValues.transaction_date, 'YYYY-MM-DD')
|
? formatDate(initialValues.expense_date, 'YYYY-MM-DD')
|
||||||
: undefined,
|
: undefined,
|
||||||
kandangs: initialValues?.kandangs.map((kandang) => ({
|
kandangs: initialValues?.kandangs.map((kandang) => ({
|
||||||
id: kandang.id,
|
id: kandang.kandang_id,
|
||||||
name: kandang.name,
|
name: kandang.name,
|
||||||
})),
|
})),
|
||||||
vendor: initialValues?.vendor
|
supplier: initialValues?.supplier
|
||||||
? {
|
? {
|
||||||
value: initialValues.vendor.id,
|
value: initialValues.supplier.id,
|
||||||
label: initialValues.vendor.name,
|
label: initialValues.supplier.name,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
existing_documents: initialValues?.request_documents,
|
existing_documents: initialValues?.documents?.map((doc) => ({
|
||||||
request_documents: [],
|
id: doc.id,
|
||||||
kandangExpenses: initialValues?.kandang_expenses
|
name: doc.path,
|
||||||
? initialValues.kandang_expenses.map((kandangExpense) => ({
|
url: doc.path,
|
||||||
kandangId: kandangExpense.kandang.id,
|
})),
|
||||||
expenses: kandangExpense.expenses.map((expenseItem) => ({
|
deleted_documents: [],
|
||||||
nonstock: {
|
documents: [],
|
||||||
value: expenseItem.nonstock.id,
|
cost_per_kandangs: initialValues?.kandangs
|
||||||
label: expenseItem.nonstock.name,
|
? initialValues.kandangs.map((kandangExpense) => ({
|
||||||
},
|
kandang_id: kandangExpense.kandang_id,
|
||||||
totalQuantity: expenseItem.total_quantity,
|
cost_items: kandangExpense.pengajuans
|
||||||
totalExpense: expenseItem.total_expense,
|
? kandangExpense.pengajuans.map((expenseItem) => ({
|
||||||
notes: expenseItem.notes,
|
nonstock: {
|
||||||
})),
|
value: expenseItem.nonstock.id,
|
||||||
|
label: expenseItem.nonstock.name,
|
||||||
|
},
|
||||||
|
quantity: expenseItem.qty,
|
||||||
|
total_cost: expenseItem.total_price,
|
||||||
|
notes: expenseItem.note,
|
||||||
|
}))
|
||||||
|
: [],
|
||||||
}))
|
}))
|
||||||
: [],
|
: [],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user