mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +00:00
refactor(FE-88): memisahkan file api project flock & penyesuaian tipe data dan paylod dengan BE
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import ProjectFlockForm from '@/components/pages/production/project-flock/form/ProjectFlockForm';
|
||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
||||
import { ProjectFlockApi } from '@/services/api/production';
|
||||
import { ProjectFlockApi } from '@/services/api/production/project-flock';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import useSWR from 'swr';
|
||||
|
||||
@@ -12,10 +12,11 @@ const ProjectFlockEdit = () => {
|
||||
|
||||
const projectFlockId = searchParams.get('projectFlockId');
|
||||
|
||||
const { data: projectFlock, isLoading: isLoadingCostumer } = useSWR(
|
||||
projectFlockId,
|
||||
(id: number) => ProjectFlockApi.getSingle(id)
|
||||
);
|
||||
const {
|
||||
data: projectFlock,
|
||||
isLoading: isLoadingProjectFlock,
|
||||
mutate: refreshProjectFlocks,
|
||||
} = useSWR(projectFlockId, (id: number) => ProjectFlockApi.getSingle(id));
|
||||
|
||||
if (!projectFlockId) {
|
||||
router.back();
|
||||
@@ -27,18 +28,25 @@ const ProjectFlockEdit = () => {
|
||||
);
|
||||
}
|
||||
|
||||
if (!isLoadingCostumer && (!projectFlock || isResponseError(projectFlock))) {
|
||||
if (
|
||||
!isLoadingProjectFlock &&
|
||||
(!projectFlock || isResponseError(projectFlock))
|
||||
) {
|
||||
router.replace('/404');
|
||||
return;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='w-full p-4 flex flex-row justify-center'>
|
||||
{isLoadingCostumer && (
|
||||
<div className='w-full p-4 flex flex-col justify-center'>
|
||||
{isLoadingProjectFlock && (
|
||||
<span className='loading loading-spinner loading-xl' />
|
||||
)}
|
||||
{!isLoadingCostumer && isResponseSuccess(projectFlock) && (
|
||||
<ProjectFlockForm formType='edit' initialValues={projectFlock.data} />
|
||||
{!isLoadingProjectFlock && isResponseSuccess(projectFlock) && (
|
||||
<>
|
||||
{JSON.stringify(projectFlock.data)}
|
||||
|
||||
<ProjectFlockForm formType='edit' initialValues={projectFlock.data} />
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import ProjectFlockForm from '@/components/pages/production/project-flock/form/ProjectFlockForm';
|
||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
||||
import { FlockApi } from '@/services/api/master-data';
|
||||
import { ProjectFlockApi } from '@/services/api/production';
|
||||
import { ProjectFlockApi } from '@/services/api/production/project-flock';
|
||||
import { ProjectFlock } from '@/types/api/production/project-flock';
|
||||
import { useRouter, useSearchParams } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
@@ -23,12 +23,13 @@ const ProjectFlockDetail = () => {
|
||||
mutate: refreshProjectFlock,
|
||||
} = useSWR(projectFlockId, (id: number) => ProjectFlockApi.getSingle(id));
|
||||
|
||||
const flockUrl = `${FlockApi.basePath}`;
|
||||
const {
|
||||
data: flock,
|
||||
isLoading: isLoadingFlock,
|
||||
mutate: refreshFlock,
|
||||
} = useSWR(flockUrl, FlockApi.getAllFetcher);
|
||||
data: approvalLines,
|
||||
isLoading: isLoadingApprovalLines,
|
||||
mutate: refreshApprovalLines,
|
||||
} = useSWR('approvals', (id: number) =>
|
||||
ProjectFlockApi.getApprovalLines((projectFlockId ?? 0) as number)
|
||||
);
|
||||
|
||||
if (!projectFlockId) {
|
||||
router.back();
|
||||
@@ -48,37 +49,17 @@ const ProjectFlockDetail = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Attach flock id to project flock
|
||||
let projectFlockAttached: ProjectFlock | undefined;
|
||||
|
||||
if (isResponseSuccess(projectFlock) && isResponseSuccess(flock)) {
|
||||
projectFlockAttached = {
|
||||
...projectFlock.data,
|
||||
flock: flock.data.find(
|
||||
(flock) =>
|
||||
flock.name ==
|
||||
projectFlock?.data?.flock_name
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.slice(0, -1)
|
||||
.join(' ')
|
||||
),
|
||||
};
|
||||
console.log('projectFlockAttached');
|
||||
console.log(projectFlockAttached);
|
||||
console.log('flocks');
|
||||
console.log(flock.data);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='w-full p-4 flex flex-row justify-center'>
|
||||
{isLoadingProjectFlock && (
|
||||
<span className='loading loading-spinner loading-xl' />
|
||||
)}
|
||||
{projectFlockAttached && (
|
||||
<div className='w-full p-4 flex flex-col justify-center'>
|
||||
{isLoadingProjectFlock ||
|
||||
(isLoadingApprovalLines && (
|
||||
<span className='loading loading-spinner loading-xl' />
|
||||
))}
|
||||
{isResponseSuccess(projectFlock) && isResponseSuccess(approvalLines) && (
|
||||
<ProjectFlockForm
|
||||
formType='detail'
|
||||
initialValues={projectFlockAttached}
|
||||
initialValues={projectFlock.data}
|
||||
initialApprovals={approvalLines.data}
|
||||
refreshProjectFlocks={refreshProjectFlock}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user