mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE-208,213): simplify PurchaseOrderForm and PurchaseOrderStaffApprovalForm by removing unused product and warehouse fields and enhancing price validation
This commit is contained in:
@@ -4,15 +4,7 @@ import { Purchase } from '@/types/api/purchase/purchase';
|
|||||||
type PurchaseRequestStaffApprovalFormSchemaType = {
|
type PurchaseRequestStaffApprovalFormSchemaType = {
|
||||||
notes: string | null;
|
notes: string | null;
|
||||||
items: {
|
items: {
|
||||||
product?: {
|
|
||||||
value: number;
|
|
||||||
label: string;
|
|
||||||
} | null;
|
|
||||||
product_id: number;
|
product_id: number;
|
||||||
warehouse?: {
|
|
||||||
value: number;
|
|
||||||
label: string;
|
|
||||||
} | null;
|
|
||||||
warehouse_id: number;
|
warehouse_id: number;
|
||||||
qty: number;
|
qty: number;
|
||||||
price: number | string;
|
price: number | string;
|
||||||
@@ -53,15 +45,7 @@ type PurchaseRequestAcceptApprovalFormSchemaType = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type PurchaseStaffApprovalItemSchema = {
|
export type PurchaseStaffApprovalItemSchema = {
|
||||||
product?: {
|
|
||||||
value: number;
|
|
||||||
label: string;
|
|
||||||
} | null;
|
|
||||||
product_id: number;
|
product_id: number;
|
||||||
warehouse?: {
|
|
||||||
value: number;
|
|
||||||
label: string;
|
|
||||||
} | null;
|
|
||||||
warehouse_id: number;
|
warehouse_id: number;
|
||||||
qty: number;
|
qty: number;
|
||||||
price: number | string;
|
price: number | string;
|
||||||
@@ -99,18 +83,10 @@ export type PurchaseDeleteItemsSchema = {
|
|||||||
|
|
||||||
const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> =
|
const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffApprovalItemSchema> =
|
||||||
Yup.object({
|
Yup.object({
|
||||||
product: Yup.object({
|
|
||||||
value: Yup.number().min(1).required(),
|
|
||||||
label: Yup.string().required(),
|
|
||||||
}).nullable(),
|
|
||||||
product_id: Yup.number()
|
product_id: Yup.number()
|
||||||
.required('Produk wajib diisi!')
|
.required('Produk wajib diisi!')
|
||||||
.min(1, 'Produk wajib diisi!')
|
.min(1, 'Produk wajib diisi!')
|
||||||
.typeError('Produk wajib diisi!'),
|
.typeError('Produk wajib diisi!'),
|
||||||
warehouse: Yup.object({
|
|
||||||
value: Yup.number().min(1).required(),
|
|
||||||
label: Yup.string().required(),
|
|
||||||
}).nullable(),
|
|
||||||
warehouse_id: Yup.number()
|
warehouse_id: Yup.number()
|
||||||
.required('Gudang wajib diisi!')
|
.required('Gudang wajib diisi!')
|
||||||
.min(1, 'Gudang wajib diisi!')
|
.min(1, 'Gudang wajib diisi!')
|
||||||
@@ -119,16 +95,34 @@ const PurchaseStaffApprovalItemObjectSchema: Yup.ObjectSchema<PurchaseStaffAppro
|
|||||||
.required('Jumlah wajib diisi!')
|
.required('Jumlah wajib diisi!')
|
||||||
.min(1, 'Jumlah harus berupa angka lebih dari 0!')
|
.min(1, 'Jumlah harus berupa angka lebih dari 0!')
|
||||||
.typeError('Jumlah harus berupa angka lebih dari 0!'),
|
.typeError('Jumlah harus berupa angka lebih dari 0!'),
|
||||||
price: Yup.number()
|
price: Yup.mixed<number | string>()
|
||||||
.required('Harga wajib diisi!')
|
.required('Harga wajib diisi!')
|
||||||
.min(0, 'Harga harus berupa angka lebih dari atau sama dengan 0!')
|
.test(
|
||||||
.typeError('Harga harus berupa angka lebih dari atau sama dengan 0!'),
|
'is-valid-price',
|
||||||
total_price: Yup.number()
|
'Harga harus berupa angka lebih dari atau sama dengan 0!',
|
||||||
|
function (value) {
|
||||||
|
if (value === '' || value === null || value === undefined)
|
||||||
|
return false;
|
||||||
|
const numValue =
|
||||||
|
typeof value === 'string' ? parseFloat(value) : value;
|
||||||
|
return !isNaN(numValue) && numValue >= 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.typeError('Harga harus berupa angka!'),
|
||||||
|
total_price: Yup.mixed<number | string>()
|
||||||
.required('Total harga wajib diisi!')
|
.required('Total harga wajib diisi!')
|
||||||
.min(0, 'Total harga harus berupa angka lebih dari atau sama dengan 0!')
|
.test(
|
||||||
.typeError(
|
'is-valid-total-price',
|
||||||
'Total harga harus berupa angka lebih dari atau sama dengan 0!'
|
'Total harga harus berupa angka lebih dari atau sama dengan 0!',
|
||||||
),
|
function (value) {
|
||||||
|
if (value === '' || value === null || value === undefined)
|
||||||
|
return false;
|
||||||
|
const numValue =
|
||||||
|
typeof value === 'string' ? parseFloat(value) : value;
|
||||||
|
return !isNaN(numValue) && numValue >= 0;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.typeError('Total harga harus berupa angka!'),
|
||||||
});
|
});
|
||||||
|
|
||||||
const PurchaseManagerApprovalObjectSchema: Yup.ObjectSchema<PurchaseRequestManagerApprovalFormSchemaType> =
|
const PurchaseManagerApprovalObjectSchema: Yup.ObjectSchema<PurchaseRequestManagerApprovalFormSchemaType> =
|
||||||
@@ -271,9 +265,7 @@ export const PurchaseRequestStaffApprovalFormInitialValues: PurchaseRequestStaff
|
|||||||
notes: '',
|
notes: '',
|
||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
product: null,
|
|
||||||
product_id: 0,
|
product_id: 0,
|
||||||
warehouse: null,
|
|
||||||
warehouse_id: 0,
|
warehouse_id: 0,
|
||||||
qty: 0,
|
qty: 0,
|
||||||
price: '',
|
price: '',
|
||||||
@@ -289,9 +281,7 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = (
|
|||||||
notes: purchase?.notes ?? null,
|
notes: purchase?.notes ?? null,
|
||||||
items: purchase?.items
|
items: purchase?.items
|
||||||
? purchase.items.map((item) => ({
|
? purchase.items.map((item) => ({
|
||||||
product: null,
|
|
||||||
product_id: item.product_id,
|
product_id: item.product_id,
|
||||||
warehouse: null,
|
|
||||||
warehouse_id: item.warehouse.id,
|
warehouse_id: item.warehouse.id,
|
||||||
qty: item.qty,
|
qty: item.qty,
|
||||||
price: item.price,
|
price: item.price,
|
||||||
@@ -299,9 +289,7 @@ export const PurchaseRequestStaffApprovalFormDefaultValues = (
|
|||||||
}))
|
}))
|
||||||
: [
|
: [
|
||||||
{
|
{
|
||||||
product: null,
|
|
||||||
product_id: 0,
|
product_id: 0,
|
||||||
warehouse: null,
|
|
||||||
warehouse_id: 0,
|
warehouse_id: 0,
|
||||||
qty: 0,
|
qty: 0,
|
||||||
price: '',
|
price: '',
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { useSearchParams } from 'next/navigation';
|
|||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
import TextInput from '@/components/input/TextInput';
|
import TextInput from '@/components/input/TextInput';
|
||||||
import NumberInput from '@/components/input/NumberInput';
|
import NumberInput from '@/components/input/NumberInput';
|
||||||
import { OptionType } from '@/components/input/SelectInput';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
PurchaseRequestStaffApprovalFormDefaultValues,
|
PurchaseRequestStaffApprovalFormDefaultValues,
|
||||||
@@ -37,22 +36,6 @@ const PurchaseOrderStaffApprovalForm = ({
|
|||||||
const [purchaseOrderFormErrorMessage, setPurchaseOrderFormErrorMessage] =
|
const [purchaseOrderFormErrorMessage, setPurchaseOrderFormErrorMessage] =
|
||||||
useState('');
|
useState('');
|
||||||
|
|
||||||
// ===== TYPE DEFINITIONS =====
|
|
||||||
interface PurchaseItemOptionType extends OptionType {
|
|
||||||
id: number;
|
|
||||||
quantity: number;
|
|
||||||
product: {
|
|
||||||
name: string;
|
|
||||||
type?: string;
|
|
||||||
uom?: {
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
warehouse: {
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== UTILITY FUNCTIONS =====
|
// ===== UTILITY FUNCTIONS =====
|
||||||
const getPurchaseItemError = (
|
const getPurchaseItemError = (
|
||||||
idx: number,
|
idx: number,
|
||||||
@@ -138,7 +121,9 @@ const PurchaseOrderStaffApprovalForm = ({
|
|||||||
items: purchaseItems.map((purchaseItem, idx) => {
|
items: purchaseItems.map((purchaseItem, idx) => {
|
||||||
const formItem = values.items?.[idx];
|
const formItem = values.items?.[idx];
|
||||||
return {
|
return {
|
||||||
purchase_item_id: purchaseItem.value,
|
product_id: purchaseItem.product_id || 0,
|
||||||
|
warehouse_id: purchaseItem.warehouse_id || 0,
|
||||||
|
qty: purchaseItem.quantity || 0,
|
||||||
price:
|
price:
|
||||||
typeof formItem?.price === 'string'
|
typeof formItem?.price === 'string'
|
||||||
? parseFloat(formItem.price) || 0
|
? parseFloat(formItem.price) || 0
|
||||||
@@ -170,9 +155,11 @@ const PurchaseOrderStaffApprovalForm = ({
|
|||||||
if (initialValues?.items) {
|
if (initialValues?.items) {
|
||||||
return initialValues.items.map((item) => ({
|
return initialValues.items.map((item) => ({
|
||||||
value: item.id,
|
value: item.id,
|
||||||
label: `${item.product.name} ${item.sub_qty}`,
|
label: item.product.name,
|
||||||
id: item.id,
|
id: item.id,
|
||||||
quantity: item.sub_qty,
|
quantity: item.sub_qty,
|
||||||
|
product_id: item.product_id,
|
||||||
|
warehouse_id: item.warehouse.id,
|
||||||
product: {
|
product: {
|
||||||
name: item.product.name,
|
name: item.product.name,
|
||||||
product_category: item.product.product_category,
|
product_category: item.product.product_category,
|
||||||
@@ -187,11 +174,12 @@ const PurchaseOrderStaffApprovalForm = ({
|
|||||||
return [];
|
return [];
|
||||||
}, [initialValues?.items]);
|
}, [initialValues?.items]);
|
||||||
|
|
||||||
// Ensure form values are properly set when purchaseItems change
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (purchaseItems.length > 0) {
|
if (purchaseItems.length > 0) {
|
||||||
const updatedItems = purchaseItems.map((purchaseItem) => ({
|
const updatedItems = purchaseItems.map((purchaseItem) => ({
|
||||||
purchase_item_id: purchaseItem.value,
|
product_id: purchaseItem.product_id || 0,
|
||||||
|
warehouse_id: purchaseItem.warehouse_id || 0,
|
||||||
|
qty: purchaseItem.quantity || 0,
|
||||||
price: '',
|
price: '',
|
||||||
total_price: '',
|
total_price: '',
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user