mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-19 17:52:45 +02:00
fix: limit reset when no pagination bar (#10634)
This commit is contained in:
parent
2cd8135988
commit
6198900014
@ -1,5 +1,4 @@
|
|||||||
import type React from 'react';
|
import type React from 'react';
|
||||||
import { useEffect } from 'react';
|
|
||||||
import { Box, Typography, Button, styled } from '@mui/material';
|
import { Box, Typography, Button, styled } from '@mui/material';
|
||||||
import { ConditionallyRender } from '../../ConditionallyRender/ConditionallyRender.tsx';
|
import { ConditionallyRender } from '../../ConditionallyRender/ConditionallyRender.tsx';
|
||||||
import { ReactComponent as ArrowRight } from 'assets/icons/arrowRight.svg';
|
import { ReactComponent as ArrowRight } from 'assets/icons/arrowRight.svg';
|
||||||
@ -61,12 +60,6 @@ export const PaginationBar: React.FC<PaginationBarProps> = ({
|
|||||||
fetchNextPage,
|
fetchNextPage,
|
||||||
setPageLimit,
|
setPageLimit,
|
||||||
}) => {
|
}) => {
|
||||||
useEffect(() => {
|
|
||||||
if (![25, 50, 75, 100].includes(pageSize)) {
|
|
||||||
setPageLimit(25);
|
|
||||||
}
|
|
||||||
}, [pageSize]);
|
|
||||||
|
|
||||||
const itemRange =
|
const itemRange =
|
||||||
totalItems !== undefined && pageSize && totalItems > 1
|
totalItems !== undefined && pageSize && totalItems > 1
|
||||||
? `${pageIndex * pageSize + 1}-${Math.min(
|
? `${pageIndex * pageSize + 1}-${Math.min(
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { encodeQueryParams, StringParam, withDefault } from 'use-query-params';
|
||||||
encodeQueryParams,
|
|
||||||
NumberParam,
|
|
||||||
StringParam,
|
|
||||||
withDefault,
|
|
||||||
} from 'use-query-params';
|
|
||||||
import { FilterItemParam } from 'utils/serializeQueryParams';
|
import { FilterItemParam } from 'utils/serializeQueryParams';
|
||||||
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
||||||
import mapValues from 'lodash.mapvalues';
|
import mapValues from 'lodash.mapvalues';
|
||||||
@ -11,6 +6,7 @@ import { useEventSearch } from 'hooks/api/getters/useEventSearch/useEventSearch'
|
|||||||
import type { SearchEventsParams } from 'openapi';
|
import type { SearchEventsParams } from 'openapi';
|
||||||
import type { FilterItemParamHolder } from 'component/filter/Filters/Filters';
|
import type { FilterItemParamHolder } from 'component/filter/Filters/Filters';
|
||||||
import { format, subYears } from 'date-fns';
|
import { format, subYears } from 'date-fns';
|
||||||
|
import { SafeNumberParam } from 'utils/safeNumberParam';
|
||||||
|
|
||||||
type Log =
|
type Log =
|
||||||
| { type: 'global' }
|
| { type: 'global' }
|
||||||
@ -58,8 +54,8 @@ export const useEventLogSearch = (
|
|||||||
refreshInterval = 15 * 1000,
|
refreshInterval = 15 * 1000,
|
||||||
) => {
|
) => {
|
||||||
const stateConfig = {
|
const stateConfig = {
|
||||||
offset: withDefault(NumberParam, 0),
|
offset: withDefault(SafeNumberParam, 0),
|
||||||
limit: withDefault(NumberParam, DEFAULT_PAGE_SIZE),
|
limit: withDefault(SafeNumberParam, DEFAULT_PAGE_SIZE),
|
||||||
query: StringParam,
|
query: StringParam,
|
||||||
from: withDefault(FilterItemParam, {
|
from: withDefault(FilterItemParam, {
|
||||||
values: [format(subYears(new Date(), 1), 'yyyy-MM-dd')],
|
values: [format(subYears(new Date(), 1), 'yyyy-MM-dd')],
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import {
|
import { encodeQueryParams, StringParam, withDefault } from 'use-query-params';
|
||||||
encodeQueryParams,
|
|
||||||
NumberParam,
|
|
||||||
StringParam,
|
|
||||||
withDefault,
|
|
||||||
} from 'use-query-params';
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_PAGE_LIMIT,
|
DEFAULT_PAGE_LIMIT,
|
||||||
useFeatureSearch,
|
useFeatureSearch,
|
||||||
@ -16,12 +11,13 @@ import {
|
|||||||
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
||||||
import mapValues from 'lodash.mapvalues';
|
import mapValues from 'lodash.mapvalues';
|
||||||
import type { SearchFeaturesParams } from 'openapi';
|
import type { SearchFeaturesParams } from 'openapi';
|
||||||
|
import { SafeNumberParam } from 'utils/safeNumberParam';
|
||||||
|
|
||||||
export const useGlobalFeatureSearch = (pageLimit = DEFAULT_PAGE_LIMIT) => {
|
export const useGlobalFeatureSearch = (pageLimit = DEFAULT_PAGE_LIMIT) => {
|
||||||
const storageKey = 'features-list-table';
|
const storageKey = 'features-list-table';
|
||||||
const stateConfig = {
|
const stateConfig = {
|
||||||
offset: withDefault(NumberParam, 0),
|
offset: withDefault(SafeNumberParam, 0),
|
||||||
limit: withDefault(NumberParam, pageLimit),
|
limit: withDefault(SafeNumberParam, pageLimit),
|
||||||
query: StringParam,
|
query: StringParam,
|
||||||
favoritesFirst: withDefault(BooleansStringParam, true),
|
favoritesFirst: withDefault(BooleansStringParam, true),
|
||||||
sortBy: withDefault(StringParam, 'createdAt'),
|
sortBy: withDefault(StringParam, 'createdAt'),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ArrayParam,
|
ArrayParam,
|
||||||
encodeQueryParams,
|
encodeQueryParams,
|
||||||
NumberParam,
|
|
||||||
StringParam,
|
StringParam,
|
||||||
withDefault,
|
withDefault,
|
||||||
} from 'use-query-params';
|
} from 'use-query-params';
|
||||||
@ -16,6 +15,7 @@ import {
|
|||||||
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
import { usePersistentTableState } from 'hooks/usePersistentTableState';
|
||||||
import mapValues from 'lodash.mapvalues';
|
import mapValues from 'lodash.mapvalues';
|
||||||
import type { SearchFeaturesParams } from 'openapi';
|
import type { SearchFeaturesParams } from 'openapi';
|
||||||
|
import { SafeNumberParam } from 'utils/safeNumberParam';
|
||||||
|
|
||||||
type Attribute =
|
type Attribute =
|
||||||
| { key: 'tag'; operator: 'INCLUDE' }
|
| { key: 'tag'; operator: 'INCLUDE' }
|
||||||
@ -28,8 +28,8 @@ export const useProjectFeatureSearch = (
|
|||||||
refreshInterval = 15 * 1000,
|
refreshInterval = 15 * 1000,
|
||||||
) => {
|
) => {
|
||||||
const stateConfig = {
|
const stateConfig = {
|
||||||
offset: withDefault(NumberParam, 0),
|
offset: withDefault(SafeNumberParam, 0),
|
||||||
limit: withDefault(NumberParam, DEFAULT_PAGE_LIMIT),
|
limit: withDefault(SafeNumberParam, DEFAULT_PAGE_LIMIT),
|
||||||
query: StringParam,
|
query: StringParam,
|
||||||
favoritesFirst: withDefault(BooleansStringParam, true),
|
favoritesFirst: withDefault(BooleansStringParam, true),
|
||||||
sortBy: withDefault(StringParam, 'createdAt'),
|
sortBy: withDefault(StringParam, 'createdAt'),
|
||||||
|
@ -51,6 +51,19 @@ export const usePersistentTableState = <T extends QueryParamConfigMap>(
|
|||||||
return reorderObject(tableState, [...searchParams.keys()]);
|
return reorderObject(tableState, [...searchParams.keys()]);
|
||||||
}, [searchParams, tableState, reorderObject]);
|
}, [searchParams, tableState, reorderObject]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
tableState.limit &&
|
||||||
|
![25, 50, 75, 100].includes(tableState.limit as number)
|
||||||
|
) {
|
||||||
|
setTableStateInternal((prevState) => ({
|
||||||
|
...prevState,
|
||||||
|
limit: 25,
|
||||||
|
offset: 0, // Reset offset when changing limit
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}, [tableState.limit, setTableStateInternal]);
|
||||||
|
|
||||||
type SetTableStateInternalParam = Parameters<
|
type SetTableStateInternalParam = Parameters<
|
||||||
typeof setTableStateInternal
|
typeof setTableStateInternal
|
||||||
>[0];
|
>[0];
|
||||||
|
12
frontend/src/utils/safeNumberParam.ts
Normal file
12
frontend/src/utils/safeNumberParam.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { encodeNumber, decodeNumber } from 'serialize-query-params';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see: https://github.com/pbeshai/use-query-params/issues/175#issuecomment-982791559
|
||||||
|
*/
|
||||||
|
export const SafeNumberParam = {
|
||||||
|
encode: encodeNumber,
|
||||||
|
decode: (input: any) => {
|
||||||
|
const result = decodeNumber(input);
|
||||||
|
return result == null ? result : Number.isNaN(result) ? null : result;
|
||||||
|
},
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user