Skip to content

Commit 6509582

Browse files
committed
feat: allow setting identifiers internally in bulk selection provider
change ApiQueuingWithBatch call signature; find http protocol internally
1 parent a0e2669 commit 6509582

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/Shared/API/APIQueuing.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ const eachCall = (batchConfig, functionCalls, resolve, reject, shouldRejectOnErr
5454
*/
5555
const ApiQueuingWithBatch = <T>(
5656
functionCalls,
57-
httpProtocol: string,
5857
shouldRejectOnError: boolean = false,
5958
batchSize: number = window._env_.API_BATCH_SIZE,
6059
): Promise<ApiQueuingWithBatchResponseItem<T>[]> => {
60+
const httpProtocol = (window.performance.getEntriesByType('resource') as PerformanceResourceTiming[]).reduce(
61+
(fallbackProtocol, entry) => entry.nextHopProtocol ?? fallbackProtocol,
62+
'http/1.0',
63+
)
64+
6165
if (!batchSize || batchSize <= 0) {
6266
// eslint-disable-next-line no-param-reassign
6367
batchSize = ['http/0.9', 'http/1.0', 'http/1.1'].indexOf(httpProtocol) !== -1 ? 5 : 30

src/Shared/Components/BulkSelection/BulkSelectionProvider.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
UseBulkSelectionProps,
3131
UseBulkSelectionReturnType,
3232
} from './types'
33-
import { CHECKBOX_VALUE, noop } from '../../../Common'
33+
import { CHECKBOX_VALUE, noop, useEffectAfterMount } from '../../../Common'
3434

3535
// giving type any here since not exporting this context, rather using it through useBulkSelection hook which is typed
3636
const BulkSelectionContext = createContext<UseBulkSelectionReturnType<any>>({
@@ -39,6 +39,7 @@ const BulkSelectionContext = createContext<UseBulkSelectionReturnType<any>>({
3939
isChecked: false,
4040
checkboxValue: CHECKBOX_VALUE.CHECKED,
4141
isBulkSelectionApplied: false,
42+
setIdentifiers: noop,
4243
getSelectedIdentifiersCount: noop,
4344
})
4445

@@ -52,10 +53,17 @@ export const useBulkSelection = <T,>() => {
5253

5354
export const BulkSelectionProvider = <T,>({
5455
children,
55-
identifiers,
56+
identifiers = null,
5657
getSelectAllDialogStatus,
5758
}: UseBulkSelectionProps<T>) => {
5859
const [selectedIdentifiers, setSelectedIdentifiers] = useState<T>({} as T)
60+
const [identifiersOnCurrentPage, setIdentifiersOnCurrentPage] = useState(identifiers ?? ({} as T))
61+
62+
useEffectAfterMount(() => {
63+
if (identifiers) {
64+
setIdentifiersOnCurrentPage(identifiers)
65+
}
66+
}, [identifiers])
5967

6068
const isBulkSelectionApplied = selectedIdentifiers[SELECT_ALL_ACROSS_PAGES_LOCATOR]
6169

@@ -96,7 +104,7 @@ export const BulkSelectionProvider = <T,>({
96104
variant: ToastVariantType.info,
97105
description: CLEAR_SELECTIONS_WARNING,
98106
})
99-
setIdentifiersAfterClear(identifiers, selectedIds)
107+
setIdentifiersAfterClear(identifiersOnCurrentPage, selectedIds)
100108
break
101109
}
102110

@@ -137,7 +145,7 @@ export const BulkSelectionProvider = <T,>({
137145
})
138146
}
139147

140-
setIdentifiersAfterPageSelection(identifiers)
148+
setIdentifiersAfterPageSelection(identifiersOnCurrentPage)
141149
break
142150
}
143151

@@ -169,7 +177,7 @@ export const BulkSelectionProvider = <T,>({
169177
}
170178

171179
// if all the identifiers are selected then CHECKED else intermediate
172-
const areAllPresentIdentifiersSelected = Object.keys(identifiers).every(
180+
const areAllPresentIdentifiersSelected = Object.keys(identifiersOnCurrentPage).every(
173181
(identifierId) => selectedIdentifiers[identifierId],
174182
)
175183

@@ -196,8 +204,17 @@ export const BulkSelectionProvider = <T,>({
196204
checkboxValue,
197205
isBulkSelectionApplied,
198206
getSelectedIdentifiersCount,
207+
setIdentifiers: setIdentifiersOnCurrentPage,
199208
}),
200-
[selectedIdentifiers, handleBulkSelection, isChecked, checkboxValue, getSelectedIdentifiersCount],
209+
[
210+
selectedIdentifiers,
211+
handleBulkSelection,
212+
isChecked,
213+
checkboxValue,
214+
getSelectedIdentifiersCount,
215+
identifiersOnCurrentPage,
216+
isBulkSelectionApplied,
217+
],
201218
)
202219

203220
return <BulkSelectionContext.Provider value={value}>{children}</BulkSelectionContext.Provider>

src/Shared/Components/BulkSelection/types.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface UseBulkSelectionReturnType<T> extends GetBulkSelectionCheckboxV
4444
handleBulkSelection: ({ action, data }: HandleBulkSelectionType<T>) => void
4545
isBulkSelectionApplied: boolean
4646
getSelectedIdentifiersCount: () => number
47+
setIdentifiers: React.Dispatch<React.SetStateAction<T>>
4748
}
4849

4950
export interface BulkSelectionProps {
@@ -72,14 +73,16 @@ export enum SelectAllDialogStatus {
7273
export type BulkSelectionIdentifiersType<T> = Record<string | number, T>
7374

7475
export interface UseBulkSelectionProps<T> {
75-
/**
76-
* Response from API, assuming structure to be array of objects with key and values
77-
* This will the given ids on current page
78-
*/
79-
identifiers: T
8076
/**
8177
* Act as buffer between select all across pages and select all on page state
8278
*/
8379
getSelectAllDialogStatus: () => SelectAllDialogStatus
80+
/**
81+
* Response from API, assuming structure to be array of objects with key and values
82+
* This will the given ids on current page.
83+
*
84+
* NOTE!: Please wrap this value with a useMemo since it goes into useEffect dependency
85+
*/
86+
identifiers?: T
8487
children?: React.ReactNode
8588
}

0 commit comments

Comments
 (0)