Skip to content

Commit ed570ca

Browse files
committed
feat: wip
1 parent 664f4f5 commit ed570ca

File tree

5 files changed

+44
-51
lines changed

5 files changed

+44
-51
lines changed

src/context/ActionViewContext.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ export type ActionViewContextType = Omit<
8080
setTreeType?: (value: TreeType) => void;
8181
sortState?: ColumnState[];
8282
setSortState?: (value: ColumnState[] | undefined) => void;
83-
pageSize: number;
84-
setPageSize?: (value: number) => void;
8583
currentPage?: number;
8684
setCurrentPage?: (value: number) => void;
8785
};
@@ -161,7 +159,6 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
161159
);
162160
const [title, setTitle] = useState<string>(titleProps);
163161

164-
const [pageSize, setPageSize] = useState<number>(DEFAULT_PAGE_SIZE);
165162
const [currentPage, setCurrentPage] = useState<number>(1);
166163

167164
useEffect(() => {
@@ -270,8 +267,6 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
270267
setTreeType,
271268
sortState,
272269
setSortState,
273-
pageSize,
274-
setPageSize,
275270
currentPage,
276271
setCurrentPage,
277272
}}
@@ -354,8 +349,6 @@ export const useActionViewContext = () => {
354349
setTreeType: () => {},
355350
sortState: undefined,
356351
setSortState: () => {},
357-
pageSize: DEFAULT_PAGE_SIZE,
358-
setPageSize: () => {},
359352
currentPage: 1,
360353
setCurrentPage: () => {},
361354
};

src/hooks/usePaginatedSearch.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
getColorMap,
1919
getStatusMap,
2020
getTableItems,
21-
getTree,
2221
getSortedFieldsFromState,
2322
getOrderFromSortFields,
2423
} from "@/helpers/treeHelper";
@@ -79,17 +78,16 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
7978
isActive,
8079
currentPage,
8180
setCurrentPage,
82-
pageSize,
83-
setPageSize,
8481
sortState: actionViewSortState,
8582
setSortState: setActionViewSortState,
83+
limit,
84+
setLimit,
8685
} = useSearchTreeState({ useLocalState: !rootTree });
8786

8887
// Local state
8988
const [totalRowsLoading, setTotalRowsLoading] = useState<boolean>(true);
9089
const [totalRows, setTotalRows] = useState<number | null>();
9190
const [results, setResults] = useState<any[]>([]);
92-
const hasRestoredSortStateForFirstTime = useRef<boolean>(false);
9391

