Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Feb 13, 2025
1 parent 664f4f5 commit ed570ca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
7 changes: 0 additions & 7 deletions src/context/ActionViewContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ export type ActionViewContextType = Omit<
setTreeType?: (value: TreeType) => void;
sortState?: ColumnState[];
setSortState?: (value: ColumnState[] | undefined) => void;
pageSize: number;
setPageSize?: (value: number) => void;
currentPage?: number;
setCurrentPage?: (value: number) => void;
};
Expand Down Expand Up @@ -161,7 +159,6 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
);
const [title, setTitle] = useState<string>(titleProps);

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

useEffect(() => {
Expand Down Expand Up @@ -270,8 +267,6 @@ const ActionViewProvider = (props: ActionViewProviderProps): any => {
setTreeType,
sortState,
setSortState,
pageSize,
setPageSize,
currentPage,
setCurrentPage,
}}
Expand Down Expand Up @@ -354,8 +349,6 @@ export const useActionViewContext = () => {
setTreeType: () => {},
sortState: undefined,
setSortState: () => {},
pageSize: DEFAULT_PAGE_SIZE,
setPageSize: () => {},
currentPage: 1,
setCurrentPage: () => {},
};
Expand Down
48 changes: 23 additions & 25 deletions src/hooks/usePaginatedSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getColorMap,
getStatusMap,
getTableItems,
getTree,
getSortedFieldsFromState,
getOrderFromSortFields,
} from "@/helpers/treeHelper";
Expand Down Expand Up @@ -79,17 +78,16 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
isActive,
currentPage,
setCurrentPage,
pageSize,
setPageSize,
sortState: actionViewSortState,
setSortState: setActionViewSortState,
limit,
setLimit,
} = useSearchTreeState({ useLocalState: !rootTree });

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

// Refs
const nameSearch = nameSearchProps || searchTreeNameSearch;
Expand Down Expand Up @@ -280,7 +278,7 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
}, [
treeView,
treeOoui,
pageSize,
limit,
currentPage,
mergedParams,
nameSearch,
Expand Down Expand Up @@ -349,8 +347,8 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {

const { results, attrsEvaluated } = await searchForTree({
params,
limit: pageSize,
offset: ((currentPage || 1) - 1) * pageSize,
limit,
offset: ((currentPage || 1) - 1) * limit,
model,
fields: treeView!.field_parent
? { ...treeView!.fields, [treeView!.field_parent]: {} }
Expand Down Expand Up @@ -410,24 +408,24 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
lastAssignedResults.current = [...preparedResults];
setResults([...preparedResults]);
}, [
context,
currentPage,
treeOoui,
treeViewFetching,
setTreeIsLoading,
actionViewSortState,
nameSearch,
domain,
mergedParams,
model,
mustUpdateTotal,
nameSearch,
pageSize,
searchForTree,
setActionViewResults,
setSearchQuery,
setTotalItemsActionView,
setTreeIsLoading,
treeOoui,
limit,
currentPage,
model,
treeView,
treeViewFetching,
context,
setSearchQuery,
setActionViewResults,
mustUpdateTotal,
updateTotalRows,
actionViewSortState,
setTotalItemsActionView,
]);

const refresh = useCallback(async () => {
Expand All @@ -448,9 +446,9 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
setTreeFirstVisibleRow(0);
setSelectedRowItems([]);
setCurrentPage(page);
pageSize && setPageSize(pageSize);
pageSize && setLimit(pageSize);
},
[setCurrentPage, setPageSize, setSelectedRowItems, setTreeFirstVisibleRow],
[setCurrentPage, setLimit, setSelectedRowItems, setTreeFirstVisibleRow],
);

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

const headerCheckboxState: CheckboxState = useMemo(() => {
if (selectedRowKeys.length === 0) return "unchecked";
if (selectedRowKeys.length === pageSize && pageSize > 0) return "checked";
if (selectedRowKeys.length === limit && limit > 0) return "checked";
if (selectedRowKeys.length === totalRows) return "checked";
return "indeterminate";
}, [selectedRowKeys, pageSize, totalRows]);
}, [selectedRowKeys, limit, totalRows]);

