mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
Merge branch 'feat/expense-enhancement' into 'development'
[FEAT/FE] Expense Enhancement See merge request mbugroup/lti-web-client!404
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useMemo, useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
@@ -9,6 +10,7 @@ import ExpenseRequestContent from '@/components/pages/expense/ExpenseRequestCont
|
|||||||
import ExpenseRealizationContent from '@/components/pages/expense/ExpenseRealizationContent';
|
import ExpenseRealizationContent from '@/components/pages/expense/ExpenseRealizationContent';
|
||||||
|
|
||||||
import { Expense } from '@/types/api/expense';
|
import { Expense } from '@/types/api/expense';
|
||||||
|
import { getExpenseListReturnTo } from '@/lib/expense-list-navigation';
|
||||||
|
|
||||||
interface ExpenseDetailProps {
|
interface ExpenseDetailProps {
|
||||||
initialValues?: Expense;
|
initialValues?: Expense;
|
||||||
@@ -16,6 +18,8 @@ interface ExpenseDetailProps {
|
|||||||
|
|
||||||
const ExpenseDetail: React.FC<ExpenseDetailProps> = ({ initialValues }) => {
|
const ExpenseDetail: React.FC<ExpenseDetailProps> = ({ initialValues }) => {
|
||||||
const [activeTab, setActiveTab] = useState<string>('request');
|
const [activeTab, setActiveTab] = useState<string>('request');
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const returnTo = getExpenseListReturnTo(searchParams);
|
||||||
|
|
||||||
const expenseDetailTabs = useMemo(() => {
|
const expenseDetailTabs = useMemo(() => {
|
||||||
const validTabs = [
|
const validTabs = [
|
||||||
@@ -46,7 +50,7 @@ const ExpenseDetail: React.FC<ExpenseDetailProps> = ({ initialValues }) => {
|
|||||||
<section className='w-full max-w-full pb-16'>
|
<section className='w-full max-w-full pb-16'>
|
||||||
<header className='flex flex-col gap-4'>
|
<header className='flex flex-col gap-4'>
|
||||||
<Button
|
<Button
|
||||||
href='/expense'
|
href={returnTo}
|
||||||
variant='link'
|
variant='link'
|
||||||
className='w-fit p-0 text-primary'
|
className='w-fit p-0 text-primary'
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
|
import { useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
@@ -16,6 +19,7 @@ import {
|
|||||||
} from '@/components/pages/expense/form/ExpenseRequestForm.schema';
|
} from '@/components/pages/expense/form/ExpenseRequestForm.schema';
|
||||||
import { ExpenseApi } from '@/services/api/expense';
|
import { ExpenseApi } from '@/services/api/expense';
|
||||||
import { isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
|
import { buildExpenseActionHref } from '@/lib/expense-list-navigation';
|
||||||
import { ACCEPTED_FILE_TYPE, S3_PUBLIC_BASE_URL } from '@/config/constant';
|
import { ACCEPTED_FILE_TYPE, S3_PUBLIC_BASE_URL } from '@/config/constant';
|
||||||
|
|
||||||
interface ExpenseRealizationContentProps {
|
interface ExpenseRealizationContentProps {
|
||||||
@@ -25,6 +29,8 @@ interface ExpenseRealizationContentProps {
|
|||||||
const ExpenseRealizationContent = ({
|
const ExpenseRealizationContent = ({
|
||||||
initialValues,
|
initialValues,
|
||||||
}: ExpenseRealizationContentProps) => {
|
}: ExpenseRealizationContentProps) => {
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const formik = useFormik<UploadRequestDocumentsFormValues>({
|
const formik = useFormik<UploadRequestDocumentsFormValues>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
documents: [],
|
documents: [],
|
||||||
@@ -74,7 +80,11 @@ const ExpenseRealizationContent = ({
|
|||||||
<Button
|
<Button
|
||||||
type='button'
|
type='button'
|
||||||
color='warning'
|
color='warning'
|
||||||
href={`/expense/realization/edit/?expenseId=${initialValues?.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/realization/edit/',
|
||||||
|
initialValues?.id as number,
|
||||||
|
searchParams
|
||||||
|
)}
|
||||||
className='px-4 grow sm:grow-0'
|
className='px-4 grow sm:grow-0'
|
||||||
>
|
>
|
||||||
<Icon icon='mdi:pencil-outline' width={24} height={24} />
|
<Icon icon='mdi:pencil-outline' width={24} height={24} />
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
@@ -31,6 +31,10 @@ import { ExpenseApi } from '@/services/api/expense';
|
|||||||
import { isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
import { EXPENSE_REQUEST_APPROVAL_LINE } from '@/config/approval-line';
|
import { EXPENSE_REQUEST_APPROVAL_LINE } from '@/config/approval-line';
|
||||||
import { BaseApiResponse } from '@/types/api/api-general';
|
import { BaseApiResponse } from '@/types/api/api-general';
|
||||||
|
import {
|
||||||
|
buildExpenseActionHref,
|
||||||
|
getExpenseListReturnTo,
|
||||||
|
} from '@/lib/expense-list-navigation';
|
||||||
|
|
||||||
interface ExpenseRequestContentProps {
|
interface ExpenseRequestContentProps {
|
||||||
initialValues?: Expense;
|
initialValues?: Expense;
|
||||||
@@ -40,6 +44,8 @@ const ExpenseRequestContent = ({
|
|||||||
initialValues,
|
initialValues,
|
||||||
}: ExpenseRequestContentProps) => {
|
}: ExpenseRequestContentProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const returnTo = getExpenseListReturnTo(searchParams);
|
||||||
|
|
||||||
const { approvals: approvalHistory, isLoading: isLoadingApprovalHistory } =
|
const { approvals: approvalHistory, isLoading: isLoadingApprovalHistory } =
|
||||||
useApprovalSteps({
|
useApprovalSteps({
|
||||||
@@ -148,7 +154,7 @@ const ExpenseRequestContent = ({
|
|||||||
|
|
||||||
if (isResponseSuccess(deleteResponse)) {
|
if (isResponseSuccess(deleteResponse)) {
|
||||||
toast.success('Berhasil menghapus data biaya operasional!');
|
toast.success('Berhasil menghapus data biaya operasional!');
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
} else {
|
} else {
|
||||||
toast.error('Gagal menghapus data biaya operasional!');
|
toast.error('Gagal menghapus data biaya operasional!');
|
||||||
}
|
}
|
||||||
@@ -164,7 +170,7 @@ const ExpenseRequestContent = ({
|
|||||||
|
|
||||||
if (isResponseSuccess(completeRes)) {
|
if (isResponseSuccess(completeRes)) {
|
||||||
toast.success(completeRes.message);
|
toast.success(completeRes.message);
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
} else {
|
} else {
|
||||||
toast.error(completeRes?.message as string);
|
toast.error(completeRes?.message as string);
|
||||||
}
|
}
|
||||||
@@ -204,7 +210,7 @@ const ExpenseRequestContent = ({
|
|||||||
|
|
||||||
toast.success(approveResponse?.message);
|
toast.success(approveResponse?.message);
|
||||||
setApprovalNotes('');
|
setApprovalNotes('');
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
} else {
|
} else {
|
||||||
approveModal.closeModal();
|
approveModal.closeModal();
|
||||||
|
|
||||||
@@ -239,7 +245,7 @@ const ExpenseRequestContent = ({
|
|||||||
|
|
||||||
toast.success(rejectResponse.message);
|
toast.success(rejectResponse.message);
|
||||||
setApprovalNotes('');
|
setApprovalNotes('');
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
} else {
|
} else {
|
||||||
rejectModal.closeModal();
|
rejectModal.closeModal();
|
||||||
|
|
||||||
@@ -365,7 +371,11 @@ const ExpenseRequestContent = ({
|
|||||||
<Button
|
<Button
|
||||||
variant='outline'
|
variant='outline'
|
||||||
color='info'
|
color='info'
|
||||||
href={`/expense/realization/?expenseId=${initialValues?.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/realization/',
|
||||||
|
initialValues?.id as number,
|
||||||
|
searchParams
|
||||||
|
)}
|
||||||
className='w-full sm:w-fit'
|
className='w-full sm:w-fit'
|
||||||
>
|
>
|
||||||
<Icon
|
<Icon
|
||||||
@@ -384,7 +394,11 @@ const ExpenseRequestContent = ({
|
|||||||
<Button
|
<Button
|
||||||
type='button'
|
type='button'
|
||||||
color='warning'
|
color='warning'
|
||||||
href={`/expense/detail/edit/?expenseId=${initialValues?.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/detail/edit/',
|
||||||
|
initialValues?.id as number,
|
||||||
|
searchParams
|
||||||
|
)}
|
||||||
className='px-4 grow sm:grow-0'
|
className='px-4 grow sm:grow-0'
|
||||||
>
|
>
|
||||||
<Icon icon='mdi:pencil-outline' width={24} height={24} />
|
<Icon icon='mdi:pencil-outline' width={24} height={24} />
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { ChangeEventHandler, useEffect, useMemo, useState } from 'react';
|
import {
|
||||||
import { usePathname } from 'next/navigation';
|
ChangeEventHandler,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useMemo,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
|
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useUiStore } from '@/stores/ui/ui.store';
|
import { useUiStore } from '@/stores/ui/ui.store';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import {
|
import {
|
||||||
@@ -31,19 +37,32 @@ import ExpenseTableSkeleton from '@/components/pages/expense/skeleton/ExpenseTab
|
|||||||
|
|
||||||
import { Expense } from '@/types/api/expense';
|
import { Expense } from '@/types/api/expense';
|
||||||
import { ExpenseApi } from '@/services/api/expense';
|
import { ExpenseApi } from '@/services/api/expense';
|
||||||
|
import { buildExpenseActionHref } from '@/lib/expense-list-navigation';
|
||||||
import { cn, formatCurrency, formatDate } from '@/lib/helper';
|
import { cn, formatCurrency, formatDate } from '@/lib/helper';
|
||||||
import { isResponseSuccess } from '@/lib/api-helper';
|
import { isResponseSuccess } from '@/lib/api-helper';
|
||||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||||
import { BaseApiResponse } from '@/types/api/api-general';
|
import { BaseApiResponse } from '@/types/api/api-general';
|
||||||
|
|
||||||
|
type ExpenseTableFilters = {
|
||||||
|
search: string;
|
||||||
|
nameSort: string;
|
||||||
|
transactionDate: string;
|
||||||
|
realizationDate: string;
|
||||||
|
locationId: string;
|
||||||
|
vendorId: string;
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
|
||||||
const RowOptionsMenu = ({
|
const RowOptionsMenu = ({
|
||||||
popoverPosition = 'bottom',
|
popoverPosition = 'bottom',
|
||||||
props,
|
props,
|
||||||
deleteClickHandler,
|
deleteClickHandler,
|
||||||
|
returnToSearchParams,
|
||||||
}: {
|
}: {
|
||||||
popoverPosition: 'bottom' | 'top';
|
popoverPosition: 'bottom' | 'top';
|
||||||
props: CellContext<Expense, unknown>;
|
props: CellContext<Expense, unknown>;
|
||||||
deleteClickHandler: () => void;
|
deleteClickHandler: () => void;
|
||||||
|
returnToSearchParams: URLSearchParams;
|
||||||
}) => {
|
}) => {
|
||||||
const popoverId = `expense#${props.row.original.id}`;
|
const popoverId = `expense#${props.row.original.id}`;
|
||||||
const popoverAnchorName = `--anchor-expense#${props.row.original.id}`;
|
const popoverAnchorName = `--anchor-expense#${props.row.original.id}`;
|
||||||
@@ -86,7 +105,11 @@ const RowOptionsMenu = ({
|
|||||||
<div className='flex flex-col bg-base-100 rounded-xl'>
|
<div className='flex flex-col bg-base-100 rounded-xl'>
|
||||||
<RequirePermission permissions='lti.expense.detail'>
|
<RequirePermission permissions='lti.expense.detail'>
|
||||||
<Button
|
<Button
|
||||||
href={`/expense/detail/?expenseId=${props.row.original.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/detail/',
|
||||||
|
props.row.original.id,
|
||||||
|
returnToSearchParams
|
||||||
|
)}
|
||||||
variant='ghost'
|
variant='ghost'
|
||||||
color='none'
|
color='none'
|
||||||
className='p-3 justify-start text-sm font-semibold w-full'
|
className='p-3 justify-start text-sm font-semibold w-full'
|
||||||
@@ -100,7 +123,11 @@ const RowOptionsMenu = ({
|
|||||||
{showEditButton && (
|
{showEditButton && (
|
||||||
<RequirePermission permissions='lti.expense.update'>
|
<RequirePermission permissions='lti.expense.update'>
|
||||||
<Button
|
<Button
|
||||||
href={`/expense/detail/edit/?expenseId=${props.row.original.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/detail/edit/',
|
||||||
|
props.row.original.id,
|
||||||
|
returnToSearchParams
|
||||||
|
)}
|
||||||
variant='ghost'
|
variant='ghost'
|
||||||
color='none'
|
color='none'
|
||||||
className='p-3 justify-start text-sm font-semibold w-full'
|
className='p-3 justify-start text-sm font-semibold w-full'
|
||||||
@@ -115,7 +142,11 @@ const RowOptionsMenu = ({
|
|||||||
{showRealizationButton && (
|
{showRealizationButton && (
|
||||||
<RequirePermission permissions='lti.expense.create.realization'>
|
<RequirePermission permissions='lti.expense.create.realization'>
|
||||||
<Button
|
<Button
|
||||||
href={`/expense/realization/?expenseId=${props.row.original.id}`}
|
href={buildExpenseActionHref(
|
||||||
|
'/expense/realization/',
|
||||||
|
props.row.original.id,
|
||||||
|
returnToSearchParams
|
||||||
|
)}
|
||||||
variant='ghost'
|
variant='ghost'
|
||||||
color='none'
|
color='none'
|
||||||
className='p-3 justify-start text-sm font-semibold w-full'
|
className='p-3 justify-start text-sm font-semibold w-full'
|
||||||
@@ -155,6 +186,8 @@ const RowOptionsMenu = ({
|
|||||||
const ExpensesTable = () => {
|
const ExpensesTable = () => {
|
||||||
const { searchValue, setSearchValue, setTableState } = useUiStore();
|
const { searchValue, setSearchValue, setTableState } = useUiStore();
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
state: tableFilterState,
|
state: tableFilterState,
|
||||||
@@ -162,9 +195,11 @@ const ExpensesTable = () => {
|
|||||||
setPage,
|
setPage,
|
||||||
setPageSize,
|
setPageSize,
|
||||||
toQueryString: getTableFilterQueryString,
|
toQueryString: getTableFilterQueryString,
|
||||||
} = useTableFilter({
|
} = useTableFilter<ExpenseTableFilters>({
|
||||||
initial: {
|
initial: {
|
||||||
search: '',
|
page: Number(searchParams.get('page')) || 1,
|
||||||
|
pageSize: Number(searchParams.get('limit')) || 10,
|
||||||
|
search: searchValue,
|
||||||
nameSort: '',
|
nameSort: '',
|
||||||
transactionDate: '',
|
transactionDate: '',
|
||||||
realizationDate: '',
|
realizationDate: '',
|
||||||
@@ -193,6 +228,54 @@ const ExpensesTable = () => {
|
|||||||
ExpenseApi.getAllFetcher
|
ExpenseApi.getAllFetcher
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const syncPaginationToUrl = useCallback(
|
||||||
|
(page: number, pageSize: number) => {
|
||||||
|
const nextQueryString = new URLSearchParams({
|
||||||
|
page: String(page),
|
||||||
|
limit: String(pageSize),
|
||||||
|
}).toString();
|
||||||
|
|
||||||
|
router.replace(
|
||||||
|
nextQueryString ? `${pathname}?${nextQueryString}` : pathname,
|
||||||
|
{
|
||||||
|
scroll: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[pathname, router]
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageChangeHandler = useCallback(
|
||||||
|
(page: number) => {
|
||||||
|
setPage(page);
|
||||||
|
syncPaginationToUrl(page, tableFilterState.pageSize);
|
||||||
|
},
|
||||||
|
[setPage, syncPaginationToUrl, tableFilterState.pageSize]
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageSizeChangeHandler = useCallback(
|
||||||
|
(pageSize: number) => {
|
||||||
|
setPageSize(pageSize);
|
||||||
|
syncPaginationToUrl(1, pageSize);
|
||||||
|
},
|
||||||
|
[setPageSize, syncPaginationToUrl]
|
||||||
|
);
|
||||||
|
|
||||||
|
const returnToSearchParams = useMemo(() => {
|
||||||
|
const returnToParams = new URLSearchParams();
|
||||||
|
const queryString = new URLSearchParams({
|
||||||
|
page: String(tableFilterState.page),
|
||||||
|
limit: String(tableFilterState.pageSize),
|
||||||
|
}).toString();
|
||||||
|
|
||||||
|
returnToParams.set(
|
||||||
|
'returnTo',
|
||||||
|
queryString ? `${pathname}?${queryString}` : pathname
|
||||||
|
);
|
||||||
|
|
||||||
|
return returnToParams;
|
||||||
|
}, [pathname, tableFilterState.page, tableFilterState.pageSize]);
|
||||||
|
|
||||||
const deleteModal = useModal();
|
const deleteModal = useModal();
|
||||||
const approveModal = useModal();
|
const approveModal = useModal();
|
||||||
const rejectModal = useModal();
|
const rejectModal = useModal();
|
||||||
@@ -373,6 +456,7 @@ const ExpensesTable = () => {
|
|||||||
popoverPosition={isLast2Rows ? 'top' : 'bottom'}
|
popoverPosition={isLast2Rows ? 'top' : 'bottom'}
|
||||||
props={props}
|
props={props}
|
||||||
deleteClickHandler={deleteClickHandler}
|
deleteClickHandler={deleteClickHandler}
|
||||||
|
returnToSearchParams={returnToSearchParams}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -512,10 +596,6 @@ const ExpensesTable = () => {
|
|||||||
setIsRejectLoading(false);
|
setIsRejectLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
updateFilter('search', searchValue);
|
|
||||||
}, [searchValue, updateFilter]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTableState('expense-table', pathname);
|
setTableState('expense-table', pathname);
|
||||||
}, [pathname, setTableState]);
|
}, [pathname, setTableState]);
|
||||||
@@ -554,7 +634,7 @@ const ExpensesTable = () => {
|
|||||||
const isNameSorted = sorting.find((sortItem) => sortItem.id === 'name');
|
const isNameSorted = sorting.find((sortItem) => sortItem.id === 'name');
|
||||||
|
|
||||||
if (!isNameSorted) {
|
if (!isNameSorted) {
|
||||||
updateFilter('nameSort', '');
|
updateFilter('nameSort', '', false);
|
||||||
} else {
|
} else {
|
||||||
updateFilter('nameSort', isNameSorted.desc ? 'desc' : 'asc');
|
updateFilter('nameSort', isNameSorted.desc ? 'desc' : 'asc');
|
||||||
}
|
}
|
||||||
@@ -734,8 +814,8 @@ const ExpensesTable = () => {
|
|||||||
totalItems={
|
totalItems={
|
||||||
isResponseSuccess(expenses) ? expenses?.meta?.total_results : 0
|
isResponseSuccess(expenses) ? expenses?.meta?.total_results : 0
|
||||||
}
|
}
|
||||||
onPageChange={setPage}
|
onPageChange={pageChangeHandler}
|
||||||
onPageSizeChange={setPageSize}
|
onPageSizeChange={pageSizeChangeHandler}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
sorting={sorting}
|
sorting={sorting}
|
||||||
setSorting={setSorting}
|
setSorting={setSorting}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useCallback, useState } from 'react';
|
import { useCallback, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
@@ -35,6 +35,7 @@ import { isResponseError } from '@/lib/api-helper';
|
|||||||
import { LocationApi, SupplierApi } from '@/services/api/master-data';
|
import { LocationApi, SupplierApi } from '@/services/api/master-data';
|
||||||
import { Supplier } from '@/types/api/master-data/supplier';
|
import { Supplier } from '@/types/api/master-data/supplier';
|
||||||
import { ACCEPTED_FILE_TYPE } from '@/config/constant';
|
import { ACCEPTED_FILE_TYPE } from '@/config/constant';
|
||||||
|
import { getExpenseListReturnTo } from '@/lib/expense-list-navigation';
|
||||||
import { cn } from '@/lib/helper';
|
import { cn } from '@/lib/helper';
|
||||||
import { useFormikErrorList } from '@/services/hooks/useFormikErrorList';
|
import { useFormikErrorList } from '@/services/hooks/useFormikErrorList';
|
||||||
|
|
||||||
@@ -48,6 +49,8 @@ const ExpenseRealizationForm = ({
|
|||||||
initialValues,
|
initialValues,
|
||||||
}: ExpenseRealizationFormProps) => {
|
}: ExpenseRealizationFormProps) => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const returnTo = getExpenseListReturnTo(searchParams);
|
||||||
|
|
||||||
const [expenseFormErrorMessage, setExpenseFormErrorMessage] = useState('');
|
const [expenseFormErrorMessage, setExpenseFormErrorMessage] = useState('');
|
||||||
|
|
||||||
@@ -64,9 +67,9 @@ const ExpenseRealizationForm = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
toast.success(createExpenseRes?.message as string);
|
toast.success(createExpenseRes?.message as string);
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
},
|
},
|
||||||
[router]
|
[initialValues?.id, returnTo, router]
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateExpenseHandler = useCallback(
|
const updateExpenseHandler = useCallback(
|
||||||
@@ -83,9 +86,9 @@ const ExpenseRealizationForm = ({
|
|||||||
|
|
||||||
toast.success(updateExpenseRes?.message as string);
|
toast.success(updateExpenseRes?.message as string);
|
||||||
router.refresh();
|
router.refresh();
|
||||||
router.push('/expense');
|
router.push(returnTo);
|
||||||
},
|
},
|
||||||
[router]
|
[returnTo, router]
|
||||||
);
|
);
|
||||||
|
|
||||||
const formik = useFormik<ExpenseRealizationFormValues>({
|
const formik = useFormik<ExpenseRealizationFormValues>({
|
||||||
@@ -258,7 +261,7 @@ const ExpenseRealizationForm = ({
|
|||||||
<section className='w-full'>
|
<section className='w-full'>
|
||||||
<header className='flex flex-col gap-4'>
|
<header className='flex flex-col gap-4'>
|
||||||
<Button
|
<Button
|
||||||
href='/expense'
|
href={returnTo}
|
||||||
variant='link'
|
variant='link'
|
||||||
className='w-fit p-0 text-primary'
|
className='w-fit p-0 text-primary'
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
type SearchParamsLike = {
|
||||||
|
get: (name: string) => string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const EXPENSE_LIST_PATH = '/expense';
|
||||||
|
|
||||||
|
export const getExpenseListReturnTo = (searchParams: SearchParamsLike) => {
|
||||||
|
const existingReturnTo = searchParams.get('returnTo');
|
||||||
|
|
||||||
|
if (existingReturnTo?.startsWith(EXPENSE_LIST_PATH)) {
|
||||||
|
return existingReturnTo;
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
const page = searchParams.get('page');
|
||||||
|
const limit = searchParams.get('limit');
|
||||||
|
|
||||||
|
if (page) params.set('page', page);
|
||||||
|
if (limit) params.set('limit', limit);
|
||||||
|
|
||||||
|
const queryString = params.toString();
|
||||||
|
|
||||||
|
return queryString
|
||||||
|
? `${EXPENSE_LIST_PATH}?${queryString}`
|
||||||
|
: EXPENSE_LIST_PATH;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const buildExpenseActionHref = (
|
||||||
|
path: string,
|
||||||
|
expenseId: number | string,
|
||||||
|
searchParams: SearchParamsLike
|
||||||
|
) => {
|
||||||
|
const params = new URLSearchParams({
|
||||||
|
expenseId: String(expenseId),
|
||||||
|
returnTo: getExpenseListReturnTo(searchParams),
|
||||||
|
});
|
||||||
|
|
||||||
|
return `${path}?${params.toString()}`;
|
||||||
|
};
|
||||||
@@ -154,7 +154,7 @@ export function useTableFilter<TExtra extends Record<string, unknown>>(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const updateFilter = useCallback(
|
const updateFilter = useCallback(
|
||||||
<K extends keyof TExtra>(key: K, value: TExtra[K], resetPage = true) => {
|
<K extends keyof TExtra>(key: K, value: TExtra[K], resetPage = false) => {
|
||||||
dispatch({ type: 'UPDATE_FILTER', key, value, resetPage });
|
dispatch({ type: 'UPDATE_FILTER', key, value, resetPage });
|
||||||
},
|
},
|
||||||
[dispatch]
|
[dispatch]
|
||||||
|
|||||||
Reference in New Issue
Block a user