9492
// Refs
9593
const nameSearch = nameSearchProps || searchTreeNameSearch;
@@ -280,7 +278,7 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
280278
}, [
281279
treeView,
282280
treeOoui,
283-
pageSize,
281+
limit,
284282
currentPage,
285283
mergedParams,
286284
nameSearch,
@@ -349,8 +347,8 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
349347

350348
const { results, attrsEvaluated } = await searchForTree({
351349
params,
352-
limit: pageSize,
353-
offset: ((currentPage || 1) - 1) * pageSize,
350+
limit,
351+
offset: ((currentPage || 1) - 1) * limit,
354352
model,
355353
fields: treeView!.field_parent
356354
? { ...treeView!.fields, [treeView!.field_parent]: {} }
@@ -410,24 +408,24 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
410408
lastAssignedResults.current = [...preparedResults];
411409
setResults([...preparedResults]);
412410
}, [
413-
context,
414-
currentPage,
411+
treeOoui,
412+
treeViewFetching,
413+
setTreeIsLoading,
414+
actionViewSortState,
415+
nameSearch,
415416
domain,
416417
mergedParams,
417-
model,
418-
mustUpdateTotal,
419-
nameSearch,
420-
pageSize,
421418
searchForTree,
422-
setActionViewResults,
423-
setSearchQuery,
424-
setTotalItemsActionView,
425-
setTreeIsLoading,
426-
treeOoui,
419+
limit,
420+
currentPage,
421+
model,
427422
treeView,
428-
treeViewFetching,
423+
context,
424+
setSearchQuery,
425+
setActionViewResults,
426+
mustUpdateTotal,
429427
updateTotalRows,
430-
actionViewSortState,
428+
setTotalItemsActionView,
431429
]);
432430

433431
const refresh = useCallback(async () => {
@@ -448,9 +446,9 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
448446
setTreeFirstVisibleRow(0);
449447
setSelectedRowItems([]);
450448
setCurrentPage(page);
451-
pageSize && setPageSize(pageSize);
449+
pageSize && setLimit(pageSize);
452450
},
453-
[setCurrentPage, setPageSize, setSelectedRowItems, setTreeFirstVisibleRow],
451+
[setCurrentPage, setLimit, setSelectedRowItems, setTreeFirstVisibleRow],
454452
);
455453

456454
const getAllIds = useCallback(async () => {
@@ -470,10 +468,10 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
470468

471469
const headerCheckboxState: CheckboxState = useMemo(() => {
472470
if (selectedRowKeys.length === 0) return "unchecked";
473-
if (selectedRowKeys.length === pageSize && pageSize > 0) return "checked";
471+
if (selectedRowKeys.length === limit && limit > 0) return "checked";
474472
if (selectedRowKeys.length === totalRows) return "checked";
475473
return "indeterminate";
476-
}, [selectedRowKeys, pageSize, totalRows]);
474+
}, [selectedRowKeys, limit, totalRows]);
477475

478476
const onHeaderCheckboxClick = useCallback(() => {
479477
if (headerCheckboxState === "unchecked") {
@@ -530,7 +528,7 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
530528
getColumnState,
531529
updateColumnState,
532530
currentPage,
533-
pageSize,
531+
limit,
534532
sortState: actionViewSortState,
535533
setSortState: setActionViewSortState,
536534
treeFirstVisibleColumn,

src/hooks/useSearchTreeState.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "@/context/ActionViewContext";
77
import { ColumnState } from "@gisce/react-formiga-table";
88
import { DEFAULT_PAGE_SIZE } from "@/hooks/usePaginatedSearch";
9+
import { DEFAULT_SEARCH_LIMIT } from "@/models/constants";
910
import {
1011
DEFAULT_TREE_TYPE,
1112
TreeType,
@@ -35,14 +36,14 @@ export type SearchTreeState = {
3536
totalItems: number;
3637
setTotalItems: (value: number) => void;
3738
isActive?: boolean;
38-
sortState?: ColumnState[];
39-
setSortState: (value: ColumnState[] | undefined) => void;
40-
pageSize: number;
41-
setPageSize: (value: number) => void;
39+
order?: ColumnState[];
40+
setOrder: (value: ColumnState[] | undefined) => void;
4241
currentPage: number;
4342
setCurrentPage: (value: number) => void;
4443
treeType: TreeType;
4544
setTreeType: (value: TreeType) => void;
45+
limit: number;
46+
setLimit: (value: number) => void;
4647
};
4748

4849
export function useSearchTreeState({
@@ -68,13 +69,11 @@ export function useSearchTreeState({
6869
const [localResults, setLocalResults] = useState<any[]>([]);
6970
const [localSearchQuery, setLocalSearchQuery] = useState<SearchQueryParams>();
7071
const [localTotalItems, setLocalTotalItems] = useState(0);
71-
const [localSortState, setLocalSortState] = useState<
72-
ColumnState[] | undefined
73-
>();
74-
const [localPageSize, setLocalPageSize] = useState<number>(DEFAULT_PAGE_SIZE);
72+
const [localOrder, setLocalOrder] = useState<ColumnState[] | undefined>();
7573
const [localCurrentPage, setLocalCurrentPage] = useState<number>(1);
7674
const [localTreeType, setLocalTreeType] =
7775
useState<TreeType>(DEFAULT_TREE_TYPE);
76+
const [localLimit, setLocalLimit] = useState<number>(DEFAULT_SEARCH_LIMIT);
7877

7978
// Return either context values or local state values based on isUnderActionViewContext
8079
return isUnderActionViewContext
@@ -106,14 +105,14 @@ export function useSearchTreeState({
106105
totalItems: actionViewContext.totalItems ?? 0,
107106
setTotalItems: actionViewContext.setTotalItems ?? (() => {}),
108107
isActive: actionViewContext.isActive,
109-
sortState: actionViewContext.sortState,
110-
setSortState: actionViewContext.setSortState ?? (() => {}),
111-
pageSize: actionViewContext.pageSize ?? DEFAULT_PAGE_SIZE,
112-
setPageSize: actionViewContext.setPageSize ?? (() => {}),
108+
order: actionViewContext.order,
109+
setOrder: actionViewContext.setOrder ?? (() => {}),
113110
currentPage: actionViewContext.currentPage ?? 1,
114111
setCurrentPage: actionViewContext.setCurrentPage ?? (() => {}),
115112
treeType: actionViewContext.treeType ?? DEFAULT_TREE_TYPE,
116113
setTreeType: actionViewContext.setTreeType ?? (() => {}),
114+
limit: actionViewContext.limit ?? DEFAULT_SEARCH_LIMIT,
115+
setLimit: actionViewContext.setLimit ?? (() => {}),
117116
}
118117
: {
119118
treeIsLoading: localTreeIsLoading,
@@ -139,13 +138,13 @@ export function useSearchTreeState({
139138
totalItems: localTotalItems,
140139
setTotalItems: setLocalTotalItems,
141140
isActive: undefined,
142-
sortState: localSortState,
143-
setSortState: setLocalSortState,
144-
pageSize: localPageSize,
145-
setPageSize: setLocalPageSize,
141+
order: localOrder,
142+
setOrder: setLocalOrder,
146143
currentPage: localCurrentPage,
147144
setCurrentPage: setLocalCurrentPage,
148145
treeType: localTreeType,
149146
setTreeType: setLocalTreeType,
147+
limit: localLimit,
148+
setLimit: setLocalLimit,
150149
};
151150
}

src/hooks/useUrlFromCurrentTab.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export function useUrlFromCurrentTab({
1313
}: {
1414
currentTab?: Tab;
1515
}): UseUrlFromCurrentTabResult {
16-
const { currentView, searchParams, currentId } = useActionViewContext();
16+
const { currentView, searchParams, currentId, limit, currentPage } =
17+
useActionViewContext();
1718
const { currentTab: currentTabContext } = useTabs();
1819

1920
const currentTab = currentTabProps || currentTabContext;
@@ -33,6 +34,8 @@ export function useUrlFromCurrentTab({
3334
...(initialView && { initialView }),
3435
...(searchParams && { searchParams }),
3536
...(currentId && { res_id: currentId }),
37+
...(limit && { limit }),
38+
...(currentPage && currentPage > 1 && { currentPage }),
3639
};
3740

3841
const shareUrl = createShareOpenUrl(finalActionData);

src/widgets/views/Tree/Paginated/SearchTreePaginated.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function SearchTreePaginatedComp(props: SearchTreePaginatedProps, ref: any) {
144144
getColumnState,
145145
updateColumnState,
146146
currentPage,
147-
pageSize,
147+
limit,
148148
sortState: actionViewSortState,
149149
setSortState: setActionViewSortState,
150150
setTreeFirstVisibleColumn,
@@ -345,7 +345,7 @@ function SearchTreePaginatedComp(props: SearchTreePaginatedProps, ref: any) {
345345
total={totalRows || 0}
346346
totalRowsLoading={totalRowsLoading}
347347
initialPage={currentPage || 1}
348-
initialPageSize={pageSize || DEFAULT_PAGE_SIZE}
348+
initialPageSize={limit || DEFAULT_PAGE_SIZE}
349349
currentPageSelectedCount={selectedRowKeys.length}
350350
onRequestPageChange={onRequestPageChange}
351351
totalSelectedCount={selectedRowKeys.length}

0 commit comments

Comments
 (0)