@applicaster/zapp-react-native-utils 16.0.0-alpha.1042942411 → 16.0.0-alpha.1052711356
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actionsExecutor/ActionExecutorContext.tsx +4 -0
- package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +22 -0
- package/actionsExecutor/actions/__tests__/loadItemsFromSource.test.ts +88 -0
- package/actionsExecutor/actions/__tests__/openBottomSheet.customContent.test.ts +76 -0
- package/actionsExecutor/actions/__tests__/showToast.test.ts +63 -0
- package/actionsExecutor/actions/dismissBottomSheet.ts +22 -0
- package/actionsExecutor/actions/index.ts +4 -0
- package/actionsExecutor/actions/openBottomSheet.ts +318 -0
- package/actionsExecutor/actions/refreshComponent.ts +6 -0
- package/actionsExecutor/actions/sendCloudEvent.ts +8 -0
- package/actionsExecutor/actions/showToast.ts +7 -7
- package/arrayUtils/__tests__/{anyThruthy.test.ts → anyTruthy.test.ts} +8 -0
- package/arrayUtils/__tests__/arrayUtils.test.ts +165 -108
- package/arrayUtils/__tests__/isEmptyArray.test.ts +11 -0
- package/arrayUtils/__tests__/isFilledArray.test.ts +12 -0
- package/arrayUtils/__tests__/isFirst.test.ts +17 -0
- package/arrayUtils/__tests__/isIndexInRange.test.ts +14 -0
- package/arrayUtils/__tests__/isLast.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOf.test.ts +31 -0
- package/arrayUtils/__tests__/makeListOfIndexes.test.ts +20 -0
- package/arrayUtils/__tests__/sample.test.ts +61 -0
- package/arrayUtils/index.ts +57 -20
- package/modalState/ContentViewModel.ts +111 -0
- package/modalState/EditableCollection.ts +17 -0
- package/modalState/EditableCollectionRegistry.ts +51 -0
- package/modalState/ModalOrchestrator.ts +198 -0
- package/modalState/RemoteEditableCollection.ts +189 -0
- package/modalState/__tests__/ContentViewModel.editable.test.ts +69 -0
- package/modalState/__tests__/ContentViewModel.test.ts +165 -0
- package/modalState/__tests__/EditableCollectionRegistry.test.ts +112 -0
- package/modalState/__tests__/ModalOrchestrator.phase3.test.ts +28 -0
- package/modalState/__tests__/RemoteEditableCollection.test.ts +259 -0
- package/modalState/__tests__/useBottomSheetContent.test.ts +372 -0
- package/modalState/index.ts +30 -83
- package/modalState/store.ts +102 -0
- package/modalState/types.ts +110 -0
- package/modalState/useBottomSheetContent.ts +98 -0
- package/package.json +2 -2
- package/zappFrameworkUtils/__tests__/localStorageHelper.test.ts +146 -0
- package/zappFrameworkUtils/localStorageHelper.ts +9 -13
- /package/reactHooks/actions/__tests__/{index.test.js → index.test.tsx} +0 -0
package/modalState/index.ts
CHANGED
|
@@ -1,83 +1,30 @@
|
|
|
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
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
set((state) => ({
|
|
32
|
-
modalState: { ...state.modalState, ...newState },
|
|
33
|
-
})),
|
|
34
|
-
openModal: ({ item, options = {}, props }: OpenModalArg) =>
|
|
35
|
-
set({
|
|
36
|
-
modalState: {
|
|
37
|
-
visible: true,
|
|
38
|
-
screen: item,
|
|
39
|
-
options: {
|
|
40
|
-
animated: true,
|
|
41
|
-
animationType: "slide",
|
|
42
|
-
onShow: () => {},
|
|
43
|
-
presentationStyle: "overFullScreen",
|
|
44
|
-
transparent: true,
|
|
45
|
-
...options,
|
|
46
|
-
onDismiss: () => set({ modalState: initialModalState }),
|
|
47
|
-
onRequestClose: () => set({ modalState: initialModalState }),
|
|
48
|
-
},
|
|
49
|
-
props,
|
|
50
|
-
},
|
|
51
|
-
}),
|
|
52
|
-
dismissModal: () => set(() => ({ modalState: initialModalState })),
|
|
53
|
-
}));
|
|
54
|
-
|
|
55
|
-
export const useModalStore = <T>(selector: (state: ModalStore) => T) =>
|
|
56
|
-
useStore(modalStore, selector);
|
|
57
|
-
|
|
58
|
-
export const openModal = modalStore.getInitialState().openModal;
|
|
59
|
-
|
|
60
|
-
export const dismissModal = modalStore.getInitialState().dismissModal;
|
|
61
|
-
|
|
62
|
-
export const openBottomSheetModal = ({
|
|
63
|
-
ModalBottomSheetContent,
|
|
64
|
-
modalBottomSheetContentProps,
|
|
65
|
-
props = {},
|
|
66
|
-
options = {},
|
|
67
|
-
}: OpenModalBottomSheetArgs) => {
|
|
68
|
-
openModal({
|
|
69
|
-
item: {
|
|
70
|
-
ModalBottomSheetContent,
|
|
71
|
-
modalBottomSheetContentProps,
|
|
72
|
-
},
|
|
73
|
-
props,
|
|
74
|
-
options: {
|
|
75
|
-
animationType: "none",
|
|
76
|
-
animated: false,
|
|
77
|
-
...options,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const useModalStoreState = (): ModalState =>
|
|
83
|
-
useModalStore<ModalState>((state) => state.modalState);
|
|
1
|
+
export {
|
|
2
|
+
modalStore,
|
|
3
|
+
useModalStore,
|
|
4
|
+
openModal,
|
|
5
|
+
dismissModal,
|
|
6
|
+
openBottomSheetModal,
|
|
7
|
+
useModalStoreState,
|
|
8
|
+
} from "./store";
|
|
9
|
+
|
|
10
|
+
export type { ModalState, ModalStore } from "./store";
|
|
11
|
+
|
|
12
|
+
export { modalOrchestrator } from "./ModalOrchestrator";
|
|
13
|
+
|
|
14
|
+
export { ContentViewModel } from "./ContentViewModel";
|
|
15
|
+
|
|
16
|
+
export type {
|
|
17
|
+
Menu,
|
|
18
|
+
MenuItem,
|
|
19
|
+
Header,
|
|
20
|
+
Content,
|
|
21
|
+
Action,
|
|
22
|
+
PresentSubMenuAction,
|
|
23
|
+
SecondaryAction,
|
|
24
|
+
SelectMode,
|
|
25
|
+
SelectionBehavior,
|
|
26
|
+
} from "./types";
|
|
27
|
+
|
|
28
|
+
export { isPresentSubMenuAction } from "./types";
|
|
29
|
+
|
|
30
|
+
export { useBottomSheetContent } from "./useBottomSheetContent";
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createStore, useStore } from "zustand";
|
|
2
|
+
|
|
3
|
+
export interface ModalState {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
screen: any;
|
|
6
|
+
options: Record<string, unknown>;
|
|
7
|
+
props: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ModalStore {
|
|
11
|
+
modalState: ModalState;
|
|
12
|
+
setModalState: (newState: Partial<ModalState>) => void;
|
|
13
|
+
openModal: (
|
|
14
|
+
item: any,
|
|
15
|
+
options?: Record<string, unknown>,
|
|
16
|
+
props?: Record<string, unknown>
|
|
17
|
+
) => void;
|
|
18
|
+
dismissModal: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const initialModalState: ModalState = {
|
|
22
|
+
visible: false,
|
|
23
|
+
screen: null,
|
|
24
|
+
options: {},
|
|
25
|
+
props: {},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const modalStore = createStore<ModalStore>((set, get) => ({
|
|
29
|
+
modalState: initialModalState,
|
|
30
|
+
setModalState: (newState) =>
|
|
31
|
+
set((state) => ({
|
|
32
|
+
modalState: { ...state.modalState, ...newState },
|
|
33
|
+
})),
|
|
34
|
+
openModal: ({ item, options = {}, props }: OpenModalArg) => {
|
|
35
|
+
const resetStore = () => set({ modalState: initialModalState });
|
|
36
|
+
const { onDismiss, onRequestClose, ...restOptions } = options as any;
|
|
37
|
+
|
|
38
|
+
set({
|
|
39
|
+
modalState: {
|
|
40
|
+
visible: true,
|
|
41
|
+
screen: item,
|
|
42
|
+
options: {
|
|
43
|
+
animated: true,
|
|
44
|
+
animationType: "slide",
|
|
45
|
+
onShow: () => {},
|
|
46
|
+
presentationStyle: "overFullScreen",
|
|
47
|
+
transparent: true,
|
|
48
|
+
...restOptions,
|
|
49
|
+
// Compose caller-provided callbacks with the store reset so both run
|
|
50
|
+
onDismiss: () => {
|
|
51
|
+
resetStore();
|
|
52
|
+
onDismiss?.();
|
|
53
|
+
},
|
|
54
|
+
onRequestClose: () => {
|
|
55
|
+
resetStore();
|
|
56
|
+
onRequestClose?.();
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
props,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
dismissModal: () => {
|
|
64
|
+
const onDismiss = (get().modalState?.options as any)?.onDismiss;
|
|
65
|
+
|
|
66
|
+
if (typeof onDismiss === "function") {
|
|
67
|
+
onDismiss();
|
|
68
|
+
} else {
|
|
69
|
+
set({ modalState: initialModalState });
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
export const useModalStore = <T>(selector: (state: ModalStore) => T) =>
|
|
75
|
+
useStore(modalStore, selector);
|
|
76
|
+
|
|
77
|
+
export const openModal = modalStore.getInitialState().openModal;
|
|
78
|
+
|
|
79
|
+
export const dismissModal = modalStore.getInitialState().dismissModal;
|
|
80
|
+
|
|
81
|
+
export const openBottomSheetModal = ({
|
|
82
|
+
ModalBottomSheetContent,
|
|
83
|
+
modalBottomSheetContentProps,
|
|
84
|
+
props = {},
|
|
85
|
+
options = {},
|
|
86
|
+
}: OpenModalBottomSheetArgs) => {
|
|
87
|
+
openModal({
|
|
88
|
+
item: {
|
|
89
|
+
ModalBottomSheetContent,
|
|
90
|
+
modalBottomSheetContentProps,
|
|
91
|
+
},
|
|
92
|
+
props,
|
|
93
|
+
options: {
|
|
94
|
+
animationType: "none",
|
|
95
|
+
animated: false,
|
|
96
|
+
...options,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const useModalStoreState = (): ModalState =>
|
|
102
|
+
useModalStore<ModalState>((state) => state.modalState);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export interface Header {
|
|
2
|
+
title: string;
|
|
3
|
+
subtitle?: string;
|
|
4
|
+
icon?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface Action {
|
|
8
|
+
type: string;
|
|
9
|
+
payload?: any;
|
|
10
|
+
options?: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Represented as 3 dots icon on top menu (opens new second level menu on desktop
|
|
14
|
+
// or a new bottom sheet on mobile)
|
|
15
|
+
export interface PresentSubMenuAction extends Action {
|
|
16
|
+
menu: Menu;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SecondaryAction {
|
|
20
|
+
action: Action | PresentSubMenuAction;
|
|
21
|
+
icon?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A group of actions attached to an entry, keyed by a button alias
|
|
26
|
+
* (e.g. "remove_item", "reorder_item"). Injected by the feed decorator and
|
|
27
|
+
* dispatched by the remote EditableCollection when an operation runs.
|
|
28
|
+
*/
|
|
29
|
+
export interface EntryActionGroup {
|
|
30
|
+
button?: { alias?: string };
|
|
31
|
+
actions?: Action[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MenuItem {
|
|
35
|
+
id?: string;
|
|
36
|
+
title: string;
|
|
37
|
+
summary?: string;
|
|
38
|
+
icon?: string;
|
|
39
|
+
isSelected?: boolean;
|
|
40
|
+
// When item represents an audio item, clicking on it will start playback
|
|
41
|
+
// and secondary action will allow to add it to queue or playlist
|
|
42
|
+
action?: Action | PresentSubMenuAction;
|
|
43
|
+
secondaryAction?: SecondaryAction;
|
|
44
|
+
submenu?: MenuItem[]; // todo: not needed. Must be replaced with PresentSubMenuAction
|
|
45
|
+
trailingButton?: any;
|
|
46
|
+
entryActions?: EntryActionGroup[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type SelectMode = "single" | "multi" | "none";
|
|
50
|
+
|
|
51
|
+
export interface SelectionBehavior {
|
|
52
|
+
selectMode?: SelectMode;
|
|
53
|
+
currentSelection?: string[];
|
|
54
|
+
selector?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Operations that act on a specific item in the collection.
|
|
59
|
+
* Their action payloads are looked up on the item's entryActions (e.g. "remove_item", "reorder_item").
|
|
60
|
+
*/
|
|
61
|
+
export type ItemScopedOperation = "remove" | "reorder";
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Operations that act on the collection as a whole.
|
|
65
|
+
* Their action payloads are looked up on dynamicCollection.events (e.g. events.add, events.clearAll, events.delete).
|
|
66
|
+
*/
|
|
67
|
+
export type CollectionScopedOperation = "add" | "clearAll" | "delete";
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* UI display or behavior flags that do not require action payloads.
|
|
71
|
+
*/
|
|
72
|
+
export type UIOperation = "nowPlaying";
|
|
73
|
+
|
|
74
|
+
export type Operation =
|
|
75
|
+
| ItemScopedOperation
|
|
76
|
+
| CollectionScopedOperation
|
|
77
|
+
| UIOperation;
|
|
78
|
+
|
|
79
|
+
export interface DynamicCollectionOptions {
|
|
80
|
+
operations: Operation[];
|
|
81
|
+
postUrl?: string;
|
|
82
|
+
provider?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Action handlers for CollectionScopedOperations (e.g. "add", "clearAll", "delete").
|
|
85
|
+
*/
|
|
86
|
+
events?: Record<CollectionScopedOperation | string, Action[]>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface Content {
|
|
90
|
+
title: string;
|
|
91
|
+
stickyTitle?: boolean;
|
|
92
|
+
items: MenuItem[];
|
|
93
|
+
itemsUrl?: string;
|
|
94
|
+
action?: Action;
|
|
95
|
+
role?: string;
|
|
96
|
+
behavior?: SelectionBehavior;
|
|
97
|
+
dynamicCollection?: DynamicCollectionOptions;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface Menu {
|
|
101
|
+
header?: Header;
|
|
102
|
+
content: Content;
|
|
103
|
+
closeButton?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function isPresentSubMenuAction(
|
|
107
|
+
action: any
|
|
108
|
+
): action is PresentSubMenuAction {
|
|
109
|
+
return !!action && typeof action === "object" && "menu" in action;
|
|
110
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import React, { useState, useEffect } from "react";
|
|
2
|
+
import { toBooleanWithDefaultFalse } from "@applicaster/zapp-react-native-utils/booleanUtils";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
isPresentSubMenuAction,
|
|
6
|
+
MenuItem,
|
|
7
|
+
modalOrchestrator,
|
|
8
|
+
} from "./ModalOrchestrator";
|
|
9
|
+
|
|
10
|
+
import { ContentViewModel } from "./ContentViewModel";
|
|
11
|
+
import type { SelectionBehavior } from "./types";
|
|
12
|
+
import type { EditableCollection } from "./EditableCollection";
|
|
13
|
+
|
|
14
|
+
type Props = {
|
|
15
|
+
viewModel?: ContentViewModel | null;
|
|
16
|
+
onPress: (item: MenuItem) => void | Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type Return = {
|
|
20
|
+
items: MenuItem[];
|
|
21
|
+
isLoading: boolean;
|
|
22
|
+
error?: Error | string;
|
|
23
|
+
showCentralLoading: boolean; // isLoading && items.length === 0
|
|
24
|
+
canGoBack: boolean; // modalOrchestrator.canGoBack
|
|
25
|
+
handleItemPress: (item: MenuItem) => Promise<void>;
|
|
26
|
+
goBackOrClose: () => void; // canGoBack ? back() : close()
|
|
27
|
+
close: () => void; // modalOrchestrator.close()
|
|
28
|
+
role?: string;
|
|
29
|
+
behavior?: SelectionBehavior;
|
|
30
|
+
editableCollection?: EditableCollection | null;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const useBottomSheetContent = (props: Props): Return => {
|
|
34
|
+
const { onPress, viewModel } = props;
|
|
35
|
+
|
|
36
|
+
const [state, setState] = useState(viewModel?.getState());
|
|
37
|
+
|
|
38
|
+
const [editableItems, setEditableItems] = useState<MenuItem[]>([]);
|
|
39
|
+
|
|
40
|
+
// Subscribe to the viewModel state changes
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
return viewModel?.subscribe((nextState) => {
|
|
43
|
+
setState(nextState);
|
|
44
|
+
});
|
|
45
|
+
}, [viewModel]);
|
|
46
|
+
|
|
47
|
+
// Subscribe to editable collection items
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (viewModel?.editableCollection) {
|
|
50
|
+
const subscription =
|
|
51
|
+
viewModel.editableCollection.items$.subscribe(setEditableItems);
|
|
52
|
+
|
|
53
|
+
return () => subscription.unsubscribe();
|
|
54
|
+
}
|
|
55
|
+
}, [viewModel?.editableCollection]);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Handles an item press:
|
|
59
|
+
* - Delegates to the parent onPress (which runs the action and awaits it).
|
|
60
|
+
* - If the item was a leaf action (not a submenu), triggers viewModel.refetch()
|
|
61
|
+
* so that checkmarks (isSelected) reflect the updated server state.
|
|
62
|
+
*/
|
|
63
|
+
const handleItemPress = async (item: MenuItem) => {
|
|
64
|
+
const isSubMenu =
|
|
65
|
+
(item.submenu && item.submenu.length > 0) ||
|
|
66
|
+
(item.action && isPresentSubMenuAction(item.action));
|
|
67
|
+
|
|
68
|
+
await onPress(item);
|
|
69
|
+
|
|
70
|
+
if (!isSubMenu) {
|
|
71
|
+
await viewModel?.refetch();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const goBackOrClose = React.useCallback(() => {
|
|
76
|
+
if (modalOrchestrator.canGoBack) {
|
|
77
|
+
modalOrchestrator.back();
|
|
78
|
+
} else {
|
|
79
|
+
modalOrchestrator.close();
|
|
80
|
+
}
|
|
81
|
+
}, [modalOrchestrator.canGoBack]);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
items: viewModel?.editableCollection ? editableItems : state?.items,
|
|
85
|
+
isLoading: toBooleanWithDefaultFalse(state?.isLoading),
|
|
86
|
+
error: state?.error,
|
|
87
|
+
showCentralLoading: toBooleanWithDefaultFalse(
|
|
88
|
+
state?.isLoading && state?.items.length === 0
|
|
89
|
+
),
|
|
90
|
+
canGoBack: modalOrchestrator.canGoBack,
|
|
91
|
+
handleItemPress,
|
|
92
|
+
goBackOrClose,
|
|
93
|
+
close: () => modalOrchestrator.close(),
|
|
94
|
+
role: state?.role,
|
|
95
|
+
behavior: state?.behavior,
|
|
96
|
+
editableCollection: viewModel?.editableCollection,
|
|
97
|
+
};
|
|
98
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "16.0.0-alpha.
|
|
3
|
+
"version": "16.0.0-alpha.1052711356",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "16.0.0-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "16.0.0-alpha.1052711356",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {
|
|
2
|
+
batchSaveOwnedValues,
|
|
3
|
+
batchRemoveOwnedValues,
|
|
4
|
+
} from "../localStorageHelper";
|
|
5
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
6
|
+
|
|
7
|
+
// Mock localStorage
|
|
8
|
+
jest.mock(
|
|
9
|
+
"@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage",
|
|
10
|
+
() => ({
|
|
11
|
+
localStorage: {
|
|
12
|
+
getItem: jest.fn(),
|
|
13
|
+
setItem: jest.fn(),
|
|
14
|
+
removeItem: jest.fn(),
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
describe("localStorageHelper", () => {
|
|
20
|
+
const ownershipKey = "test_ownership_key";
|
|
21
|
+
const ownershipNamespace = "test_namespace";
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.clearAllMocks();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe("addOwnedKeys via batchSaveOwnedValues", () => {
|
|
28
|
+
it("should add new keys to existing owned keys", async () => {
|
|
29
|
+
// Setup existing keys in storage
|
|
30
|
+
const existingKeys = {
|
|
31
|
+
namespace1: ["key1"],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
35
|
+
JSON.stringify(existingKeys)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const newValues = {
|
|
39
|
+
namespace1: { key2: "value2" },
|
|
40
|
+
namespace2: { key3: "value3" },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
await batchSaveOwnedValues({
|
|
44
|
+
storageValues: newValues,
|
|
45
|
+
ownershipKey,
|
|
46
|
+
ownershipNamespace,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Verify localStorage.setItem was called with correct updated keys
|
|
50
|
+
// Note: order of keys in array might vary depending on Set implementation, but usually insertion order
|
|
51
|
+
// However, to be safe, we can parse the argument and check contents
|
|
52
|
+
|
|
53
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
54
|
+
(call) => call[0] === ownershipKey
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(lastCall).toBeDefined();
|
|
58
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
59
|
+
|
|
60
|
+
expect(savedData.namespace1).toContain("key1");
|
|
61
|
+
expect(savedData.namespace1).toContain("key2");
|
|
62
|
+
expect(savedData.namespace2).toContain("key3");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should handle empty existing keys", async () => {
|
|
66
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(null);
|
|
67
|
+
|
|
68
|
+
const newValues = {
|
|
69
|
+
namespace1: { key1: "value1" },
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
await batchSaveOwnedValues({
|
|
73
|
+
storageValues: newValues,
|
|
74
|
+
ownershipKey,
|
|
75
|
+
ownershipNamespace,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
79
|
+
(call) => call[0] === ownershipKey
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(lastCall).toBeDefined();
|
|
83
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
84
|
+
|
|
85
|
+
expect(savedData.namespace1).toEqual(["key1"]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("removeOwnedKeys via batchRemoveOwnedValues", () => {
|
|
90
|
+
it("should remove keys from owned keys", async () => {
|
|
91
|
+
const existingKeys = {
|
|
92
|
+
namespace1: ["key1", "key2"],
|
|
93
|
+
namespace2: ["key3"],
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
97
|
+
JSON.stringify(existingKeys)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const keysToRemove = {
|
|
101
|
+
namespace1: ["key1"],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await batchRemoveOwnedValues({
|
|
105
|
+
storageValues: keysToRemove,
|
|
106
|
+
ownershipKey,
|
|
107
|
+
ownershipNamespace,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const lastCall = (localStorage.setItem as jest.Mock).mock.calls.find(
|
|
111
|
+
(call) => call[0] === ownershipKey
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(lastCall).toBeDefined();
|
|
115
|
+
const savedData = JSON.parse(lastCall[1]);
|
|
116
|
+
|
|
117
|
+
expect(savedData.namespace1).toEqual(["key2"]);
|
|
118
|
+
expect(savedData.namespace2).toEqual(["key3"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should remove namespace if all keys removed", async () => {
|
|
122
|
+
const existingKeys = {
|
|
123
|
+
namespace1: ["key1"],
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
(localStorage.getItem as jest.Mock).mockResolvedValue(
|
|
127
|
+
JSON.stringify(existingKeys)
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const keysToRemove = {
|
|
131
|
+
namespace1: ["key1"],
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
await batchRemoveOwnedValues({
|
|
135
|
+
storageValues: keysToRemove,
|
|
136
|
+
ownershipKey,
|
|
137
|
+
ownershipNamespace,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(localStorage.removeItem).toHaveBeenCalledWith(
|
|
141
|
+
ownershipKey,
|
|
142
|
+
ownershipNamespace
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -2,10 +2,10 @@ import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/
|
|
|
2
2
|
import { isNilOrEmpty } from "../reactUtils/helpers";
|
|
3
3
|
import { parseJsonIfNeeded } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
4
4
|
import {
|
|
5
|
-
StorageOwnedValues,
|
|
6
5
|
NamespaceValues,
|
|
7
|
-
|
|
6
|
+
StorageOwnedValues,
|
|
8
7
|
StorageValuesToAdd,
|
|
8
|
+
StorageValuesToRemove,
|
|
9
9
|
} from "./types";
|
|
10
10
|
import { Storage } from "@applicaster/zapp-react-native-bridge/ZappStorage/Storage";
|
|
11
11
|
|
|
@@ -43,10 +43,10 @@ function mapOwnedKeysToAdd(
|
|
|
43
43
|
): StorageOwnedValues {
|
|
44
44
|
const mappedData = {};
|
|
45
45
|
|
|
46
|
-
Object.keys(storageValues)
|
|
46
|
+
for (const namespace of Object.keys(storageValues)) {
|
|
47
47
|
const data = storageValues[namespace];
|
|
48
48
|
mappedData[namespace] = Object.keys(data);
|
|
49
|
-
}
|
|
49
|
+
}
|
|
50
50
|
|
|
51
51
|
return mappedData;
|
|
52
52
|
}
|
|
@@ -111,12 +111,12 @@ async function addOwnedKeys({
|
|
|
111
111
|
ownershipNamespace
|
|
112
112
|
);
|
|
113
113
|
|
|
114
|
-
Object.keys(newKeys)
|
|
114
|
+
for (const namespace of Object.keys(newKeys)) {
|
|
115
115
|
const newOwnedKeys: string[] = newKeys[namespace];
|
|
116
116
|
const currentKeys = allStoragedOwnedKeys[namespace] || [];
|
|
117
117
|
const combinedSet = new Set([...currentKeys, ...newOwnedKeys]);
|
|
118
118
|
allStoragedOwnedKeys[namespace] = Array.from(combinedSet);
|
|
119
|
-
}
|
|
119
|
+
}
|
|
120
120
|
|
|
121
121
|
const data = JSON.stringify(allStoragedOwnedKeys);
|
|
122
122
|
await localStorage.setItem(ownershipKey, data, ownershipNamespace);
|
|
@@ -136,18 +136,14 @@ async function removeOwnedKeys({
|
|
|
136
136
|
ownershipNamespace
|
|
137
137
|
);
|
|
138
138
|
|
|
139
|
-
Object.keys(toRemoveKeys)
|
|
139
|
+
for (const namespace of Object.keys(toRemoveKeys)) {
|
|
140
140
|
const keysToRemove: string[] = toRemoveKeys[namespace] || [];
|
|
141
141
|
const currentKeys: string[] = currentKeysList[namespace] || [];
|
|
142
142
|
|
|
143
143
|
const storageOwnedSet = new Set(currentKeys);
|
|
144
144
|
const keysToRemoveSet = new Set(keysToRemove);
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
toBeRemovedSet.forEach(Set.prototype.delete, originalSet);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
removeAll(storageOwnedSet, keysToRemoveSet);
|
|
146
|
+
keysToRemoveSet.forEach((key) => storageOwnedSet.delete(key));
|
|
151
147
|
const newLoginKeys = Array.from(storageOwnedSet);
|
|
152
148
|
|
|
153
149
|
if (isNilOrEmpty(newLoginKeys)) {
|
|
@@ -155,7 +151,7 @@ async function removeOwnedKeys({
|
|
|
155
151
|
} else {
|
|
156
152
|
currentKeysList[namespace] = newLoginKeys;
|
|
157
153
|
}
|
|
158
|
-
}
|
|
154
|
+
}
|
|
159
155
|
|
|
160
156
|
if (isNilOrEmpty(currentKeysList)) {
|
|
161
157
|
await localStorage.removeItem(ownershipKey, ownershipNamespace);
|
|
File without changes
|