refactor(FE): Move table slice into UI store and persist search

This commit is contained in:
rstubryan
2026-01-22 15:58:56 +07:00
parent 756701722a
commit 1f6ce36976
5 changed files with 31 additions and 39 deletions
@@ -34,7 +34,7 @@ import { useTableFilter } from '@/services/hooks/useTableFilter';
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import Badge from '@/components/Badge'; import Badge from '@/components/Badge';
import CheckboxInput from '@/components/input/CheckboxInput'; import CheckboxInput from '@/components/input/CheckboxInput';
import { useTableStore } from '@/stores/table/table.store'; import { useUiStore } from '@/stores/ui/ui.store';
import { BaseApproval, BaseApiResponse } from '@/types/api/api-general'; import { BaseApproval, BaseApiResponse } from '@/types/api/api-general';
const RowOptionsMenu = ({ const RowOptionsMenu = ({
@@ -351,7 +351,7 @@ const ApprovalHistoryModal = ({
}; };
const RecordingTable = () => { const RecordingTable = () => {
const { searchValue, setSearchValue, resetSearchValue } = useTableStore(); const { searchValue, setSearchValue, resetSearchValue } = useUiStore();
const previousPathRef = useRef<string | null>(null); const previousPathRef = useRef<string | null>(null);
const { const {
-27
View File
@@ -1,27 +0,0 @@
'use client';
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { createTableSlice } from '@/stores/table/slices/table.slice';
import type { TableSlice } from '@/stores/table/slices/table.slice';
export type TableStore = TableSlice;
export const useTableStore = create<TableStore>()(
devtools(
persist(
(...args) => ({
...createTableSlice(...args),
}),
{
name: 'table-cache',
partialize: (state) => ({
searchValue: state.searchValue,
}),
}
),
{
name: 'TableStore',
}
)
);
@@ -4,15 +4,18 @@ export interface TableState {
searchValue: string; searchValue: string;
} }
export interface TableSlice { export interface TableUISlice {
searchValue: string; searchValue: string;
setSearchValue: (value: string) => void; setSearchValue: (value: string) => void;
resetSearchValue: () => void; resetSearchValue: () => void;
} }
export const createTableSlice: StateCreator<TableSlice, [], [], TableSlice> = ( export const createTableUISlice: StateCreator<
set TableUISlice,
) => ({ [],
[],
TableUISlice
> = (set) => ({
// Initial state // Initial state
searchValue: '', searchValue: '',
+11 -1
View File
@@ -1,18 +1,28 @@
'use client'; 'use client';
import { create } from 'zustand'; import { create } from 'zustand';
import { devtools } from 'zustand/middleware'; import { devtools, persist } from 'zustand/middleware';
import { UIStore } from '@/types/stores'; import { UIStore } from '@/types/stores';
import { createMainUiSlice } from '@/stores/ui/slices/main.slice'; import { createMainUiSlice } from '@/stores/ui/slices/main.slice';
import { createDrawerUISlice } from '@/stores/ui/slices/drawer.slice'; import { createDrawerUISlice } from '@/stores/ui/slices/drawer.slice';
import { createTableUISlice } from '@/stores/ui/slices/table.slice';
export const useUiStore = create<UIStore>()( export const useUiStore = create<UIStore>()(
devtools( devtools(
persist(
(...args) => ({ (...args) => ({
...createMainUiSlice(...args), ...createMainUiSlice(...args),
...createDrawerUISlice(...args), ...createDrawerUISlice(...args),
...createTableUISlice(...args),
}), }),
{
name: 'ui-cache',
partialize: (state) => ({
searchValue: state.searchValue,
}),
}
),
{ {
name: 'UIStore', name: 'UIStore',
} }
+7 -1
View File
@@ -26,7 +26,13 @@ type DrawerUISlice = {
setIsNextStep: (v: boolean) => void; setIsNextStep: (v: boolean) => void;
}; };
export type UIStore = MainUiSlice & DrawerUISlice; type TableUISlice = {
searchValue: string;
setSearchValue: (value: string) => void;
resetSearchValue: () => void;
};
export type UIStore = MainUiSlice & DrawerUISlice & TableUISlice;
type ProductionStandardFormSlice = { type ProductionStandardFormSlice = {
formData: { formData: {