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