mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-22 22:35:45 +00:00
Merge branch 'fix/remove-fcr-related' into 'development'
[FIX/FE] Remove FCR Related See merge request mbugroup/lti-web-client!319
This commit is contained in:
@@ -1,11 +0,0 @@
|
|||||||
import FcrForm from '@/components/pages/master-data/fcr/form/FcrForm';
|
|
||||||
|
|
||||||
const AddFcr = () => {
|
|
||||||
return (
|
|
||||||
<div className='w-full p-4 flex flex-row justify-center'>
|
|
||||||
<FcrForm />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default AddFcr;
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
|
||||||
import useSWR from 'swr';
|
|
||||||
|
|
||||||
import FcrForm from '@/components/pages/master-data/fcr/form/FcrForm';
|
|
||||||
|
|
||||||
import { FcrApi } from '@/services/api/master-data';
|
|
||||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
|
||||||
import { BaseApiResponse } from '@/types/api/api-general';
|
|
||||||
import { FcrWithStandards } from '@/types/api/master-data/fcr';
|
|
||||||
|
|
||||||
const FcrEdit = () => {
|
|
||||||
const router = useRouter();
|
|
||||||
const searchParams = useSearchParams();
|
|
||||||
|
|
||||||
const fcrId = searchParams.get('fcrId');
|
|
||||||
|
|
||||||
const { data: fcr, isLoading: isLoadingFcr } = useSWR(
|
|
||||||
fcrId,
|
|
||||||
(id: number) =>
|
|
||||||
FcrApi.getSingle(id) as Promise<
|
|
||||||
BaseApiResponse<FcrWithStandards> | undefined
|
|
||||||
>
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!fcrId) {
|
|
||||||
router.back();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className='w-full flex flex-row justify-center items-center p-4'>
|
|
||||||
<span className='loading loading-spinner loading-xl' />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isLoadingFcr && (!fcr || isResponseError(fcr))) {
|
|
||||||
router.replace('/404');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className='w-full p-4 flex flex-row justify-center'>
|
|
||||||
{isLoadingFcr && <span className='loading loading-spinner loading-xl' />}
|
|
||||||
{!isLoadingFcr && isResponseSuccess(fcr) && (
|
|
||||||
<FcrForm type='edit' initialValues={fcr.data} />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default FcrEdit;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import SuspenseHelper from '@/components/helper/SuspenseHelper';
|
|
||||||
|
|
||||||
const Layout = ({
|
|
||||||
children,
|
|
||||||
}: Readonly<{
|
|
||||||
children: React.ReactNode;
|
|
||||||
}>) => {
|
|
||||||
return <SuspenseHelper>{children}</SuspenseHelper>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Layout;
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
|
||||||
import useSWR from 'swr';
|
|
||||||
|
|
||||||
import FcrForm from '@/components/pages/master-data/fcr/form/FcrForm';
|
|
||||||
|
|
||||||
import { FcrApi } from '@/services/api/master-data';
|
|
||||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
|
||||||
import { FcrWithStandards } from '@/types/api/master-data/fcr';
|
|
||||||
import { BaseApiResponse } from '@/types/api/api-general';
|
|
||||||
|
|
||||||
const FcrDetail = () => {
|
|
||||||
const router = useRouter();
|
|
||||||
const searchParams = useSearchParams();
|
|
||||||
|
|
||||||
const fcrId = searchParams.get('fcrId');
|
|
||||||
|
|
||||||
const { data: fcr, isLoading: isLoadingFcr } = useSWR(
|
|
||||||
fcrId,
|
|
||||||
(id: number) =>
|
|
||||||
FcrApi.getSingle(id) as Promise<
|
|
||||||
BaseApiResponse<FcrWithStandards> | undefined
|
|
||||||
>
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!fcrId) {
|
|
||||||
router.back();
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className='w-full flex flex-row justify-center items-center p-4'>
|
|
||||||
<span className='loading loading-spinner loading-xl' />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isLoadingFcr && (!fcr || isResponseError(fcr))) {
|
|
||||||
router.replace('/404');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className='w-full p-4 flex flex-row justify-center'>
|
|
||||||
{isLoadingFcr && <span className='loading loading-spinner loading-xl' />}
|
|
||||||
{!isLoadingFcr && isResponseSuccess(fcr) && (
|
|
||||||
<FcrForm type='detail' initialValues={fcr.data} />
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default FcrDetail;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import FcrsTable from '@/components/pages/master-data/fcr/FcrsTable';
|
|
||||||
|
|
||||||
const Fcr = () => {
|
|
||||||
return (
|
|
||||||
<section className='w-full p-4'>
|
|
||||||
<FcrsTable />
|
|
||||||
</section>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Fcr;
|
|
||||||
@@ -1,289 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { ChangeEventHandler, useEffect, useState } from 'react';
|
|
||||||
import useSWR from 'swr';
|
|
||||||
import { CellContext, ColumnDef, SortingState } from '@tanstack/react-table';
|
|
||||||
import toast from 'react-hot-toast';
|
|
||||||
|
|
||||||
import { Icon } from '@iconify/react';
|
|
||||||
import Table from '@/components/Table';
|
|
||||||
import DebouncedTextInput from '@/components/input/DebouncedTextInput';
|
|
||||||
import Button from '@/components/Button';
|
|
||||||
import { useModal } from '@/components/Modal';
|
|
||||||
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
|
||||||
import SelectInput, { OptionType } from '@/components/input/SelectInput';
|
|
||||||
import RowDropdownOptions from '@/components/table/RowDropdownOptions';
|
|
||||||
import RowCollapseOptions from '@/components/table/RowCollapseOptions';
|
|
||||||
import RowOptionsMenuWrapper from '@/components/table/RowOptionsMenuWrapper';
|
|
||||||
import RequirePermission from '@/components/helper/RequirePermission';
|
|
||||||
|
|
||||||
import { Fcr } from '@/types/api/master-data/fcr';
|
|
||||||
import { FcrApi } from '@/services/api/master-data';
|
|
||||||
import { cn } from '@/lib/helper';
|
|
||||||
import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
|
|
||||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
|
||||||
import { ROWS_OPTIONS } from '@/config/constant';
|
|
||||||
|
|
||||||
const RowOptionsMenu = ({
|
|
||||||
type = 'dropdown',
|
|
||||||
props,
|
|
||||||
deleteClickHandler,
|
|
||||||
}: {
|
|
||||||
type: 'dropdown' | 'collapse';
|
|
||||||
props: CellContext<Fcr, unknown>;
|
|
||||||
deleteClickHandler: () => void;
|
|
||||||
}) => {
|
|
||||||
return (
|
|
||||||
<RowOptionsMenuWrapper type={type}>
|
|
||||||
<RequirePermission permissions='lti.master.fcr.detail'>
|
|
||||||
<Button
|
|
||||||
href={`/master-data/fcr/detail/?fcrId=${props.row.original.id}`}
|
|
||||||
variant='ghost'
|
|
||||||
color='primary'
|
|
||||||
className='justify-start text-sm'
|
|
||||||
>
|
|
||||||
<Icon icon='mdi:eye-outline' width={16} height={16} />
|
|
||||||
Detail
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
|
|
||||||
<RequirePermission permissions='lti.master.fcr.update'>
|
|
||||||
<Button
|
|
||||||
href={`/master-data/fcr/detail/edit/?fcrId=${props.row.original.id}`}
|
|
||||||
variant='ghost'
|
|
||||||
color='warning'
|
|
||||||
className='justify-start text-sm'
|
|
||||||
>
|
|
||||||
<Icon icon='material-symbols:edit-outline' width={16} height={16} />
|
|
||||||
Edit
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
|
|
||||||
<RequirePermission permissions='lti.master.fcr.delete'>
|
|
||||||
<Button
|
|
||||||
onClick={deleteClickHandler}
|
|
||||||
variant='ghost'
|
|
||||||
color='error'
|
|
||||||
className='justify-start text-sm text-error focus-visible:text-error-content hover:text-error-content'
|
|
||||||
>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:delete-outline-rounded'
|
|
||||||
width={16}
|
|
||||||
height={16}
|
|
||||||
className='justify-start text-sm'
|
|
||||||
/>
|
|
||||||
Delete
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
</RowOptionsMenuWrapper>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const FcrsTable = () => {
|
|
||||||
const {
|
|
||||||
state: tableFilterState,
|
|
||||||
updateFilter,
|
|
||||||
setPage,
|
|
||||||
setPageSize,
|
|
||||||
toQueryString: getTableFilterQueryString,
|
|
||||||
} = useTableFilter({
|
|
||||||
initial: { search: '', nameSort: '' },
|
|
||||||
paramMap: { page: 'page', pageSize: 'limit', nameSort: 'sort_name' },
|
|
||||||
});
|
|
||||||
|
|
||||||
const {
|
|
||||||
data: fcrs,
|
|
||||||
isLoading,
|
|
||||||
mutate: refreshFcrs,
|
|
||||||
} = useSWR(
|
|
||||||
`${FcrApi.basePath}${getTableFilterQueryString()}`,
|
|
||||||
FcrApi.getAllFetcher
|
|
||||||
);
|
|
||||||
|
|
||||||
const deleteModal = useModal();
|
|
||||||
|
|
||||||
const [selectedFcr, setSelectedFcr] = useState<Fcr | undefined>(undefined);
|
|
||||||
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
|
||||||
|
|
||||||
const [sorting, setSorting] = useState<SortingState>([]);
|
|
||||||
|
|
||||||
const fcrsColumns: ColumnDef<Fcr>[] = [
|
|
||||||
{
|
|
||||||
header: '#',
|
|
||||||
cell: (props) =>
|
|
||||||
tableFilterState.pageSize * (tableFilterState.page - 1) +
|
|
||||||
props.row.index +
|
|
||||||
1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accessorKey: 'name',
|
|
||||||
header: 'Nama',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
header: 'Aksi',
|
|
||||||
cell: (props) => {
|
|
||||||
const currentPageSize = props.table.getPaginationRowModel().rows.length;
|
|
||||||
const currentPageRows = props.table.getPaginationRowModel().flatRows;
|
|
||||||
const currentRowRelativeIndex =
|
|
||||||
currentPageRows.findIndex((r) => r.id === props.row.id) + 1;
|
|
||||||
|
|
||||||
const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;
|
|
||||||
|
|
||||||
const deleteClickHandler = () => {
|
|
||||||
setSelectedFcr(props.row.original);
|
|
||||||
deleteModal.openModal();
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
{currentPageSize > 2 && (
|
|
||||||
<RowDropdownOptions isLast2Rows={isLast2Rows}>
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='dropdown'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</RowDropdownOptions>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{currentPageSize <= 2 && (
|
|
||||||
<RowCollapseOptions>
|
|
||||||
<RowOptionsMenu
|
|
||||||
type='collapse'
|
|
||||||
props={props}
|
|
||||||
deleteClickHandler={deleteClickHandler}
|
|
||||||
/>
|
|
||||||
</RowCollapseOptions>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const confirmationModalDeleteClickHandler = async () => {
|
|
||||||
setIsDeleteLoading(true);
|
|
||||||
|
|
||||||
const deleteResponse = await FcrApi.delete(selectedFcr?.id as number);
|
|
||||||
|
|
||||||
if (isResponseError(deleteResponse)) {
|
|
||||||
toast.error(deleteResponse.message);
|
|
||||||
setIsDeleteLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
refreshFcrs();
|
|
||||||
|
|
||||||
deleteModal.closeModal();
|
|
||||||
toast.success('Successfully delete FCR!');
|
|
||||||
setIsDeleteLoading(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
||||||
updateFilter('search', e.target.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const pageSizeChangeHandler = (val: OptionType | OptionType[] | null) => {
|
|
||||||
const newVal = val as OptionType;
|
|
||||||
|
|
||||||
setPageSize(newVal.value as number);
|
|
||||||
};
|
|
||||||
|
|
||||||
// track sorting
|
|
||||||
useEffect(() => {
|
|
||||||
const isNameSorted = sorting.find((sortItem) => sortItem.id === 'name');
|
|
||||||
|
|
||||||
if (!isNameSorted) {
|
|
||||||
updateFilter('nameSort', '');
|
|
||||||
} else {
|
|
||||||
updateFilter('nameSort', isNameSorted.desc ? 'desc' : 'asc');
|
|
||||||
}
|
|
||||||
}, [sorting]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<div className='w-full p-0 sm:p-4'>
|
|
||||||
<div className='flex flex-col gap-2 mb-4'>
|
|
||||||
<div className='w-full flex flex-col sm:flex-row justify-between items-end sm:items-center gap-2'>
|
|
||||||
<div className='w-full flex flex-row'>
|
|
||||||
<RequirePermission permissions='lti.master.fcr.create'>
|
|
||||||
<Button
|
|
||||||
href='/master-data/fcr/add'
|
|
||||||
variant='outline'
|
|
||||||
color='primary'
|
|
||||||
className='w-full sm:w-fit'
|
|
||||||
>
|
|
||||||
<Icon icon='ic:round-plus' width={24} height={24} />
|
|
||||||
Tambah
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<DebouncedTextInput
|
|
||||||
name='search'
|
|
||||||
placeholder='Cari FCR'
|
|
||||||
value={tableFilterState.search}
|
|
||||||
onChange={searchChangeHandler}
|
|
||||||
className={{ wrapper: 'sm:max-w-3xs' }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className='flex flex-row justify-end'>
|
|
||||||
<SelectInput
|
|
||||||
label='Baris'
|
|
||||||
options={ROWS_OPTIONS}
|
|
||||||
value={{
|
|
||||||
label: String(tableFilterState.pageSize),
|
|
||||||
value: tableFilterState.pageSize,
|
|
||||||
}}
|
|
||||||
onChange={pageSizeChangeHandler}
|
|
||||||
className={{ wrapper: 'max-w-28' }}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Table<Fcr>
|
|
||||||
data={isResponseSuccess(fcrs) ? fcrs?.data : []}
|
|
||||||
columns={fcrsColumns}
|
|
||||||
pageSize={tableFilterState.pageSize}
|
|
||||||
page={isResponseSuccess(fcrs) ? fcrs?.meta?.page : 0}
|
|
||||||
totalItems={isResponseSuccess(fcrs) ? fcrs?.meta?.total_results : 0}
|
|
||||||
onPageChange={setPage}
|
|
||||||
isLoading={isLoading}
|
|
||||||
sorting={sorting}
|
|
||||||
setSorting={setSorting}
|
|
||||||
className={{
|
|
||||||
containerClassName: cn({
|
|
||||||
'mb-20': isResponseSuccess(fcrs) && fcrs?.data?.length === 0,
|
|
||||||
}),
|
|
||||||
tableWrapperClassName: 'overflow-x-auto min-h-full!',
|
|
||||||
tableClassName: 'font-inter w-full table-auto min-h-full!',
|
|
||||||
headerRowClassName: 'border-b border-b-gray-200',
|
|
||||||
headerColumnClassName:
|
|
||||||
'px-6 py-3 text-xs font-semibold text-gray-500 last:flex last:flex-row last:justify-end',
|
|
||||||
bodyRowClassName: 'border-b border-b-gray-200',
|
|
||||||
bodyColumnClassName:
|
|
||||||
'px-6 py-3 last:flex last:flex-row last:justify-end',
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<ConfirmationModal
|
|
||||||
ref={deleteModal.ref}
|
|
||||||
type='error'
|
|
||||||
text={`Apakah anda yakin ingin menghapus data FCR ini (${selectedFcr?.name})?`}
|
|
||||||
secondaryButton={{
|
|
||||||
text: 'Tidak',
|
|
||||||
}}
|
|
||||||
primaryButton={{
|
|
||||||
text: 'Ya',
|
|
||||||
color: 'error',
|
|
||||||
isLoading: isDeleteLoading,
|
|
||||||
onClick: confirmationModalDeleteClickHandler,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default FcrsTable;
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import * as Yup from 'yup';
|
|
||||||
|
|
||||||
const FcrStandardSchema: Yup.ObjectSchema<{
|
|
||||||
weight: number | string;
|
|
||||||
fcr_number: number | string;
|
|
||||||
mortality: number | string;
|
|
||||||
}> = Yup.object({
|
|
||||||
weight: Yup.number().nullable().required('Bobot wajib diisi!'),
|
|
||||||
fcr_number: Yup.number()
|
|
||||||
.nullable()
|
|
||||||
.typeError('FCR harus angka!')
|
|
||||||
.required('FCR harus diisi!'),
|
|
||||||
mortality: Yup.number().nullable().required('Mortalitas wajib diisi!'),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const FcrFormSchema = Yup.object({
|
|
||||||
name: Yup.string().required('Nama wajib diisi!'),
|
|
||||||
fcrStandards: Yup.array()
|
|
||||||
.of(FcrStandardSchema)
|
|
||||||
.min(1, 'Minimal 1 FCR Standard diisi1')
|
|
||||||
.required('FCR wajib diisi!'),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const UpdateFcrFormSchema = FcrFormSchema;
|
|
||||||
|
|
||||||
export type FcrFormValues = Yup.InferType<typeof FcrFormSchema>;
|
|
||||||
@@ -1,401 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
||||||
import { useRouter } from 'next/navigation';
|
|
||||||
import { useFormik } from 'formik';
|
|
||||||
import { toast } from 'react-hot-toast';
|
|
||||||
|
|
||||||
import { Icon } from '@iconify/react';
|
|
||||||
import Button from '@/components/Button';
|
|
||||||
import TextInput from '@/components/input/TextInput';
|
|
||||||
import { useModal } from '@/components/Modal';
|
|
||||||
import ConfirmationModal from '@/components/modal/ConfirmationModal';
|
|
||||||
import RequirePermission from '@/components/helper/RequirePermission';
|
|
||||||
|
|
||||||
import {
|
|
||||||
FcrFormSchema,
|
|
||||||
FcrFormValues,
|
|
||||||
UpdateFcrFormSchema,
|
|
||||||
} from '@/components/pages/master-data/fcr/form/FcrForm.schema';
|
|
||||||
import { isResponseError } from '@/lib/api-helper';
|
|
||||||
import {
|
|
||||||
CreateFcrPayload,
|
|
||||||
Fcr,
|
|
||||||
FcrWithStandards,
|
|
||||||
UpdateFcrPayload,
|
|
||||||
} from '@/types/api/master-data/fcr';
|
|
||||||
import { FcrApi } from '@/services/api/master-data';
|
|
||||||
import { cn } from '@/lib/helper';
|
|
||||||
import AlertErrorList from '@/components/helper/form/FormErrors';
|
|
||||||
import { useFormikErrorList } from '@/services/hooks/useFormikErrorList';
|
|
||||||
|
|
||||||
interface FcrFormProps {
|
|
||||||
type?: 'add' | 'edit' | 'detail';
|
|
||||||
initialValues?: FcrWithStandards;
|
|
||||||
}
|
|
||||||
|
|
||||||
const FcrForm = ({ type = 'add', initialValues }: FcrFormProps) => {
|
|
||||||
const router = useRouter();
|
|
||||||
const deleteModal = useModal();
|
|
||||||
|
|
||||||
const [fcrFormErrorMessage, setFcrFormErrorMessage] = useState('');
|
|
||||||
const [isDeleteLoading, setIsDeleteLoading] = useState(false);
|
|
||||||
|
|
||||||
const createFcrHandler = useCallback(
|
|
||||||
async (payload: CreateFcrPayload) => {
|
|
||||||
const createFcrRes = await FcrApi.create(payload);
|
|
||||||
|
|
||||||
if (isResponseError(createFcrRes)) {
|
|
||||||
setFcrFormErrorMessage(createFcrRes.message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success(createFcrRes?.message as string);
|
|
||||||
router.push('/master-data/fcr');
|
|
||||||
},
|
|
||||||
[router]
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateFcrHandler = useCallback(
|
|
||||||
async (fcrId: number, payload: UpdateFcrPayload) => {
|
|
||||||
const updateFcrRes = await FcrApi.update(fcrId, payload);
|
|
||||||
|
|
||||||
if (updateFcrRes?.status === 'error') {
|
|
||||||
setFcrFormErrorMessage(updateFcrRes.message);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.success(updateFcrRes?.message as string);
|
|
||||||
router.refresh();
|
|
||||||
router.push('/master-data/fcr');
|
|
||||||
},
|
|
||||||
[router]
|
|
||||||
);
|
|
||||||
|
|
||||||
const formikInitialValues = useMemo<FcrFormValues>(() => {
|
|
||||||
return {
|
|
||||||
name: initialValues?.name ?? '',
|
|
||||||
fcrStandards: initialValues?.fcr_standards
|
|
||||||
? initialValues?.fcr_standards
|
|
||||||
: [
|
|
||||||
{
|
|
||||||
weight: '',
|
|
||||||
fcr_number: '',
|
|
||||||
mortality: '',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
}, [initialValues]);
|
|
||||||
|
|
||||||
const formik = useFormik<FcrFormValues>({
|
|
||||||
initialValues: formikInitialValues,
|
|
||||||
validationSchema: type === 'edit' ? UpdateFcrFormSchema : FcrFormSchema,
|
|
||||||
onSubmit: async (values) => {
|
|
||||||
setFcrFormErrorMessage('');
|
|
||||||
|
|
||||||
const fcrPayload: CreateFcrPayload = {
|
|
||||||
name: values.name,
|
|
||||||
fcr_standards: values.fcrStandards as CreateFcrPayload['fcr_standards'],
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case 'add':
|
|
||||||
await createFcrHandler(fcrPayload);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'edit':
|
|
||||||
await updateFcrHandler(initialValues?.id as number, fcrPayload);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const { setValues: formikSetValues } = formik;
|
|
||||||
|
|
||||||
const addFcrStandard = () =>
|
|
||||||
formik.setFieldValue('fcrStandards', [
|
|
||||||
...formik.values.fcrStandards,
|
|
||||||
{
|
|
||||||
weight: '',
|
|
||||||
fcr_number: '',
|
|
||||||
mortality: '',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
|
|
||||||
const removeFcrStandard = (i: number) =>
|
|
||||||
formik.setFieldValue(
|
|
||||||
'fcrStandards',
|
|
||||||
formik.values.fcrStandards.filter((_, idx) => idx !== i)
|
|
||||||
);
|
|
||||||
|
|
||||||
const deleteFcrClickHandler = () => {
|
|
||||||
deleteModal.openModal();
|
|
||||||
};
|
|
||||||
|
|
||||||
const confirmationModalDeleteClickHandler = async () => {
|
|
||||||
setIsDeleteLoading(true);
|
|
||||||
|
|
||||||
await FcrApi.delete(initialValues?.id as number);
|
|
||||||
|
|
||||||
deleteModal.closeModal();
|
|
||||||
toast.success('Successfully delete FCR!');
|
|
||||||
setIsDeleteLoading(false);
|
|
||||||
router.push('/master-data/fcr');
|
|
||||||
};
|
|
||||||
|
|
||||||
const isRepeaterInputError = (
|
|
||||||
column: keyof CreateFcrPayload['fcr_standards'][0],
|
|
||||||
idx: number
|
|
||||||
) => {
|
|
||||||
return (
|
|
||||||
formik.touched.fcrStandards?.[idx]?.[column] &&
|
|
||||||
Boolean(
|
|
||||||
formik.errors.fcrStandards?.[idx] instanceof Object &&
|
|
||||||
formik.errors.fcrStandards?.[idx]?.[column]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
formikSetValues(formikInitialValues);
|
|
||||||
}, [formikSetValues, formikInitialValues]);
|
|
||||||
|
|
||||||
// ===== Formik Error List =====
|
|
||||||
const { formErrorList, close, handleFormSubmit } = useFormikErrorList(formik);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<section className='w-full max-w-5xl'>
|
|
||||||
<header className='flex flex-col gap-4'>
|
|
||||||
<Button
|
|
||||||
href='/master-data/fcr'
|
|
||||||
variant='link'
|
|
||||||
className='w-fit p-0 text-primary'
|
|
||||||
>
|
|
||||||
<Icon icon='uil:arrow-left' width={24} height={24} />
|
|
||||||
Kembali
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<h1 className='text-2xl font-bold text-center'>
|
|
||||||
{type === 'add' && 'Tambah FCR'}
|
|
||||||
{type === 'edit' && 'Edit FCR'}
|
|
||||||
{type === 'detail' && 'Detail FCR'}
|
|
||||||
</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<form
|
|
||||||
onSubmit={handleFormSubmit}
|
|
||||||
onReset={formik.handleReset}
|
|
||||||
className='w-full mt-8 flex flex-col gap-6'
|
|
||||||
>
|
|
||||||
<div className='flex flex-col gap-4'>
|
|
||||||
<TextInput
|
|
||||||
required
|
|
||||||
label='Nama'
|
|
||||||
name='name'
|
|
||||||
placeholder='Masukkan nama FCR'
|
|
||||||
value={formik.values.name}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
onBlur={formik.handleBlur}
|
|
||||||
isError={formik.touched.name && Boolean(formik.errors.name)}
|
|
||||||
errorMessage={formik.errors.name}
|
|
||||||
readOnly={type === 'detail'}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<div className='overflow-x-auto'>
|
|
||||||
<table className='table'>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Bobot</th>
|
|
||||||
<th>FCR</th>
|
|
||||||
<th>Mortalitas</th>
|
|
||||||
{type !== 'detail' && <th>Aksi</th>}
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
|
|
||||||
<tbody>
|
|
||||||
{formik.values.fcrStandards.map((fcrStandard, idx) => (
|
|
||||||
<tr key={idx}>
|
|
||||||
<td>
|
|
||||||
<TextInput
|
|
||||||
required
|
|
||||||
type='number'
|
|
||||||
name={`fcrStandards[${idx}].weight`}
|
|
||||||
placeholder='Masukkan bobot'
|
|
||||||
value={fcrStandard.weight}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
onBlur={formik.handleBlur}
|
|
||||||
isError={isRepeaterInputError('weight', idx)}
|
|
||||||
readOnly={type === 'detail'}
|
|
||||||
className={{
|
|
||||||
wrapper: 'w-full min-w-24',
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<TextInput
|
|
||||||
required
|
|
||||||
type='number'
|
|
||||||
name={`fcrStandards[${idx}].fcr_number`}
|
|
||||||
placeholder='Masukkan FCR'
|
|
||||||
value={fcrStandard.fcr_number}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
onBlur={formik.handleBlur}
|
|
||||||
isError={isRepeaterInputError('fcr_number', idx)}
|
|
||||||
readOnly={type === 'detail'}
|
|
||||||
className={{
|
|
||||||
wrapper: 'w-full min-w-24',
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<TextInput
|
|
||||||
required
|
|
||||||
type='number'
|
|
||||||
name={`fcrStandards[${idx}].mortality`}
|
|
||||||
placeholder='Masukkan mortalitas'
|
|
||||||
value={fcrStandard.mortality}
|
|
||||||
onChange={formik.handleChange}
|
|
||||||
onBlur={formik.handleBlur}
|
|
||||||
isError={isRepeaterInputError('mortality', idx)}
|
|
||||||
readOnly={type === 'detail'}
|
|
||||||
className={{
|
|
||||||
wrapper: 'w-full min-w-24',
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</td>
|
|
||||||
{type !== 'detail' && (
|
|
||||||
<td>
|
|
||||||
<Button
|
|
||||||
type='button'
|
|
||||||
color='error'
|
|
||||||
onClick={() => removeFcrStandard(idx)}
|
|
||||||
>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:delete-outline-rounded'
|
|
||||||
width={24}
|
|
||||||
height={24}
|
|
||||||
/>
|
|
||||||
</Button>
|
|
||||||
</td>
|
|
||||||
)}
|
|
||||||
</tr>
|
|
||||||
))}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{type !== 'detail' && (
|
|
||||||
<Button
|
|
||||||
type='button'
|
|
||||||
color='success'
|
|
||||||
onClick={addFcrStandard}
|
|
||||||
className='w-fit mx-auto'
|
|
||||||
>
|
|
||||||
<Icon icon='ic:round-plus' width={24} height={24} /> Tambah FCR
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<AlertErrorList formErrorList={formErrorList} onClose={close} />
|
|
||||||
|
|
||||||
<div className='flex flex-row justify-between gap-2 flex-wrap'>
|
|
||||||
{type !== 'add' && (
|
|
||||||
<div className='flex flex-row justify-start gap-2'>
|
|
||||||
<RequirePermission permissions='lti.master.fcr.delete'>
|
|
||||||
<Button
|
|
||||||
type='button'
|
|
||||||
color='error'
|
|
||||||
onClick={deleteFcrClickHandler}
|
|
||||||
className='px-4'
|
|
||||||
>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:delete-outline-rounded'
|
|
||||||
width={24}
|
|
||||||
height={24}
|
|
||||||
className='justify-start text-sm'
|
|
||||||
/>
|
|
||||||
Delete
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
|
|
||||||
{type !== 'edit' && (
|
|
||||||
<RequirePermission permissions='lti.master.fcr.update'>
|
|
||||||
<Button
|
|
||||||
type='button'
|
|
||||||
color='warning'
|
|
||||||
href={`/master-data/fcr/detail/edit/?fcrId=${initialValues?.id}`}
|
|
||||||
className='px-4'
|
|
||||||
>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:edit-outline'
|
|
||||||
width={24}
|
|
||||||
height={24}
|
|
||||||
className='justify-start text-sm'
|
|
||||||
/>
|
|
||||||
Edit
|
|
||||||
</Button>
|
|
||||||
</RequirePermission>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{type !== 'detail' && (
|
|
||||||
<div
|
|
||||||
className={cn('flex flex-row justify-end gap-2', {
|
|
||||||
'w-full': type === 'add',
|
|
||||||
})}
|
|
||||||
>
|
|
||||||
<Button type='reset' color='warning' className='px-4'>
|
|
||||||
Reset
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
type='submit'
|
|
||||||
color='primary'
|
|
||||||
isLoading={formik.isSubmitting}
|
|
||||||
disabled={formik.isSubmitting}
|
|
||||||
className='px-4'
|
|
||||||
>
|
|
||||||
Submit
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{fcrFormErrorMessage && (
|
|
||||||
<div role='alert' className='alert alert-error'>
|
|
||||||
<Icon
|
|
||||||
icon='material-symbols:error-outline'
|
|
||||||
width={24}
|
|
||||||
height={24}
|
|
||||||
/>
|
|
||||||
<span>{fcrFormErrorMessage}</span>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</form>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
{type !== 'add' && (
|
|
||||||
<ConfirmationModal
|
|
||||||
ref={deleteModal.ref}
|
|
||||||
type='error'
|
|
||||||
text={`Apakah anda yakin ingin menghapus data FCR ini (${initialValues?.name})?`}
|
|
||||||
secondaryButton={{
|
|
||||||
text: 'Tidak',
|
|
||||||
}}
|
|
||||||
primaryButton={{
|
|
||||||
text: 'Ya',
|
|
||||||
color: 'error',
|
|
||||||
isLoading: isDeleteLoading,
|
|
||||||
onClick: confirmationModalDeleteClickHandler,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default FcrForm;
|
|
||||||
@@ -220,7 +220,6 @@ export const MAIN_DRAWER_LINKS: SidebarMenuItem[] = [
|
|||||||
'lti.master.area.list',
|
'lti.master.area.list',
|
||||||
'lti.master.banks.list',
|
'lti.master.banks.list',
|
||||||
'lti.master.customer.list',
|
'lti.master.customer.list',
|
||||||
'lti.master.fcr.list',
|
|
||||||
'lti.master.flocks.list',
|
'lti.master.flocks.list',
|
||||||
'lti.master.kandangs.list',
|
'lti.master.kandangs.list',
|
||||||
'lti.master.locations.list',
|
'lti.master.locations.list',
|
||||||
@@ -283,11 +282,6 @@ export const MAIN_DRAWER_LINKS: SidebarMenuItem[] = [
|
|||||||
link: '/master-data/nonstock',
|
link: '/master-data/nonstock',
|
||||||
permission: ['lti.master.nonstocks.list'],
|
permission: ['lti.master.nonstocks.list'],
|
||||||
},
|
},
|
||||||
{
|
|
||||||
text: 'FCR',
|
|
||||||
link: '/master-data/fcr',
|
|
||||||
permission: ['lti.master.fcr.list'],
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
text: 'Supplier',
|
text: 'Supplier',
|
||||||
link: '/master-data/supplier',
|
link: '/master-data/supplier',
|
||||||
|
|||||||
@@ -195,11 +195,6 @@ export const ROUTE_PERMISSIONS: Record<string, string[]> = {
|
|||||||
'/master-data/nonstock/detail/': ['lti.master.nonstocks.detail'],
|
'/master-data/nonstock/detail/': ['lti.master.nonstocks.detail'],
|
||||||
'/master-data/nonstock/detail/edit/': ['lti.master.nonstocks.update'],
|
'/master-data/nonstock/detail/edit/': ['lti.master.nonstocks.update'],
|
||||||
|
|
||||||
'/master-data/fcr/': ['lti.master.fcr.list'],
|
|
||||||
'/master-data/fcr/add/': ['lti.master.fcr.create'],
|
|
||||||
'/master-data/fcr/detail/': ['lti.master.fcr.detail'],
|
|
||||||
'/master-data/fcr/detail/edit/': ['lti.master.fcr.update'],
|
|
||||||
|
|
||||||
'/master-data/supplier/': ['lti.master.suppliers.list'],
|
'/master-data/supplier/': ['lti.master.suppliers.list'],
|
||||||
'/master-data/supplier/add/': ['lti.master.suppliers.create'],
|
'/master-data/supplier/add/': ['lti.master.suppliers.create'],
|
||||||
'/master-data/supplier/detail/': ['lti.master.suppliers.detail'],
|
'/master-data/supplier/detail/': ['lti.master.suppliers.detail'],
|
||||||
|
|||||||
@@ -54,11 +54,7 @@ import {
|
|||||||
CreateBankPayload,
|
CreateBankPayload,
|
||||||
UpdateBankPayload,
|
UpdateBankPayload,
|
||||||
} from '@/types/api/master-data/bank';
|
} from '@/types/api/master-data/bank';
|
||||||
import {
|
|
||||||
CreateFcrPayload,
|
|
||||||
Fcr,
|
|
||||||
UpdateFcrPayload,
|
|
||||||
} from '@/types/api/master-data/fcr';
|
|
||||||
import {
|
import {
|
||||||
CreateFlockPayload,
|
CreateFlockPayload,
|
||||||
Flock,
|
Flock,
|
||||||
@@ -131,12 +127,6 @@ export const BankApi = new BaseApiService<
|
|||||||
UpdateBankPayload
|
UpdateBankPayload
|
||||||
>('/master-data/banks');
|
>('/master-data/banks');
|
||||||
|
|
||||||
export const FcrApi = new BaseApiService<
|
|
||||||
Fcr,
|
|
||||||
CreateFcrPayload,
|
|
||||||
UpdateFcrPayload
|
|
||||||
>('/master-data/fcrs');
|
|
||||||
|
|
||||||
export const FlockApi = new BaseApiService<
|
export const FlockApi = new BaseApiService<
|
||||||
Flock,
|
Flock,
|
||||||
CreateFlockPayload,
|
CreateFlockPayload,
|
||||||
|
|||||||
Vendored
-1
@@ -1,5 +1,4 @@
|
|||||||
import { Area } from '@/types/api/master-data/area';
|
import { Area } from '@/types/api/master-data/area';
|
||||||
import { Fcr } from '@/types/api/master-data/fcr';
|
|
||||||
import { Flock } from '@/types/api/master-data/flock';
|
import { Flock } from '@/types/api/master-data/flock';
|
||||||
import { Location } from '@/types/api/master-data/location';
|
import { Location } from '@/types/api/master-data/location';
|
||||||
import { Kandang } from '@/types/api/master-data/kandang';
|
import { Kandang } from '@/types/api/master-data/kandang';
|
||||||
|
|||||||
Vendored
-30
@@ -1,30 +0,0 @@
|
|||||||
import { BaseMetadata } from '@/types/api/api-general';
|
|
||||||
|
|
||||||
export type BaseFcr = {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type FcrStandard = {
|
|
||||||
id: number;
|
|
||||||
weight: number;
|
|
||||||
fcr_number: number;
|
|
||||||
mortality: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Fcr = BaseMetadata & BaseFcr;
|
|
||||||
|
|
||||||
export type FcrWithStandards = Fcr & {
|
|
||||||
fcr_standards: FcrStandard[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type CreateFcrPayload = {
|
|
||||||
name: string;
|
|
||||||
fcr_standards: {
|
|
||||||
weight: number;
|
|
||||||
fcr_number: number;
|
|
||||||
mortality: number;
|
|
||||||
}[];
|
|
||||||
};
|
|
||||||
|
|
||||||
export type UpdateFcrPayload = CreateFcrPayload;
|
|
||||||
Reference in New Issue
Block a user