-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexploreState.tsx
More file actions
782 lines (749 loc) · 22.1 KB
/
exploreState.tsx
File metadata and controls
782 lines (749 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import { RowSelectionState } from "@tanstack/react-table";
import React, {
createContext,
Dispatch,
ReactNode,
useEffect,
useMemo,
useReducer,
useState,
} from "react";
import { AzulSearchIndex } from "../apis/azul/common/entities";
import { CategoryView } from "../common/categories/views/types";
import { SelectedFilter } from "../common/entities";
import { FILTER_SORT } from "../common/filters/sort/config/types";
import { RowPreviewState } from "../components/Table/features/RowPreview/entities";
import { CategoryGroup, SiteConfig } from "../config/entities";
import { useToken } from "../hooks/authentication/token/useToken";
import { META_COMMAND } from "../hooks/stateSyncManager/hooks/UseMetaCommands/types";
import {
buildCategoryViews,
buildNextFilterState,
} from "../hooks/useCategoryFilter";
import { useConfig } from "../hooks/useConfig";
import { useURLFilterParams } from "../hooks/useURLFilterParams";
import { clearMetaAction } from "./exploreState/actions/clearMeta/action";
import { ClearMetaAction } from "./exploreState/actions/clearMeta/types";
import { stateToUrlAction } from "./exploreState/actions/stateToUrl/action";
import { StateToUrlAction } from "./exploreState/actions/stateToUrl/types";
import { updateFilterSortAction } from "./exploreState/actions/updateFilterSort/action";
import { UpdateFilterSortAction } from "./exploreState/actions/updateFilterSort/types";
import { updateGroupingAction } from "./exploreState/actions/updateGrouping/action";
import { UpdateGroupingAction } from "./exploreState/actions/updateGrouping/types";
import { updateColumnVisibilityAction } from "./exploreState/actions/updateVisibility/action";
import { UpdateColumnVisibilityAction } from "./exploreState/actions/updateVisibility/types";
import { urlToStateAction } from "./exploreState/actions/urlToState/action";
import { UrlToStateAction } from "./exploreState/actions/urlToState/types";
import {
EntityPageStateMapper,
EntityStateByCategoryGroupConfigKey,
ListItem,
Meta,
} from "./exploreState/entities";
import { buildNextEntities } from "./exploreState/entities/state";
import { EntitiesContext } from "./exploreState/entities/types";
import {
DEFAULT_PAGINATION_STATE,
INITIAL_STATE,
} from "./exploreState/initializer/constants";
import { initReducerArguments } from "./exploreState/initializer/utils";
import {
ApplySavedFilterPayload,
PaginateTablePayload,
PatchExploreResponsePayload,
ProcessExploreResponsePayload,
ResetExploreResponsePayload,
UpdateEntityFiltersPayload,
UpdateEntityViewAccessPayload,
UpdateFilterPayload,
UpdateRowPreviewPayload,
UpdateRowSelectionPayload,
UpdateSortingPayload,
} from "./exploreState/payloads/entities";
import {
buildEntityStateSavedFilterState,
buildNextSavedFilterState,
closeRowPreview,
getEntityCategoryGroupConfigKey,
getEntityState,
getEntityStateSavedProperty,
getFilterCount,
patchEntityListItems,
resetPage,
updateEntityPageState,
updateEntityPageStateWithCommonCategoryGroupConfigKey,
updateEntityStateByCategoryGroupConfigKey,
updateSelectColumnVisibility,
} from "./exploreState/utils";
export type CatalogState = string | undefined;
/**
* Explore context.
*/
export interface ExploreContext {
config: SiteConfig;
entityList: string;
}
/**
* Explore state.
*/
export type ExploreState = {
catalogState: CatalogState;
categoryGroups?: CategoryGroup[];
categoryViews: CategoryView[];
entities: EntitiesContext;
entityPageState: EntityPageStateMapper;
entityStateByCategoryGroupConfigKey: EntityStateByCategoryGroupConfigKey;
featureFlagState: FeatureFlagState;
filterCount: number;
filterSort: FILTER_SORT;
filterState: SelectedFilter[];
listItems: ListItems;
loading: boolean;
meta: Meta | null;
paginationState: PaginationState;
rowPreview: RowPreviewState;
tabValue: string;
};
/**
* Model of explore state context.
*/
export interface ExploreStateContextProps {
exploreDispatch: Dispatch<ExploreAction>;
exploreState: ExploreState;
}
export type FeatureFlagState = string | undefined;
/**
* List items.
*/
export type ListItems = ListItem[] | undefined;
/**
* Pagination index.
*/
export interface PaginationIndex {
type: AzulSearchIndex;
value: string | null;
}
/**
* Pagination response.
*/
export interface PaginationResponse {
nextIndex: PaginationIndex | null;
pages: number;
pageSize: number;
previousIndex: PaginationIndex | null;
rows: number;
}
/**
* Pagination state.
*/
export interface PaginationState {
currentPage: number;
index: PaginationIndex | null;
nextIndex: PaginationIndex | null;
pages: number;
pageSize: number;
previousIndex: PaginationIndex | null;
rows: number;
}
/**
* Explore state context for storing and using filter-related and explore state.
*/
export const ExploreStateContext = createContext<ExploreStateContextProps>({
/**
* The defaultValue argument is only used when a component does not have a matching Provider
* above it in the tree. This default value can be helpful for testing components
* in isolation without wrapping them. Note: passing undefined as a Provider value
* does not cause consuming components to use defaultValue.
* So basically the default value is not used...
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function -- default note used
exploreDispatch: () => {},
exploreState: INITIAL_STATE,
});
/**
* Explore state provider for consuming components to subscribe to changes in filter-related and explore-related state.
* @param props - Component inputs.
* @param props.children - Set of children components that can possibly consume the query provider.
* @param props.entityListType - type of list to display
* @returns Provider element to be used by consumers to both update explore state and subscribe to changes in explore state.
*/
export function ExploreStateProvider({
children,
entityListType,
}: {
children: ReactNode | ReactNode[];
entityListType: string;
}): JSX.Element {
const { config, defaultEntityListType } = useConfig();
const { decodedCatalogParam, decodedFeatureFlagParam, decodedFilterParam } =
useURLFilterParams();
const { token } = useToken();
const entityList = entityListType || defaultEntityListType;
const [initializerArg] = useState(() =>
initReducerArguments(
config,
entityList,
decodedFilterParam,
decodedCatalogParam,
decodedFeatureFlagParam
)
);
const [exploreState, exploreDispatch] = useReducer(
(s: ExploreState, a: ExploreAction) =>
exploreReducer(s, a, {
config,
entityList,
}),
initializerArg
);
// does this help? https://hswolff.com/blog/how-to-usecontext-with-usereducer/
const exploreContextValue = useMemo(() => {
return { exploreDispatch, exploreState };
}, [exploreDispatch, exploreState]);
// Reset explore response when token changes.
useEffect(() => {
exploreDispatch({
payload: undefined,
type: ExploreActionKind.ResetExploreResponse,
});
}, [exploreDispatch, token]);
return (
<ExploreStateContext.Provider value={exploreContextValue}>
{children}
</ExploreStateContext.Provider>
);
}
/**
* Explore action kind.
*/
export enum ExploreActionKind {
ApplySavedFilter = "APPLY_SAVED_FILTER",
ClearFilters = "CLEAR_FILTERS",
ClearMeta = "CLEAR_META",
PaginateTable = "PAGINATE_TABLE",
PatchExploreResponse = "PATCH_EXPLORE_RESPONSE",
ProcessExploreResponse = "PROCESS_EXPLORE_RESPONSE",
ResetExploreResponse = "RESET_EXPLORE_RESPONSE",
ResetState = "RESET_STATE",
SelectEntityType = "SELECT_ENTITY_TYPE",
StateToUrl = "STATE_TO_URL",
UpdateColumnVisibility = "UPDATE_COLUMN_VISIBILITY",
UpdateEntityFilters = "UPDATE_ENTITY_FILTERS",
UpdateEntityViewAccess = "UPDATE_ENTITY_VIEW_ACCESS",
UpdateFilter = "UPDATE_FILTER",
UpdateFilterSort = "UPDATE_FILTER_SORT",
UpdateGrouping = "UPDATE_GROUPING",
UpdateRowPreview = "UPDATE_ROW_PREVIEW",
UpdateRowSelection = "UPDATE_ROW_SELECTION",
UpdateSorting = "UPDATE_SORTING",
UrlToState = "URL_TO_STATE",
}
/**
* Explore action.
*/
export type ExploreAction =
| ApplySavedFilterAction
| ClearFiltersAction
| ClearMetaAction
| PaginateTableAction
| PatchExploreResponseAction
| ProcessExploreResponseAction
| ResetExploreResponseAction
| ResetStateAction
| SelectEntityTypeAction
| StateToUrlAction
| UpdateColumnVisibilityAction
| UpdateEntityFiltersAction
| UpdateEntityViewAccessAction
| UpdateFilterAction
| UpdateFilterSortAction
| UpdateGroupingAction
| UpdateRowPreviewAction
| UpdateRowSelectionAction
| UpdateSortingAction
| UrlToStateAction;
/**
* Apply saved filter action.
*/
type ApplySavedFilterAction = {
payload: ApplySavedFilterPayload;
type: ExploreActionKind.ApplySavedFilter;
};
/**
* Clear filters action.
*/
type ClearFiltersAction = {
payload: undefined;
type: ExploreActionKind.ClearFilters;
};
/**
* Paginate table action.
*/
type PaginateTableAction = {
payload: PaginateTablePayload;
type: ExploreActionKind.PaginateTable;
};
/**
* Patch explore response action.
*/
type PatchExploreResponseAction = {
payload: PatchExploreResponsePayload;
type: ExploreActionKind.PatchExploreResponse;
};
/**
* Process explore response action.
*/
type ProcessExploreResponseAction = {
payload: ProcessExploreResponsePayload;
type: ExploreActionKind.ProcessExploreResponse;
};
/**
* Reset explore response action.
*/
type ResetExploreResponseAction = {
payload: ResetExploreResponsePayload;
type: ExploreActionKind.ResetExploreResponse;
};
/**
* Reset state type action.
*/
type ResetStateAction = {
payload: string;
type: ExploreActionKind.ResetState;
};
/**
* Select entity type action.
*/
type SelectEntityTypeAction = {
payload: string;
type: ExploreActionKind.SelectEntityType;
};
/**
* Update entity filters action.
*/
type UpdateEntityFiltersAction = {
payload: UpdateEntityFiltersPayload;
type: ExploreActionKind.UpdateEntityFilters;
};
/**
* Update entity view access action.
*/
type UpdateEntityViewAccessAction = {
payload: UpdateEntityViewAccessPayload;
type: ExploreActionKind.UpdateEntityViewAccess;
};
/**
* Update filter action.
*/
type UpdateFilterAction = {
payload: UpdateFilterPayload;
type: ExploreActionKind.UpdateFilter;
};
/**
* Update row preview action.
*/
export type UpdateRowPreviewAction = {
payload: UpdateRowPreviewPayload;
type: ExploreActionKind.UpdateRowPreview;
};
/**
* Update row selection action.
*/
type UpdateRowSelectionAction = {
payload: UpdateRowSelectionPayload;
type: ExploreActionKind.UpdateRowSelection;
};
/**
* Update sorting action.
*/
export type UpdateSortingAction = {
payload: UpdateSortingPayload;
type: ExploreActionKind.UpdateSorting;
};
/**
* Explore reducer.
* @param state - Explore state.
* @param action - Reducer action.
* @param exploreContext - Explore context.
* @returns updated explore state.
*/
function exploreReducer(
state: ExploreState,
action: ExploreAction,
exploreContext: ExploreContext
): ExploreState {
const { payload, type } = action;
const { config, entityList } = exploreContext;
switch (type) {
/**
* Apply saved filter
**/
case ExploreActionKind.ApplySavedFilter: {
const filterState = buildNextSavedFilterState(
state,
payload.selectedValue,
payload.selected
);
const rowPreview = closeRowPreview(state.rowPreview);
const rowSelection: RowSelectionState = {};
const savedFilterState = buildEntityStateSavedFilterState(
payload.categoryKey,
payload.selectedValue,
payload.selected
);
const savedGrouping = getEntityStateSavedProperty(
state,
payload.selectedValue,
payload.selected,
"grouping"
);
const savedSorting = getEntityStateSavedProperty(
state,
payload.selectedValue,
payload.selected,
"sorting"
);
const grouping = savedGrouping || []; // Reset grouping to default if saved grouping is not available.
const sorting = savedSorting || []; // Reset sorting to default if saved sorting is not available.
updateEntityStateByCategoryGroupConfigKey(state, {
filterState,
savedFilterState,
});
return {
...state,
entities: buildNextEntities(state, state.tabValue, { filterState }),
entityPageState: updateEntityPageStateWithCommonCategoryGroupConfigKey(
state,
{ grouping, rowPreview, rowSelection, sorting }
),
filterCount: getFilterCount(filterState),
filterState,
meta: { command: META_COMMAND.STATE_TO_URL_PUSH },
paginationState: resetPage(state.paginationState),
rowPreview,
};
}
/**
* Clear all filters
**/
case ExploreActionKind.ClearFilters: {
const filterCount = 0;
const filterState: SelectedFilter[] = [];
const rowPreview = closeRowPreview(state.rowPreview);
const rowSelection: RowSelectionState = {};
const savedFilterState: SelectedFilter[] = [];
updateEntityStateByCategoryGroupConfigKey(state, {
filterState,
savedFilterState,
});
return {
...state,
entities: buildNextEntities(state, state.tabValue, { filterState }),
entityPageState: updateEntityPageStateWithCommonCategoryGroupConfigKey(
state,
{ rowPreview, rowSelection }
),
filterCount,
filterState,
meta: { command: META_COMMAND.STATE_TO_URL_PUSH },
paginationState: resetPage(state.paginationState),
rowPreview,
};
}
/**
* Clear meta
*/
case ExploreActionKind.ClearMeta: {
return clearMetaAction(state, payload);
}
/**
* Paginate table
**/
case ExploreActionKind.PaginateTable: {
const nextPaginationState = { ...state.paginationState };
if (payload == "next") {
nextPaginationState.currentPage++;
nextPaginationState.index = nextPaginationState.nextIndex;
} else {
nextPaginationState.currentPage--;
nextPaginationState.index = nextPaginationState.previousIndex;
}
const rowPreview = closeRowPreview(state.rowPreview);
return {
...state,
entityPageState: updateEntityPageState(
state.tabValue,
state.entityPageState,
{ rowPreview }
),
paginationState: nextPaginationState,
rowPreview,
};
}
/**
* Patch explore response
*/
case ExploreActionKind.PatchExploreResponse: {
return {
...state,
entityPageState: updateEntityPageState(
state.tabValue,
state.entityPageState,
{ rowSelection: {} }
),
listItems: patchEntityListItems(
state.listItems,
payload.updatedListItems,
payload.listItemKey
),
};
}
/**
* Process explore response
**/
case ExploreActionKind.ProcessExploreResponse: {
const entityPageState = state.entityPageState[state.tabValue];
const entityState = getEntityState(state);
const nextCategoryViews = payload.selectCategories
? buildCategoryViews(
[
...payload.selectCategories,
...entityState.savedSelectCategories, // "savedFilter" select categories are built from config at reducer initialization.
],
entityState.categoryConfigs,
[...state.filterState, ...entityState.savedFilterState],
state.filterSort
)
: state.categoryViews;
const rowPreview = entityPageState.rowPreview;
updateEntityStateByCategoryGroupConfigKey(state, {
categoryViews: nextCategoryViews,
});
return {
...state,
categoryViews: nextCategoryViews,
listItems: payload.loading ? [] : payload.listItems,
loading: payload.loading,
paginationState: {
...state.paginationState,
...payload.paginationResponse,
},
rowPreview,
};
}
/**
* Reset explore response.
**/
case ExploreActionKind.ResetExploreResponse: {
return {
...state,
listItems: [],
loading: true,
paginationState: DEFAULT_PAGINATION_STATE,
};
}
/**
* Reset the current state to the initial
*/
case ExploreActionKind.ResetState: {
return initReducerArguments(config, entityList, "");
}
/**
* Select entity type
**/
case ExploreActionKind.SelectEntityType: {
if (payload === state.tabValue) {
return state;
}
const entityState = getEntityState(
state,
getEntityCategoryGroupConfigKey(payload, state.entityPageState)
);
const rowPreview = closeRowPreview(state.rowPreview); // Close row preview, without updating the entity page state row preview.
return {
...state,
categoryGroups: entityState.categoryGroups,
categoryViews: entityState.categoryViews,
filterCount: getFilterCount(entityState.filterState),
filterState: entityState.filterState,
listItems: [],
loading: true,
paginationState: { ...resetPage(state.paginationState), rows: 0 },
rowPreview,
tabValue: payload,
};
}
/**
* State >> URL sync
*/
case ExploreActionKind.StateToUrl: {
return stateToUrlAction(state, payload);
}
/**
* Update column visibility
**/
case ExploreActionKind.UpdateColumnVisibility: {
return updateColumnVisibilityAction(state, payload);
}
/**
* Update entity filters.
* Updates state for the given entity and entities with the same category group config key, prior to navigation to the entity.
* Actions for the given entity and entities with the same category group config key:
* - updates filter state,
* - clears saved filter state,
* - resets row selection, and
* - closes row preview.
* Actions for the current entity:
* - closes row preview, without updating the entity page state row preview.
*/
case ExploreActionKind.UpdateEntityFilters: {
const {
entityListType = state.tabValue,
filters: filterState,
grouping = [],
sorting = [],
} = payload;
const categoryGroupConfigKey = getEntityCategoryGroupConfigKey(
entityListType,
state.entityPageState
);
const rowPreview = closeRowPreview(
state.entityPageState[entityListType].rowPreview
);
const rowSelection: RowSelectionState = {};
const savedFilterState: SelectedFilter[] = [];
updateEntityStateByCategoryGroupConfigKey(
state,
{
filterState,
savedFilterState,
},
categoryGroupConfigKey
);
return {
...state,
entities: buildNextEntities(state, entityListType, { filterState }),
entityPageState: updateEntityPageStateWithCommonCategoryGroupConfigKey(
state,
{ grouping, rowPreview, rowSelection, sorting },
categoryGroupConfigKey
),
rowPreview: closeRowPreview(state.rowPreview),
...(entityListType === state.tabValue
? {
filterCount: getFilterCount(filterState),
filterState,
paginationState: { ...resetPage(state.paginationState), rows: 0 },
}
: {}),
};
}
/**
* Update entity view access
**/
case ExploreActionKind.UpdateEntityViewAccess: {
return {
...state,
entityPageState: updateSelectColumnVisibility(state, payload.canEdit),
};
}
/**
* Update filter
**/
case ExploreActionKind.UpdateFilter: {
const filterState = buildNextFilterState(
state.filterState,
payload.categoryKey,
payload.selectedValue,
payload.selected,
payload.viewKind
);
const rowPreview = closeRowPreview(state.rowPreview);
const rowSelection: RowSelectionState = {};
const savedFilterState: SelectedFilter[] = []; // Clear saved filter state.
updateEntityStateByCategoryGroupConfigKey(state, {
filterState,
savedFilterState,
});
return {
...state,
entities: buildNextEntities(state, state.tabValue, { filterState }),
entityPageState: updateEntityPageStateWithCommonCategoryGroupConfigKey(
state,
{ rowPreview, rowSelection }
),
filterCount: getFilterCount(filterState),
filterState,
meta: { command: META_COMMAND.STATE_TO_URL_PUSH },
paginationState: resetPage(state.paginationState),
rowPreview,
};
}
/**
* Update filter sort
*/
case ExploreActionKind.UpdateFilterSort: {
return updateFilterSortAction(state, payload);
}
/**
* Update grouping
**/
case ExploreActionKind.UpdateGrouping: {
return updateGroupingAction(state, payload);
}
/**
* Update row preview
*/
case ExploreActionKind.UpdateRowPreview: {
const rowPreview = payload;
return {
...state,
entityPageState: updateEntityPageState(
state.tabValue,
state.entityPageState,
{ rowPreview }
),
rowPreview,
};
}
/**
* Update row selection
*/
case ExploreActionKind.UpdateRowSelection: {
const rowSelection = payload;
return {
...state,
entityPageState: updateEntityPageState(
state.tabValue,
state.entityPageState,
{ rowSelection }
),
};
}
/**
* Update sorting
**/
case ExploreActionKind.UpdateSorting: {
const rowPreview = closeRowPreview(state.rowPreview);
const sorting = payload;
return {
...state,
entityPageState: updateEntityPageState(
state.tabValue,
state.entityPageState,
{ rowPreview, sorting }
),
paginationState: resetPage(state.paginationState),
rowPreview,
};
}
/**
* URL >> state sync.
*/
case ExploreActionKind.UrlToState: {
return urlToStateAction(state, payload);
}
default:
return state;
}
}