mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
refactor(FE-212,213): unify purchase API service references in Purchase components
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import useSWR from 'swr';
|
||||
import PurchaseRequestForm from '@/components/pages/purchase/form/request/PurchaseRequestForm';
|
||||
import { PurchaseRequestApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
import { isResponseSuccess, isResponseError } from '@/lib/api-helper';
|
||||
|
||||
const PurchaseEdit = () => {
|
||||
@@ -14,7 +14,7 @@ const PurchaseEdit = () => {
|
||||
|
||||
const { data: purchase, isLoading: isLoadingPurchase } = useSWR(
|
||||
purchaseId,
|
||||
(id: number) => PurchaseRequestApi.getSingle(id)
|
||||
(id: number) => PurchaseApi.getSingle(id)
|
||||
);
|
||||
|
||||
if (!purchaseId) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import useSWR from 'swr';
|
||||
import PurchaseOrderDetail from '@/components/pages/purchase/order/PurchaseOrderDetail';
|
||||
import { PurchaseRequestApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
import { isResponseSuccess, isResponseError } from '@/lib/api-helper';
|
||||
|
||||
const PurchaseDetail = () => {
|
||||
@@ -14,7 +14,7 @@ const PurchaseDetail = () => {
|
||||
|
||||
const { data: purchase, isLoading: isLoadingPurchase } = useSWR(
|
||||
purchaseId,
|
||||
(id: number) => PurchaseRequestApi.getSingle(id)
|
||||
(id: number) => PurchaseApi.getSingle(id)
|
||||
);
|
||||
|
||||
if (!purchaseId) {
|
||||
|
||||
@@ -22,7 +22,7 @@ import { isResponseSuccess } from '@/lib/api-helper';
|
||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||
import { ROWS_OPTIONS } from '@/config/constant';
|
||||
import { Purchase } from '@/types/api/purchase/purchase';
|
||||
import { PurchaseRequestApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
|
||||
// ===== INTERFACES =====
|
||||
interface RowOptionsMenuProps {
|
||||
@@ -110,8 +110,8 @@ const PurchaseTable = () => {
|
||||
isLoading,
|
||||
mutate: refreshPurchaseRequests,
|
||||
} = useSWR(
|
||||
`${PurchaseRequestApi.basePath}${getTableFilterQueryString()}`,
|
||||
PurchaseRequestApi.getAllFetcher
|
||||
`${PurchaseApi.basePath}${getTableFilterQueryString()}`,
|
||||
PurchaseApi.getAllFetcher
|
||||
);
|
||||
|
||||
// ===== TABLE COLUMNS DEFINITION =====
|
||||
@@ -208,7 +208,7 @@ const PurchaseTable = () => {
|
||||
setIsDeleteLoading(true);
|
||||
|
||||
try {
|
||||
await PurchaseRequestApi.delete(selectedPurchase?.id as number);
|
||||
await PurchaseApi.delete(selectedPurchase?.id as number);
|
||||
refreshPurchaseRequests();
|
||||
deleteModal.closeModal();
|
||||
toast.success('Berhasil menghapus data permintaan pembelian!');
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
PurchaseRequestAcceptApprovalFormSchema,
|
||||
} from './PurchaseOrderForm.schema';
|
||||
import { isResponseError } from '@/lib/api-helper';
|
||||
import { AcceptApprovalApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
import {
|
||||
CreateAcceptApprovalRequestPayload,
|
||||
Purchase,
|
||||
@@ -93,7 +93,7 @@ const PurchaseOrderAcceptApprovalForm = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await AcceptApprovalApi.acceptApproval(
|
||||
const res = await PurchaseApi.acceptApproval.create(
|
||||
purchaseRequestId,
|
||||
payload
|
||||
);
|
||||
@@ -113,7 +113,7 @@ const PurchaseOrderAcceptApprovalForm = ({
|
||||
|
||||
const updateAcceptApprovalHandler = useCallback(
|
||||
async (purchaseId: number, payload: CreateAcceptApprovalRequestPayload) => {
|
||||
const res = await AcceptApprovalApi.acceptApproval(purchaseId, payload);
|
||||
const res = await PurchaseApi.acceptApproval.create(purchaseId, payload);
|
||||
if (isResponseError(res)) {
|
||||
setPurchaseOrderFormErrorMessage(res.message);
|
||||
return;
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
PurchaseRequestStaffApprovalFormSchema,
|
||||
} from './PurchaseOrderForm.schema';
|
||||
import { isResponseError } from '@/lib/api-helper';
|
||||
import { StaffApprovalApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
import {
|
||||
CreateStaffApprovalRequestPayload,
|
||||
UpdateStaffApprovalRequestPayload,
|
||||
@@ -84,7 +84,7 @@ const PurchaseOrderStaffApprovalForm = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await StaffApprovalApi.createStaffApproval(
|
||||
const res = await PurchaseApi.staffApproval.create(
|
||||
purchaseRequestId,
|
||||
payload
|
||||
);
|
||||
@@ -104,7 +104,7 @@ const PurchaseOrderStaffApprovalForm = ({
|
||||
|
||||
const updateStaffApprovalHandler = useCallback(
|
||||
async (purchaseId: number, payload: UpdateStaffApprovalRequestPayload) => {
|
||||
const res = await StaffApprovalApi.updateStaffApproval(
|
||||
const res = await PurchaseApi.staffApproval.update(
|
||||
purchaseId,
|
||||
payload
|
||||
);
|
||||
|
||||
@@ -33,7 +33,7 @@ import {
|
||||
import { Supplier } from '@/types/api/master-data/supplier';
|
||||
import { Product } from '@/types/api/master-data/product';
|
||||
import { isResponseSuccess, isResponseError } from '@/lib/api-helper';
|
||||
import { PurchaseRequestApi } from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
|
||||
import Card from '@/components/Card';
|
||||
import {
|
||||
@@ -99,7 +99,7 @@ const PurchaseRequestForm = ({
|
||||
// ===== SUBMISSION HANDLERS =====
|
||||
const createPurchaseRequestHandler = useCallback(
|
||||
async (payload: CreatePurchaseRequestPayload) => {
|
||||
const res = await PurchaseRequestApi.create(payload);
|
||||
const res = await PurchaseApi.create(payload);
|
||||
if (isResponseError(res)) {
|
||||
setPurchaseRequestFormErrorMessage(res.message);
|
||||
return;
|
||||
@@ -115,7 +115,7 @@ const PurchaseRequestForm = ({
|
||||
purchaseRequestId: number,
|
||||
payload: CreatePurchaseRequestPayload
|
||||
) => {
|
||||
const res = await PurchaseRequestApi.update(purchaseRequestId, payload);
|
||||
const res = await PurchaseApi.update(purchaseRequestId, payload);
|
||||
if (isResponseError(res)) {
|
||||
setPurchaseRequestFormErrorMessage(res.message);
|
||||
return;
|
||||
@@ -135,7 +135,7 @@ const PurchaseRequestForm = ({
|
||||
if (!initialValues?.id) return;
|
||||
|
||||
setIsDeleteLoading(true);
|
||||
await PurchaseRequestApi.delete(initialValues.id);
|
||||
await PurchaseApi.delete(initialValues.id);
|
||||
deleteModal.closeModal();
|
||||
toast.success('Successfully delete Purchase Request!');
|
||||
setIsDeleteLoading(false);
|
||||
|
||||
@@ -26,10 +26,7 @@ import {
|
||||
Purchase,
|
||||
PurchaseItem,
|
||||
} from '@/types/api/purchase/purchase';
|
||||
import {
|
||||
ManagerApprovalApi,
|
||||
PurchaseDeleteItemsApi,
|
||||
} from '@/services/api/purchase';
|
||||
import { PurchaseApi } from '@/services/api/purchase';
|
||||
import { isResponseError } from '@/lib/api-helper';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
@@ -230,7 +227,7 @@ const PurchaseOrderDetail = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await ManagerApprovalApi.createManagerApproval(
|
||||
const res = await PurchaseApi.managerApproval.create(
|
||||
purchaseRequestId,
|
||||
payload
|
||||
);
|
||||
@@ -276,7 +273,7 @@ const PurchaseOrderDetail = ({
|
||||
setIsDeleteLoading(true);
|
||||
|
||||
try {
|
||||
const res = await PurchaseDeleteItemsApi.deleteItems(purchaseRequestId, {
|
||||
const res = await PurchaseApi.items.delete(purchaseRequestId, {
|
||||
item_ids: itemIdsToDelete,
|
||||
});
|
||||
|
||||
|
||||
+64
-100
@@ -11,123 +11,87 @@ import {
|
||||
import { BaseApiService } from '@/services/api/base';
|
||||
import { BaseApiResponse } from '@/types/api/api-general';
|
||||
|
||||
export const PurchaseRequestApi = new BaseApiService<
|
||||
const basePurchaseApi = new BaseApiService<
|
||||
Purchase,
|
||||
CreatePurchaseRequestPayload,
|
||||
UpdatePurchaseRequestPayload
|
||||
>('/purchases');
|
||||
|
||||
export class StaffApprovalService extends BaseApiService<
|
||||
Purchase,
|
||||
CreateStaffApprovalRequestPayload,
|
||||
UpdateStaffApprovalRequestPayload
|
||||
> {
|
||||
constructor(basePath: string = '') {
|
||||
super(basePath);
|
||||
}
|
||||
export const PurchaseApi = {
|
||||
basePath: basePurchaseApi.basePath,
|
||||
header: basePurchaseApi.header,
|
||||
getAllFetcher: basePurchaseApi.getAllFetcher.bind(basePurchaseApi),
|
||||
getSingle: basePurchaseApi.getSingle.bind(basePurchaseApi),
|
||||
create: basePurchaseApi.create.bind(basePurchaseApi),
|
||||
update: basePurchaseApi.update.bind(basePurchaseApi),
|
||||
delete: basePurchaseApi.delete.bind(basePurchaseApi),
|
||||
customRequest: basePurchaseApi.customRequest.bind(basePurchaseApi),
|
||||
|
||||
async createStaffApproval(
|
||||
purchaseRequestId: number,
|
||||
payload: CreateStaffApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.customRequest<BaseApiResponse<{ message: string }>>(
|
||||
`${purchaseRequestId}/approvals/staff`,
|
||||
{
|
||||
staffApproval: {
|
||||
create: async (
|
||||
purchaseRequestId: number,
|
||||
payload: CreateStaffApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
|
||||
return await basePurchaseApi.customRequest<
|
||||
BaseApiResponse<{ message: string }>
|
||||
>(`${purchaseRequestId}/approvals/staff`, {
|
||||
method: 'POST',
|
||||
payload,
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
async updateStaffApproval(
|
||||
purchaseRequestId: number,
|
||||
payload: UpdateStaffApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.customRequest<BaseApiResponse<{ message: string }>>(
|
||||
`${purchaseRequestId}/approvals/staff`,
|
||||
{
|
||||
update: async (
|
||||
purchaseRequestId: number,
|
||||
payload: UpdateStaffApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
|
||||
return await basePurchaseApi.customRequest<
|
||||
BaseApiResponse<{ message: string }>
|
||||
>(`${purchaseRequestId}/approvals/staff`, {
|
||||
method: 'POST',
|
||||
payload,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
export class ManagerApprovalService extends BaseApiService<
|
||||
Purchase,
|
||||
CreateManagerApprovalRequestPayload,
|
||||
CreateManagerApprovalRequestPayload
|
||||
> {
|
||||
constructor(basePath: string = '') {
|
||||
super(basePath);
|
||||
}
|
||||
|
||||
async createManagerApproval(
|
||||
purchaseRequestId: number,
|
||||
payload: CreateManagerApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.customRequest<BaseApiResponse<{ message: string }>>(
|
||||
`${purchaseRequestId}/approvals/manager`,
|
||||
{
|
||||
managerApproval: {
|
||||
create: async (
|
||||
purchaseRequestId: number,
|
||||
payload: CreateManagerApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
|
||||
return await basePurchaseApi.customRequest<
|
||||
BaseApiResponse<{ message: string }>
|
||||
>(`${purchaseRequestId}/approvals/manager`, {
|
||||
method: 'POST',
|
||||
payload,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
export class AcceptApprovalService extends BaseApiService<
|
||||
Purchase,
|
||||
CreateAcceptApprovalRequestPayload,
|
||||
unknown
|
||||
> {
|
||||
constructor(basePath: string = '') {
|
||||
super(basePath);
|
||||
}
|
||||
|
||||
async acceptApproval(
|
||||
purchaseRequestId: number,
|
||||
payload: CreateAcceptApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.customRequest<BaseApiResponse<{ message: string }>>(
|
||||
`${purchaseRequestId}/receipts`,
|
||||
{
|
||||
acceptApproval: {
|
||||
create: async (
|
||||
purchaseRequestId: number,
|
||||
payload: CreateAcceptApprovalRequestPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
|
||||
return await basePurchaseApi.customRequest<
|
||||
BaseApiResponse<{ message: string }>
|
||||
>(`${purchaseRequestId}/receipts`, {
|
||||
method: 'POST',
|
||||
payload,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
|
||||
export class PurchaseDeleteItemsService extends BaseApiService<
|
||||
Purchase,
|
||||
DeletePurchaseRequestItemPayload,
|
||||
unknown
|
||||
> {
|
||||
constructor(basePath: string = '') {
|
||||
super(basePath);
|
||||
}
|
||||
|
||||
async deleteItems(
|
||||
purchaseRequestId: number,
|
||||
payload: DeletePurchaseRequestItemPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> {
|
||||
return await this.customRequest<BaseApiResponse<{ message: string }>>(
|
||||
`${purchaseRequestId}/items`,
|
||||
{
|
||||
items: {
|
||||
delete: async (
|
||||
purchaseRequestId: number,
|
||||
payload: DeletePurchaseRequestItemPayload
|
||||
): Promise<BaseApiResponse<{ message: string }> | undefined> => {
|
||||
return await basePurchaseApi.customRequest<
|
||||
BaseApiResponse<{ message: string }>
|
||||
>(`${purchaseRequestId}/items`, {
|
||||
method: 'DELETE',
|
||||
payload,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const StaffApprovalApi = new StaffApprovalService('/purchases');
|
||||
|
||||
export const ManagerApprovalApi = new ManagerApprovalService('/purchases');
|
||||
|
||||
export const AcceptApprovalApi = new AcceptApprovalService('/purchases');
|
||||
|
||||
export const PurchaseDeleteItemsApi = new PurchaseDeleteItemsService(
|
||||
'/purchases'
|
||||
);
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user