const onHeaderCheckboxClick = useCallback(() => {
if (headerCheckboxState === "unchecked") {
Expand Down Expand Up @@ -530,7 +528,7 @@ export const usePaginatedSearch = (props: PaginatedSearchProps) => {
getColumnState,
updateColumnState,
currentPage,
pageSize,
limit,
sortState: actionViewSortState,
setSortState: setActionViewSortState,
treeFirstVisibleColumn,
Expand Down
31 changes: 15 additions & 16 deletions src/hooks/useSearchTreeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@/context/ActionViewContext";
import { ColumnState } from "@gisce/react-formiga-table";
import { DEFAULT_PAGE_SIZE } from "@/hooks/usePaginatedSearch";
import { DEFAULT_SEARCH_LIMIT } from "@/models/constants";
import {
DEFAULT_TREE_TYPE,
TreeType,
Expand Down Expand Up @@ -35,14 +36,14 @@ export type SearchTreeState = {
totalItems: number;
setTotalItems: (value: number) => void;
isActive?: boolean;
sortState?: ColumnState[];
setSortState: (value: ColumnState[] | undefined) => void;
pageSize: number;
setPageSize: (value: number) => void;
order?: ColumnState[];
setOrder: (value: ColumnState[] | undefined) => void;
currentPage: number;
setCurrentPage: (value: number) => void;
treeType: TreeType;
setTreeType: (value: TreeType) => void;
limit: number;
setLimit: (value: number) => void;
};

export function useSearchTreeState({
Expand All @@ -68,13 +69,11 @@ export function useSearchTreeState({
const [localResults, setLocalResults] = useState<any[]>([]);
const [localSearchQuery, setLocalSearchQuery] = useState<SearchQueryParams>();
const [localTotalItems, setLocalTotalItems] = useState(0);
const [localSortState, setLocalSortState] = useState<
ColumnState[] | undefined
>();
const [localPageSize, setLocalPageSize] = useState<number>(DEFAULT_PAGE_SIZE);
const [localOrder, setLocalOrder] = useState<ColumnState[] | undefined>();
const [localCurrentPage, setLocalCurrentPage] = useState<number>(1);
const [localTreeType, setLocalTreeType] =
useState<TreeType>(DEFAULT_TREE_TYPE);
const [localLimit, setLocalLimit] = useState<number>(DEFAULT_SEARCH_LIMIT);

// Return either context values or local state values based on isUnderActionViewContext
return isUnderActionViewContext
Expand Down Expand Up @@ -106,14 +105,14 @@ export function useSearchTreeState({
totalItems: actionViewContext.totalItems ?? 0,
setTotalItems: actionViewContext.setTotalItems ?? (() => {}),
isActive: actionViewContext.isActive,
sortState: actionViewContext.sortState,
setSortState: actionViewContext.setSortState ?? (() => {}),
pageSize: actionViewContext.pageSize ?? DEFAULT_PAGE_SIZE,
setPageSize: actionViewContext.setPageSize ?? (() => {}),
order: actionViewContext.order,
setOrder: actionViewContext.setOrder ?? (() => {}),
currentPage: actionViewContext.currentPage ?? 1,
setCurrentPage: actionViewContext.setCurrentPage ?? (() => {}),
treeType: actionViewContext.treeType ?? DEFAULT_TREE_TYPE,
setTreeType: actionViewContext.setTreeType ?? (() => {}),
limit: actionViewContext.limit ?? DEFAULT_SEARCH_LIMIT,
setLimit: actionViewContext.setLimit ?? (() => {}),
}
: {
treeIsLoading: localTreeIsLoading,
Expand All @@ -139,13 +138,13 @@ export function useSearchTreeState({
totalItems: localTotalItems,
setTotalItems: setLocalTotalItems,
isActive: undefined,
sortState: localSortState,
setSortState: setLocalSortState,
pageSize: localPageSize,
setPageSize: setLocalPageSize,
order: localOrder,
setOrder: setLocalOrder,
currentPage: localCurrentPage,
setCurrentPage: setLocalCurrentPage,
treeType: localTreeType,
setTreeType: setLocalTreeType,
limit: localLimit,
setLimit: setLocalLimit,
};
}
5 changes: 4 additions & 1 deletion src/hooks/useUrlFromCurrentTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function useUrlFromCurrentTab({
}: {
currentTab?: Tab;
}): UseUrlFromCurrentTabResult {
const { currentView, searchParams, currentId } = useActionViewContext();
const { currentView, searchParams, currentId, limit, currentPage } =
useActionViewContext();
const { currentTab: currentTabContext } = useTabs();

const currentTab = currentTabProps || currentTabContext;
Expand All @@ -33,6 +34,8 @@ export function useUrlFromCurrentTab({
...(initialView && { initialView }),
...(searchParams && { searchParams }),
...(currentId && { res_id: currentId }),
...(limit && { limit }),
...(currentPage && currentPage > 1 && { currentPage }),
};

const shareUrl = createShareOpenUrl(finalActionData);
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/views/Tree/Paginated/SearchTreePaginated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function SearchTreePaginatedComp(props: SearchTreePaginatedProps, ref: any) {
getColumnState,
updateColumnState,
currentPage,
pageSize,
limit,
sortState: actionViewSortState,
setSortState: setActionViewSortState,
setTreeFirstVisibleColumn,
Expand Down Expand Up @@ -345,7 +345,7 @@ function SearchTreePaginatedComp(props: SearchTreePaginatedProps, ref: any) {
total={totalRows || 0}
totalRowsLoading={totalRowsLoading}
initialPage={currentPage || 1}
initialPageSize={pageSize || DEFAULT_PAGE_SIZE}
initialPageSize={limit || DEFAULT_PAGE_SIZE}
currentPageSelectedCount={selectedRowKeys.length}
onRequestPageChange={onRequestPageChange}
totalSelectedCount={selectedRowKeys.length}
Expand Down

0 comments on commit ed570ca

Please sign in to comment.