forked from keplergl/kepler.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui-state-updaters.ts
More file actions
933 lines (878 loc) · 22.9 KB
/
Copy pathui-state-updaters.ts
File metadata and controls
933 lines (878 loc) · 22.9 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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import {
ADD_DATA_ID,
DATA_TABLE_ID,
DEFAULT_NOTIFICATION_TOPICS,
DELETE_DATA_ID,
EXPORT_DATA_TYPE,
EXPORT_HTML_MAP_MODES,
EXPORT_IMG_RATIOS,
EXPORT_MAP_FORMATS,
RESOLUTIONS,
MAP_CONTROLS
} from '@kepler.gl/constants';
import {LOCALE_CODES} from '@kepler.gl/localization';
import {
createNotification,
errorNotification,
calculateExportImageSize,
getApplicationConfig
} from '@kepler.gl/utils';
import {payload_, apply_, compose_} from './composer-helpers';
import {
ActionTypes,
KeplerGlInitPayload,
LoadFilesErrUpdaterAction,
UIStateActions
} from '@kepler.gl/actions';
import {
ExportData,
ExportHtml,
ExportJson,
ExportMap,
ExportImage,
MapControlItem,
MapControls,
UiState
} from '@kepler.gl/types';
export const DEFAULT_ACTIVE_SIDE_PANEL = 'layer';
export const DEFAULT_MODAL = ADD_DATA_ID;
/**
* Updaters for `uiState` reducer. Can be used in your root reducer to directly modify kepler.gl's state.
* Read more about [Using updaters](../advanced-usage/using-updaters.md)
*
* @public
* @example
*
* import keplerGlReducer, {uiStateUpdaters} from '@kepler.gl/reducers';
* // Root Reducer
* const reducers = combineReducers({
* keplerGl: keplerGlReducer,
* app: appReducer
* });
*
* const composedReducer = (state, action) => {
* switch (action.type) {
* // click button to close side panel
* case 'CLICK_BUTTON':
* return {
* ...state,
* keplerGl: {
* ...state.keplerGl,
* foo: {
* ...state.keplerGl.foo,
* uiState: uiStateUpdaters.toggleSidePanelUpdater(
* uiState, {payload: null}
* )
* }
* }
* };
* }
* return reducers(state, action);
* };
*
* export default composedReducer;
*/
/* eslint-disable @typescript-eslint/no-unused-vars */
// @ts-ignore
const uiStateUpdaters = null;
/* eslint-enable @typescript-eslint/no-unused-vars */
const DEFAULT_MAP_CONTROLS_FEATURES: MapControlItem = {
show: true,
active: false,
disableClose: false,
// defines which map index users are interacting with (through map controls)
activeMapIndex: 0
};
const DEFAULT_MAP_LEGEND_CONTROL = {
...DEFAULT_MAP_CONTROLS_FEATURES,
disableEdit: false
};
/**
* A list of map control visibility and whether is it active.
* @memberof uiStateUpdaters
* @constant
* @property visibleLayers Default: `{show: true, active: false}`
* @property mapLegend Default: `{show: true, active: false}`
* @property toggle3d Default: `{show: true}`
* @property splitMap Default: `{show: true}`
* @property mapDraw Default: `{show: true, active: false}`
* @property mapLocale Default: `{show: false, active: false}`
* @public
*/
export const DEFAULT_MAP_CONTROLS: MapControls = (
Object.keys(MAP_CONTROLS) as Array<keyof typeof MAP_CONTROLS>
).reduce(
(final, current) => ({
...final,
[current]:
current === MAP_CONTROLS.mapLegend
? DEFAULT_MAP_LEGEND_CONTROL
: DEFAULT_MAP_CONTROLS_FEATURES
}),
{} as MapControls
);
/**
* Default image export config
* @memberof uiStateUpdaters
* @constant
* @property legend Default: `false`,
* @property mapH Default: 0,
* @property mapW Default: 0,
* @property imageSize Default: {zoomOffset: 0, scale: 1, imageW: 0, imageH: 0},
* @property imageDataUri Default: `''`,
* @property exporting Default: `false`
* @property error Default: `false`
* @property escapeXhtmlForWebpack Default: from application config (auto-detected: `true` for webpack)
* @public
*/
export const DEFAULT_EXPORT_IMAGE: ExportImage = {
// user options
legend: false,
mapH: 0,
mapW: 0,
imageSize: {
zoomOffset: 0,
scale: 1,
imageW: 0,
imageH: 0
},
// when this is set to true, the mock map viewport will move to the center of data
center: false,
// exporting state
imageDataUri: '',
// exporting: used to attach plot-container to dom
exporting: false,
// processing: used as loading indicator when export image is being produced
processing: false,
error: false,
// whether to apply fix for uglify error in dom-to-image (from application config, auto-detects build tool)
escapeXhtmlForWebpack: getApplicationConfig().escapeXhtmlForWebpack
};
export const DEFAULT_LOAD_FILES = {
fileLoading: false
};
/**
* Default initial `exportData` settings
* @memberof uiStateUpdaters
* @constant
* @property selectedDataset Default: `''`,
* @property dataType Default: `'csv'`,
* @property filtered Default: `true`,
* @public
*/
export const DEFAULT_EXPORT_DATA: ExportData = {
selectedDataset: '',
dataType: EXPORT_DATA_TYPE.CSV,
filtered: true
};
/**
* @constant
*/
export const DEFAULT_NOTIFICATIONS = [];
/**
* @constant
* @property exportMapboxAccessToken - Default: null, this is used when we provide a default mapbox token for users to take advantage of
* @property userMapboxToken - Default: '', mapbox token provided by user through input field
* @property mode - Default: 'READ', read only or editable
* @public
*/
export const DEFAULT_EXPORT_HTML: ExportHtml = {
exportMapboxAccessToken: null,
userMapboxToken: '',
mode: EXPORT_HTML_MAP_MODES.READ
};
/**
* @constant
* @property hasData - Default: 'true',
* @public
*/
export const DEFAULT_EXPORT_JSON: ExportJson = {
hasData: true
};
/**
* Export Map Config
* @constant
* @property HTML - Default: 'DEFAULT_EXPORT_HTML',
* @property JSON - Default: 'DEFAULT_EXPORT_JSON',
* @property format - Default: 'HTML',
* @public
*/
export const DEFAULT_EXPORT_MAP: ExportMap = {
[EXPORT_MAP_FORMATS.HTML]: DEFAULT_EXPORT_HTML,
[EXPORT_MAP_FORMATS.JSON]: DEFAULT_EXPORT_JSON,
format: EXPORT_MAP_FORMATS.HTML
};
/**
* Default initial `uiState`
* @memberof uiStateUpdaters
* @constant
* @property readOnly Default: `false`
* @property activeSidePanel Default: `'layer'`
* @property currentModal Default: `'addData'`
* @property datasetKeyToRemove Default: `null`
* @property visibleDropdown Default: `null`
* @property exportImage Default: [`DEFAULT_EXPORT_IMAGE`](#default_export_image)
* @property exportData Default: [`DEFAULT_EXPORT_DATA`](#default_export_data)
* @property exportMap Default: [`DEFAULT_EXPORT_MAP`](#default_export_map)
* @property mapControls Default: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @property notifications Default: `[]`
* @property notifications Default: `[]`
* @property loadFiles
* @property isSidePanelCloseButtonVisible Default: `true`
* @public
*/
export const INITIAL_UI_STATE: UiState = {
readOnly: false,
activeSidePanel: DEFAULT_ACTIVE_SIDE_PANEL,
currentModal: DEFAULT_MODAL,
datasetKeyToRemove: null,
visibleDropdown: null,
// export image modal ui
exportImage: DEFAULT_EXPORT_IMAGE,
// export data modal ui
exportData: DEFAULT_EXPORT_DATA,
// html export
exportMap: DEFAULT_EXPORT_MAP,
// map control panels
mapControls: DEFAULT_MAP_CONTROLS,
// ui notifications
notifications: DEFAULT_NOTIFICATIONS,
// load files
loadFiles: DEFAULT_LOAD_FILES,
// Locale of the UI
locale: LOCALE_CODES.en,
layerPanelListView: 'list',
filterPanelListView: 'list',
isSidePanelCloseButtonVisible: true
};
/* Updaters */
/**
* @memberof uiStateUpdaters
*/
export const initUiStateUpdater = (
state: UiState,
action: {
type?: (typeof ActionTypes)['INIT'];
payload: KeplerGlInitPayload;
}
): UiState => ({
...state,
...(action.payload || {}).initialUiState
});
/**
* Toggle active side panel
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload id of side panel to be shown, one of `layer`, `filter`, `interaction`, `map`. close side panel if `null`
* @returns nextState
* @public
*/
export const toggleSidePanelUpdater = (
state: UiState,
{payload: id}: UIStateActions.ToggleSidePanelUpdaterAction
): UiState => {
return id === state.activeSidePanel
? state
: {
...state,
activeSidePanel: id
};
};
/**
* Show and hide modal dialog
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @paramaction.payload id of modal to be shown, null to hide modals. One of:
* - [`DATA_TABLE_ID`](../constants/default-settings.md#data_table_id)
* - [`DELETE_DATA_ID`](../constants/default-settings.md#delete_data_id)
* - [`ADD_DATA_ID`](../constants/default-settings.md#add_data_id)
* - [`EXPORT_IMAGE_ID`](../constants/default-settings.md#export_image_id)
* - [`EXPORT_DATA_ID`](../constants/default-settings.md#export_data_id)
* - [`ADD_MAP_STYLE_ID`](../constants/default-settings.md#add_map_style_id)
* @returns nextState
* @public
*/
export const toggleModalUpdater = (
state: UiState,
{payload: id}: UIStateActions.ToggleModalUpdaterAction
): UiState => ({
...state,
currentModal: id
});
/**
* Hide and show side panel header dropdown, activated by clicking the share link on top of the side panel
* @memberof uiStateUpdaters
* @public
*/
export const showExportDropdownUpdater = (
state: UiState,
{payload: id}: UIStateActions.ShowExportDropdownUpdaterAction
): UiState => ({
...state,
visibleDropdown: id
});
/**
* Hide side panel header dropdown, activated by clicking the share link on top of the side panel
* @memberof uiStateUpdaters
* @public
*/
export const hideExportDropdownUpdater = (state: UiState): UiState => ({
...state,
visibleDropdown: null
});
/**
* Toggle side panel close button on top left of the side panel
* @memberof uiStateUpdaters
* @public
*/
export const toggleSidePanelCloseButtonUpdater = (
state: UiState,
{payload: {show}}: UIStateActions.ToggleSidePanelCloseButtonUpdaterAction
): UiState => ({
...state,
isSidePanelCloseButtonVisible: show
});
/**
* Toggle active map control panel
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action action
* @param action.payload map control panel id, one of the keys of: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @returns nextState
* @public
*/
export const toggleMapControlUpdater = (
state: UiState,
{payload: {panelId, index = 0}}: UIStateActions.ToggleMapControlUpdaterAction
): UiState => {
let updatedState = state;
// The effect panel and ai assistant panel can not be active at the same time
// so we need to deactivate the other panel when one is activated
const panelToDeactivate =
panelId === MAP_CONTROLS.effect
? MAP_CONTROLS.aiAssistant
: panelId === MAP_CONTROLS.aiAssistant
? MAP_CONTROLS.effect
: null;
// To to toggle the mapDraw and mapLocal dropdowns
// We have to deactivate the other active dropdown
const dropdownToDeactivate =
panelId === MAP_CONTROLS.mapDraw
? MAP_CONTROLS.mapLocale
: panelId === MAP_CONTROLS.mapLocale
? MAP_CONTROLS.mapDraw
: null;
// If we need to deactivate a competing panel and it's currently active
if (panelToDeactivate && state.mapControls[panelToDeactivate]?.active) {
updatedState = {
...state,
mapControls: {
...updatedState.mapControls,
[panelToDeactivate]: {
...updatedState.mapControls[panelToDeactivate],
active: false
}
}
};
}
// If we need to deactivate a competing dropdown and it's currently active
if (dropdownToDeactivate && state.mapControls[dropdownToDeactivate]?.active) {
updatedState = {
...state,
mapControls: {
...updatedState.mapControls,
[dropdownToDeactivate]: {
...updatedState.mapControls[dropdownToDeactivate],
active: false
}
}
};
}
return {
...updatedState,
mapControls: {
...updatedState.mapControls,
[panelId]: {
...updatedState.mapControls[panelId],
active: !updatedState.mapControls[panelId].active,
activeMapIndex: index
}
}
};
};
/**
* Toggle map control visibility
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action action
* @param action.payload map control panel id, one of the keys of: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @returns nextState
* @public
*/
export const setMapControlVisibilityUpdater = (
state: UiState,
{payload: {panelId, show}}: UIStateActions.setMapControlVisibilityUpdaterAction
): UiState => {
if (!state.mapControls?.[panelId]) {
return state;
}
return {
...state,
mapControls: {
...state.mapControls,
[panelId]: {
...state.mapControls[panelId],
show: Boolean(show)
}
}
};
};
/**
* Toggle map control settings
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action action
* @param action.payload map control panel id, one of the keys of: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @returns nextState
* @public
*/
export const setMapControlSettingsUpdater = (
state: UiState,
{payload: {panelId, settings}}: UIStateActions.setMapControlSettingsUpdaterAction
): UiState => {
const mapControl = state.mapControls?.[panelId];
if (!mapControl) {
return state;
}
return {
...state,
mapControls: {
...state.mapControls,
[panelId]: {...mapControl, settings: {...mapControl.settings, ...settings}}
}
};
};
/**
* Toggle active map control panel
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload dataset id
* @returns nextState
* @public
*/
export const openDeleteModalUpdater = (
state: UiState,
{payload: datasetKeyToRemove}: UIStateActions.OpenDeleteModalUpdaterAction
): UiState => ({
...state,
currentModal: DELETE_DATA_ID,
datasetKeyToRemove
});
/**
* Set `exportImage.legend` to `true` or `false`
* @memberof uiStateUpdaters
* @param state `uiState`
* @returns nextState
* @public
*/
export const setExportImageSettingUpdater = (
state: UiState,
{payload: newSetting}: UIStateActions.SetExportImageSettingUpdaterAction
): UiState => {
const updated = {...state.exportImage, ...newSetting};
const imageSize = calculateExportImageSize(updated) || state.exportImage.imageSize;
return {
...state,
exportImage: {
...updated,
// @ts-expect-error
// TODO: calculateExportImageSize does not return imageSize.zoomOffset,
// do we need take this value from current state, or return defaul value = 0
imageSize
}
};
};
/**
* Set `exportImage.setExportImageDataUri` to a image dataUri
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload export image data uri
* @returns nextState
* @public
*/
export const setExportImageDataUriUpdater = (
state: UiState,
{payload: dataUri}: UIStateActions.SetExportImageDataUriUpdaterAction
): UiState => {
if (dataUri === state.exportImage.imageDataUri) {
return state;
}
return {
...state,
exportImage: {
...state.exportImage,
processing: false,
imageDataUri: dataUri
}
};
};
/**
* @memberof uiStateUpdaters
* @public
*/
export const setExportImageErrorUpdater = (
state: UiState,
{payload: error}: UIStateActions.SetExportImageErrorUpdaterAction
): UiState => ({
...state,
exportImage: {
...state.exportImage,
processing: false,
error
}
});
/**
* Delete cached export image
* @memberof uiStateUpdaters
* @public
*/
export const cleanupExportImageUpdater = (state: UiState): UiState => ({
...state,
exportImage: {
...state.exportImage,
exporting: false,
imageDataUri: '',
error: false,
processing: false,
center: false
}
});
/**
* Start image exporting flow
* @memberof uiStateUpdaters
* @param state
* @param options
* @returns {UiState}
* @public
*/
export const startExportingImageUpdater = (
state: UiState,
{payload: options = {}}: {payload: Partial<ExportImage>}
): UiState => {
const imageSettings = {
...options,
exporting: true
};
return compose_([
cleanupExportImageUpdater,
apply_(setExportImageSettingUpdater, payload_(imageSettings))
])(state);
};
/**
* Set selected dataset for export
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload dataset id
* @returns nextState
* @public
*/
export const setExportSelectedDatasetUpdater = (
state: UiState,
{payload: dataset}: UIStateActions.SetExportSelectedDatasetUpdaterAction
): UiState => ({
...state,
exportData: {
...state.exportData,
selectedDataset: dataset
}
});
/**
* Set data format for exporting data
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload one of `'text/csv'`
* @returns nextState
* @public
*/
export const setExportDataTypeUpdater = (
state: UiState,
{payload: dataType}: UIStateActions.SetExportDataTypeUpdaterAction
): UiState => ({
...state,
exportData: {
...state.exportData,
dataType
}
});
/**
* Whether to export filtered data, `true` or `false`
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload
* @returns nextState
* @public
*/
export const setExportFilteredUpdater = (
state: UiState,
{payload: filtered}: UIStateActions.SetExportFilteredUpdaterAction
): UiState => ({
...state,
exportData: {
...state.exportData,
filtered
}
});
/**
* Whether to including data in map config, toggle between `true` or `false`
* @memberof uiStateUpdaters
* @param state `uiState`
* @returns nextState
* @public
*/
export const setExportDataUpdater = (state: UiState): UiState => ({
...state,
exportMap: {
...state.exportMap,
[EXPORT_MAP_FORMATS.JSON]: {
...state.exportMap[EXPORT_MAP_FORMATS.JSON],
hasData: !state.exportMap[EXPORT_MAP_FORMATS.JSON].hasData
}
}
});
/**
* whether to export a mapbox access to HTML single page
* @param state - `uiState`
* @param action
* @param action.payload
* @returns nextState
* @public
*/
export const setUserMapboxAccessTokenUpdater = (
state: UiState,
{payload: userMapboxToken}: UIStateActions.SetUserMapboxAccessTokenUpdaterAction
): UiState => ({
...state,
exportMap: {
...state.exportMap,
[EXPORT_MAP_FORMATS.HTML]: {
...state.exportMap[EXPORT_MAP_FORMATS.HTML],
userMapboxToken
}
}
});
/**
* Sets the export map format
* @param state - `uiState`
* @param action
* @param action.payload format to use to export the map into
* @return nextState
*/
export const setExportMapFormatUpdater = (
state: UiState,
{payload: format}: UIStateActions.SetExportMapFormatUpdaterAction
): UiState => ({
...state,
exportMap: {
...state.exportMap,
// @ts-expect-error
format
}
});
/**
* Set the export html map mode
* @param state - `uiState`
* @param action
* @param action.payload to be set (available modes: EXPORT_HTML_MAP_MODES)
* @return nextState
*/
export const setExportMapHTMLModeUpdater = (
state: UiState,
{payload: mode}: UIStateActions.SetExportHTMLMapModeUpdaterAction
): UiState => ({
...state,
exportMap: {
...state.exportMap,
[EXPORT_MAP_FORMATS.HTML]: {
...state.exportMap[EXPORT_MAP_FORMATS.HTML],
mode
}
}
});
/**
* Adds a new notification.
* Updates a notification in case of matching ids.
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload Params of a notification
* @returns nextState
* @public
*/
export const addNotificationUpdater = (
state: UiState,
{payload}: UIStateActions.AddNotificationUpdaterAction
): UiState => {
const oldNotifications = state.notifications || [];
// @ts-expect-error
const payloadId = payload?.id;
const notificationToUpdate = payloadId ? oldNotifications.find(n => n.id === payloadId) : null;
let notifications;
if (notificationToUpdate) {
notifications = oldNotifications.map(n =>
n.id === payloadId ? createNotification({...payload, count: n.count + 1}) : n
);
} else {
notifications = [...oldNotifications, createNotification(payload)];
}
return {...state, notifications};
};
/**
* Remove a notification
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload id of the notification to be removed
* @returns nextState
* @public
*/
export const removeNotificationUpdater = (
state: UiState,
{payload: id}: UIStateActions.RemoveNotificationUpdaterAction
): UiState => ({
...state,
notifications: state.notifications.filter(n => n.id !== id)
});
/**
* Fired when file loading begin
* @memberof uiStateUpdaters
* @param state `uiState`
* @returns nextState
* @public
*/
export const loadFilesUpdater = (state: UiState): UiState => ({
...state,
loadFiles: {
...state.loadFiles,
fileLoading: true
}
});
/**
* Handles loading file success and set fileLoading property to false
* @memberof uiStateUpdaters
* @param state `uiState`
* @returns nextState
*/
export const loadFilesSuccessUpdater = (state: UiState): UiState => ({
...state,
loadFiles: {
...state.loadFiles,
fileLoading: false
}
});
/**
* Handles load file error and set fileLoading property to false
* @memberof uiStateUpdaters
* @param state
* @param action
* @param action.error
* @returns nextState
* @public
*/
export const loadFilesErrUpdater = (state: UiState, {error}: LoadFilesErrUpdaterAction): UiState =>
addNotificationUpdater(
{
...state,
loadFiles: {
...state.loadFiles,
fileLoading: false
}
},
{
payload: errorNotification({
message: (error || {}).message || 'Failed to upload files',
topic: DEFAULT_NOTIFICATION_TOPICS.global
})
}
);
/**
* Handles toggle map split and reset all map control index to 0
* @memberof uiStateUpdaters
* @param state
* @returns nextState
* @public
*/
export const toggleSplitMapUpdater = (state: UiState): UiState => ({
...state,
mapControls: Object.entries(state.mapControls).reduce(
(acc, entry) => ({
...acc,
[entry[0]]: {
...entry[1],
activeMapIndex: 0
}
}),
{} as MapControls
)
});
/**
* Toggle modal data
* @memberof uiStateUpdaters
* @param state
* @returns nextState
* @public
*/
export const showDatasetTableUpdater = (state: UiState): UiState =>
toggleModalUpdater(state, {payload: DATA_TABLE_ID});
/**
* Set the locale of the UI
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload
* @param action.payload.locale locale
* @returns nextState
* @public
*/
export const setLocaleUpdater = (
state: UiState,
{payload: {locale}}: UIStateActions.SetLocaleUpdaterAction
): UiState => ({
...state,
locale
});
/**
* Toggle layer panel list view
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action
* @param action.payload layer panel listView value. Can be 'list' or 'sortByDataset'
* @returns nextState
* @public
*/
export const togglePanelListViewUpdater = (
state: UiState,
{payload: {panelId, listView}}: UIStateActions.TogglePanelListViewAction
): UiState => {
const stateProp =
panelId === 'layer'
? 'layerPanelListView'
: panelId === 'filter'
? 'filterPanelListView'
: null;
if (!stateProp || state[stateProp] === listView) {
return state;
}
return {
...state,
[stateProp]: listView
};
};