@applicaster/zapp-react-native-utils 14.0.0-alpha.1054425138 → 14.0.0-alpha.1430213104
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 +82 -53
- package/appUtils/contextKeysManager/contextResolver.ts +1 -15
- package/package.json +2 -2
- package/playerUtils/index.ts +6 -0
- package/reactHooks/cell-click/index.ts +1 -5
- package/reactHooks/feed/useFeedLoader.tsx +6 -13
- package/actionsExecutor/ScreenActions.ts +0 -90
- package/actionsExecutor/StorageActions.ts +0 -110
|
@@ -16,6 +16,7 @@ import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickB
|
|
|
16
16
|
import { showConfirmationDialog } from "../alertUtils";
|
|
17
17
|
import { createCloudEvent, sendCloudEvent } from "../cloudEventsUtils";
|
|
18
18
|
import { createLogger } from "../logger";
|
|
19
|
+
import { StorageMultiSelectProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/StorageMultiSelectProvider";
|
|
19
20
|
import { ACTIVE_LAYOUT_ID_STORAGE_KEY } from "@applicaster/quick-brick-core/App/remoteContextReloader/consts";
|
|
20
21
|
import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
|
|
21
22
|
import { loadPipesData } from "@applicaster/zapp-react-native-redux/ZappPipes";
|
|
@@ -24,20 +25,15 @@ import {
|
|
|
24
25
|
resolveObjectValues,
|
|
25
26
|
} from "../appUtils/contextKeysManager/contextResolver";
|
|
26
27
|
import { useNavigation } from "../reactHooks";
|
|
28
|
+
import { get } from "lodash";
|
|
27
29
|
|
|
28
30
|
import {
|
|
29
31
|
useContentTypes,
|
|
30
32
|
usePickFromState,
|
|
31
33
|
} from "@applicaster/zapp-react-native-redux/hooks";
|
|
32
|
-
import {
|
|
34
|
+
import { TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT } from "./consts";
|
|
35
|
+
import { postEvent, useSubscriberFor } from "../reactHooks/useSubscriberFor";
|
|
33
36
|
import { APP_EVENTS } from "../appUtils/events";
|
|
34
|
-
import {
|
|
35
|
-
localStorageToggleFlag,
|
|
36
|
-
sessionStorageToggleFlag,
|
|
37
|
-
} from "./StorageActions";
|
|
38
|
-
|
|
39
|
-
import { ScreenSingleValueProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/ScreenSingleValueProvider";
|
|
40
|
-
import { screenSetVariable, screenToggleFlag } from "./ScreenActions";
|
|
41
37
|
|
|
42
38
|
export const { log_error, log_info, log_debug } = createLogger({
|
|
43
39
|
subsystem: "ActionExecutorContext",
|
|
@@ -86,6 +82,19 @@ function findParentComponent(
|
|
|
86
82
|
return null;
|
|
87
83
|
}
|
|
88
84
|
|
|
85
|
+
// send all data just in case (like for message string formatting)
|
|
86
|
+
// Type is not exported for now
|
|
87
|
+
type MaxTagsReachedEvent = {
|
|
88
|
+
selectedItems: string[];
|
|
89
|
+
maxItems: number;
|
|
90
|
+
tag: string;
|
|
91
|
+
keyNamespace: string;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
async function onMaxTagsReached(data: MaxTagsReachedEvent) {
|
|
95
|
+
postEvent(TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT, [data]);
|
|
96
|
+
}
|
|
97
|
+
|
|
89
98
|
const prepareDefaultActions = (actionExecutor) => {
|
|
90
99
|
actionExecutor.registerAction("localStorageSet", async (action) => {
|
|
91
100
|
const namespaces = action.options.content;
|
|
@@ -169,16 +178,10 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
169
178
|
|
|
170
179
|
const entry = context?.entry || {};
|
|
171
180
|
const entryResolver = new EntryResolver(entry);
|
|
172
|
-
const route = context.screenRoute;
|
|
173
|
-
const screenData = ScreenSingleValueProvider.getState(route) as any;
|
|
174
|
-
const screenResolver = new EntryResolver(screenData || {});
|
|
175
181
|
|
|
176
182
|
const data =
|
|
177
183
|
options?.data && options.inflateData
|
|
178
|
-
? await resolveObjectValues(options.data, {
|
|
179
|
-
entry: entryResolver,
|
|
180
|
-
screen: screenResolver,
|
|
181
|
-
})
|
|
184
|
+
? await resolveObjectValues(options.data, { entry: entryResolver })
|
|
182
185
|
: options?.data || entry;
|
|
183
186
|
|
|
184
187
|
const cloudEvent = await createCloudEvent({
|
|
@@ -212,55 +215,81 @@ const prepareDefaultActions = (actionExecutor) => {
|
|
|
212
215
|
return ActionResult.Error;
|
|
213
216
|
});
|
|
214
217
|
|
|
215
|
-
actionExecutor.registerAction(
|
|
216
|
-
"sessionStorageToggleFlag",
|
|
217
|
-
async (
|
|
218
|
-
action: ActionType,
|
|
219
|
-
context?: Record<string, any>
|
|
220
|
-
): Promise<ActionResult> => {
|
|
221
|
-
return await sessionStorageToggleFlag(context, action);
|
|
222
|
-
}
|
|
223
|
-
);
|
|
224
|
-
|
|
225
218
|
actionExecutor.registerAction(
|
|
226
219
|
"localStorageToggleFlag",
|
|
227
220
|
async (
|
|
228
221
|
action: ActionType,
|
|
229
222
|
context?: Record<string, any>
|
|
230
223
|
): Promise<ActionResult> => {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
224
|
+
if (!context) {
|
|
225
|
+
log_error(
|
|
226
|
+
"handleAction: localStorageToggleFlag action missing context"
|
|
227
|
+
);
|
|
234
228
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
async (
|
|
238
|
-
action: ActionType,
|
|
239
|
-
context?: Record<string, any>
|
|
240
|
-
): Promise<ActionResult> => {
|
|
241
|
-
const route = context?.screenRoute;
|
|
242
|
-
const { key, value } = action.options;
|
|
243
|
-
screenSetVariable(route, key, value);
|
|
229
|
+
return ActionResult.Error;
|
|
230
|
+
}
|
|
244
231
|
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
);
|
|
232
|
+
const entry = context?.entry as ZappEntry;
|
|
248
233
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
context?: Record<string, any>
|
|
254
|
-
): Promise<ActionResult> => {
|
|
255
|
-
const screenRoute = context?.screenRoute;
|
|
234
|
+
if (!entry) {
|
|
235
|
+
log_error(
|
|
236
|
+
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
237
|
+
);
|
|
256
238
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
239
|
+
return ActionResult.Error;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const tag = action.options?.selector
|
|
243
|
+
? get(entry, action.options.selector)
|
|
244
|
+
: (entry.extensions?.tag ?? entry.id);
|
|
245
|
+
|
|
246
|
+
const keyNamespace = action.options?.key;
|
|
247
|
+
|
|
248
|
+
if (keyNamespace && tag) {
|
|
249
|
+
const multiSelectProvider =
|
|
250
|
+
StorageMultiSelectProvider.getProvider(keyNamespace);
|
|
251
|
+
|
|
252
|
+
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
253
|
+
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
254
|
+
|
|
255
|
+
log_info(
|
|
256
|
+
`handleAction: localStorageToggleFlag event will ${
|
|
257
|
+
isTagInSelectedItems ? "remove" : "add"
|
|
258
|
+
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
259
|
+
);
|
|
262
260
|
|
|
263
|
-
|
|
261
|
+
if (selectedItems.includes(tag)) {
|
|
262
|
+
await multiSelectProvider.removeItem(tag);
|
|
263
|
+
} else {
|
|
264
|
+
const maxItems = action.options?.max_items;
|
|
265
|
+
|
|
266
|
+
if (maxItems && selectedItems.length >= maxItems) {
|
|
267
|
+
log_info(
|
|
268
|
+
`handleAction: localStorageToggleFlag event reached max items limit: ${maxItems}, cannot add tag: ${tag}`
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
await onMaxTagsReached({
|
|
272
|
+
selectedItems,
|
|
273
|
+
maxItems,
|
|
274
|
+
tag,
|
|
275
|
+
keyNamespace,
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
return ActionResult.Cancel;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
await multiSelectProvider.addItem(tag);
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
log_error(
|
|
285
|
+
"handleAction: localStorageToggleFlag event missing keyNamespace or tag",
|
|
286
|
+
{ keyNamespace, tag }
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return ActionResult.Error;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return ActionResult.Success;
|
|
264
293
|
}
|
|
265
294
|
);
|
|
266
295
|
};
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { ContextKeysManager } from "./index";
|
|
2
2
|
import * as R from "ramda";
|
|
3
|
-
import { screenStates } from "@applicaster/zapp-react-native-bridge/ZappStorage/ScreenSingleValueProvider";
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
interface IResolver {
|
|
6
5
|
resolve: (string) => Promise<string | number | object>;
|
|
7
6
|
}
|
|
8
7
|
|
|
9
|
-
// TODO: Rename to ObjectKeyResolver or similar
|
|
10
8
|
export class EntryResolver implements IResolver {
|
|
11
9
|
entry: ZappEntry;
|
|
12
10
|
|
|
@@ -23,18 +21,6 @@ export class EntryResolver implements IResolver {
|
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
// TODO: Move to proper place
|
|
27
|
-
|
|
28
|
-
export class ScreenStateResolver implements IResolver {
|
|
29
|
-
constructor(private route: string) {}
|
|
30
|
-
|
|
31
|
-
async resolve(key: string) {
|
|
32
|
-
const screenState = screenStates[this.route];
|
|
33
|
-
|
|
34
|
-
return screenState?.[key];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
24
|
export class ContextResolver implements IResolver {
|
|
39
25
|
resolve = async (compositeKey: string) =>
|
|
40
26
|
ContextKeysManager.instance.getKey(compositeKey);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "14.0.0-alpha.
|
|
3
|
+
"version": "14.0.0-alpha.1430213104",
|
|
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": "14.0.0-alpha.
|
|
30
|
+
"@applicaster/applicaster-types": "14.0.0-alpha.1430213104",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
package/playerUtils/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as R from "ramda";
|
|
2
2
|
import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
|
|
3
3
|
import { isString } from "@applicaster/zapp-react-native-utils/typeGuards";
|
|
4
|
+
import { isFilledArray } from "@applicaster/zapp-react-native-utils/arrayUtils";
|
|
5
|
+
import { isTV } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
4
6
|
|
|
5
7
|
import { getBoolFromConfigValue } from "../configurationUtils";
|
|
6
8
|
|
|
@@ -89,3 +91,7 @@ export const isAudioItem = (item: Option<ZappEntry>) => {
|
|
|
89
91
|
|
|
90
92
|
return false;
|
|
91
93
|
};
|
|
94
|
+
|
|
95
|
+
export const isInlineTV = (screenData) => {
|
|
96
|
+
return isTV() && isFilledArray(screenData?.ui_components);
|
|
97
|
+
};
|
|
@@ -16,7 +16,7 @@ import { ActionExecutorContext } from "@applicaster/zapp-react-native-utils/acti
|
|
|
16
16
|
import { isFunction, noop } from "../../functionUtils";
|
|
17
17
|
import { useSendAnalyticsOnPress } from "../analytics";
|
|
18
18
|
import { logOnPress, warnEmptyContentType } from "./helpers";
|
|
19
|
-
import { useCurrentScreenData
|
|
19
|
+
import { useCurrentScreenData } from "../screen";
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* If onCellTap is defined execute the function and
|
|
@@ -46,7 +46,6 @@ export const useCellClick = ({
|
|
|
46
46
|
const onCellTap: Option<Function> = React.useContext(CellTapContext);
|
|
47
47
|
const actionExecutor = React.useContext(ActionExecutorContext);
|
|
48
48
|
const screenData = useCurrentScreenData();
|
|
49
|
-
const screenState = useScreenContext()?.options;
|
|
50
49
|
|
|
51
50
|
const cellSelectable = toBooleanWithDefaultTrue(
|
|
52
51
|
component?.rules?.component_cells_selectable
|
|
@@ -84,8 +83,6 @@ export const useCellClick = ({
|
|
|
84
83
|
await actionExecutor?.handleEntryActions(selectedItem, {
|
|
85
84
|
component,
|
|
86
85
|
screenData,
|
|
87
|
-
screenState,
|
|
88
|
-
screenRoute: pathname,
|
|
89
86
|
});
|
|
90
87
|
}
|
|
91
88
|
|
|
@@ -120,7 +117,6 @@ export const useCellClick = ({
|
|
|
120
117
|
push,
|
|
121
118
|
sendAnalyticsOnPress,
|
|
122
119
|
screenData,
|
|
123
|
-
screenState,
|
|
124
120
|
]
|
|
125
121
|
);
|
|
126
122
|
|
|
@@ -8,7 +8,6 @@ import { reactHooksLogger } from "../logger";
|
|
|
8
8
|
import { shouldDispatchData, useIsInitialRender } from "../utils";
|
|
9
9
|
import { useInflatedUrl } from "./useInflatedUrl";
|
|
10
10
|
import { useRoute } from "../navigation";
|
|
11
|
-
import { useScreenResolvers } from "@applicaster/zapp-react-native-ui-components/Decorators/ZappPipesDataConnector";
|
|
12
11
|
|
|
13
12
|
const logger = reactHooksLogger.addSubsystem("useFeedLoader");
|
|
14
13
|
|
|
@@ -51,8 +50,7 @@ export const useFeedLoader = ({
|
|
|
51
50
|
|
|
52
51
|
const isInitialRender = useIsInitialRender();
|
|
53
52
|
const dispatch = useDispatch();
|
|
54
|
-
const { screenData
|
|
55
|
-
const resolvers = useScreenResolvers(pathname);
|
|
53
|
+
const { screenData } = useRoute();
|
|
56
54
|
|
|
57
55
|
const callableFeedUrl = useInflatedUrl({ feedUrl, mapping });
|
|
58
56
|
|
|
@@ -71,12 +69,11 @@ export const useFeedLoader = ({
|
|
|
71
69
|
silentRefresh,
|
|
72
70
|
callback,
|
|
73
71
|
riverId,
|
|
74
|
-
resolvers,
|
|
75
72
|
})
|
|
76
73
|
);
|
|
77
74
|
}
|
|
78
75
|
},
|
|
79
|
-
[callableFeedUrl
|
|
76
|
+
[callableFeedUrl]
|
|
80
77
|
);
|
|
81
78
|
|
|
82
79
|
const loadNext: FeedLoaderResponse["loadNext"] = React.useCallback(() => {
|
|
@@ -89,12 +86,11 @@ export const useFeedLoader = ({
|
|
|
89
86
|
silentRefresh: true,
|
|
90
87
|
parentFeed: callableFeedUrl,
|
|
91
88
|
riverId,
|
|
92
|
-
resolvers,
|
|
93
89
|
})
|
|
94
90
|
);
|
|
95
91
|
}
|
|
96
92
|
}
|
|
97
|
-
}, [callableFeedUrl, currentFeed?.data?.next
|
|
93
|
+
}, [callableFeedUrl, currentFeed?.data?.next]);
|
|
98
94
|
|
|
99
95
|
useEffect(() => {
|
|
100
96
|
if (
|
|
@@ -106,7 +102,6 @@ export const useFeedLoader = ({
|
|
|
106
102
|
...pipesOptions,
|
|
107
103
|
clearCache: true,
|
|
108
104
|
riverId,
|
|
109
|
-
resolvers,
|
|
110
105
|
})
|
|
111
106
|
);
|
|
112
107
|
} else if (!callableFeedUrl) {
|
|
@@ -131,16 +126,14 @@ export const useFeedLoader = ({
|
|
|
131
126
|
jsOnly: true,
|
|
132
127
|
});
|
|
133
128
|
}
|
|
134
|
-
}, [
|
|
129
|
+
}, []);
|
|
135
130
|
|
|
136
131
|
// Reload feed when feedUrl changes, unless skipLoading is true
|
|
137
132
|
useEffect(() => {
|
|
138
133
|
if (!isInitialRender && callableFeedUrl && !pipesOptions.skipLoading) {
|
|
139
|
-
dispatch(
|
|
140
|
-
loadPipesData(callableFeedUrl, { ...pipesOptions, riverId, resolvers })
|
|
141
|
-
);
|
|
134
|
+
dispatch(loadPipesData(callableFeedUrl, { ...pipesOptions, riverId }));
|
|
142
135
|
}
|
|
143
|
-
}, [callableFeedUrl
|
|
136
|
+
}, [callableFeedUrl]);
|
|
144
137
|
|
|
145
138
|
return React.useMemo(() => {
|
|
146
139
|
if (!callableFeedUrl || !feedUrl) {
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
2
|
-
|
|
3
|
-
import { log_error, log_info } from "./ActionExecutorContext";
|
|
4
|
-
import { ActionResult } from "./ActionExecutor";
|
|
5
|
-
import { get } from "lodash";
|
|
6
|
-
|
|
7
|
-
import { ScreenMultiSelectProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/ScreenStateMultiSelectProvider";
|
|
8
|
-
import { onMaxTagsReached } from "./StorageActions";
|
|
9
|
-
|
|
10
|
-
export const screenSetVariable = (
|
|
11
|
-
_screenRoute: string,
|
|
12
|
-
_key: string,
|
|
13
|
-
_value: string
|
|
14
|
-
) => {};
|
|
15
|
-
|
|
16
|
-
export const screenToggleFlag = async (
|
|
17
|
-
screenRoute: string,
|
|
18
|
-
context: Record<string, any>,
|
|
19
|
-
action: ActionType
|
|
20
|
-
) => {
|
|
21
|
-
if (!context) {
|
|
22
|
-
log_error("handleAction: localStorageToggleFlag action missing context");
|
|
23
|
-
|
|
24
|
-
return ActionResult.Error;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const entry = context?.entry as ZappEntry;
|
|
28
|
-
|
|
29
|
-
if (!entry) {
|
|
30
|
-
log_error(
|
|
31
|
-
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
return ActionResult.Error;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const tag = action.options?.selector
|
|
38
|
-
? get(entry, action.options.selector)
|
|
39
|
-
: (entry.extensions?.tag ?? entry.id);
|
|
40
|
-
|
|
41
|
-
const keyNamespace = action.options?.key;
|
|
42
|
-
|
|
43
|
-
if (keyNamespace && tag) {
|
|
44
|
-
const multiSelectProvider = ScreenMultiSelectProvider.getProvider(
|
|
45
|
-
keyNamespace,
|
|
46
|
-
screenRoute
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
50
|
-
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
51
|
-
|
|
52
|
-
log_info(
|
|
53
|
-
`handleAction: screenToggleFlag event will ${
|
|
54
|
-
isTagInSelectedItems ? "remove" : "add"
|
|
55
|
-
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
if (selectedItems.includes(tag)) {
|
|
59
|
-
await multiSelectProvider.removeItem(tag);
|
|
60
|
-
} else {
|
|
61
|
-
const maxItems = action.options?.max_items;
|
|
62
|
-
|
|
63
|
-
if (maxItems && selectedItems.length >= maxItems) {
|
|
64
|
-
log_info(
|
|
65
|
-
`handleAction: screenToggleFlag event reached max items limit: ${maxItems}, cannot add tag: ${tag}`
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
await onMaxTagsReached({
|
|
69
|
-
selectedItems,
|
|
70
|
-
maxItems,
|
|
71
|
-
tag,
|
|
72
|
-
keyNamespace,
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return ActionResult.Cancel;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
await multiSelectProvider.addItem(tag);
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
log_error(
|
|
82
|
-
"handleAction: screenToggleFlag event missing keyNamespace or tag",
|
|
83
|
-
{ keyNamespace, tag }
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
return ActionResult.Error;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return ActionResult.Success;
|
|
90
|
-
};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { ActionResult } from "./ActionExecutor";
|
|
2
|
-
import { get } from "lodash";
|
|
3
|
-
import { StorageMultiSelectProvider } from "@applicaster/zapp-react-native-bridge/ZappStorage/StorageMultiSelectProvider";
|
|
4
|
-
import { log_error, log_info } from "./ActionExecutorContext";
|
|
5
|
-
import { postEvent } from "../reactHooks/useSubscriberFor";
|
|
6
|
-
import { TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT } from "./consts";
|
|
7
|
-
import { StorageType } from "../appUtils/contextKeysManager/consts";
|
|
8
|
-
|
|
9
|
-
// send all data just in case (like for message string formatting)
|
|
10
|
-
// Type is not exported for now
|
|
11
|
-
type MaxTagsReachedEvent = {
|
|
12
|
-
selectedItems: string[];
|
|
13
|
-
maxItems: number;
|
|
14
|
-
tag: string;
|
|
15
|
-
keyNamespace: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export async function onMaxTagsReached(data: MaxTagsReachedEvent) {
|
|
19
|
-
postEvent(TOGGLE_FLAG_MAX_ITEMS_REACHED_EVENT, [data]);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function storageToggleFlag(
|
|
23
|
-
context: Record<string, any>,
|
|
24
|
-
action: ActionType,
|
|
25
|
-
storageType: StorageType
|
|
26
|
-
) {
|
|
27
|
-
if (!context) {
|
|
28
|
-
log_error("handleAction: localStorageToggleFlag action missing context");
|
|
29
|
-
|
|
30
|
-
return ActionResult.Error;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const entry = context?.entry as ZappEntry;
|
|
34
|
-
|
|
35
|
-
if (!entry) {
|
|
36
|
-
log_error(
|
|
37
|
-
"handleAction: localStorageToggleFlag action missing entry. Entry is required to get the tag."
|
|
38
|
-
);
|
|
39
|
-
|
|
40
|
-
return ActionResult.Error;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const tag = action.options?.selector
|
|
44
|
-
? get(entry, action.options.selector)
|
|
45
|
-
: (entry.extensions?.tag ?? entry.id);
|
|
46
|
-
|
|
47
|
-
const keyNamespace = action.options?.key;
|
|
48
|
-
|
|
49
|
-
if (keyNamespace && tag) {
|
|
50
|
-
const multiSelectProvider = StorageMultiSelectProvider.getProvider(
|
|
51
|
-
keyNamespace,
|
|
52
|
-
storageType
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const selectedItems = await multiSelectProvider.getSelectedAsync();
|
|
56
|
-
const isTagInSelectedItems = selectedItems.includes(tag);
|
|
57
|
-
|
|
58
|
-
log_info(
|
|
59
|
-
`handleAction: localStorageToggleFlag event will ${
|
|
60
|
-
isTagInSelectedItems ? "remove" : "add"
|
|
61
|
-
} tag: ${tag} for keyNamespace: ${keyNamespace}, current selectedItems: ${selectedItems}`
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (selectedItems.includes(tag)) {
|
|
65
|
-
await multiSelectProvider.removeItem(tag);
|
|
66
|
-
} else {
|
|
67
|
-
const maxItems = action.options?.max_items;
|
|
68
|
-
|
|
69
|
-
if (maxItems && selectedItems.length >= maxItems) {
|
|
70
|
-
log_info(
|
|
71
|
-
`handleAction: localStorageToggleFlag event reached max items limit: ${maxItems}, cannot add tag: ${tag}`
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
await onMaxTagsReached({
|
|
75
|
-
selectedItems,
|
|
76
|
-
maxItems,
|
|
77
|
-
tag,
|
|
78
|
-
keyNamespace,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
return ActionResult.Cancel;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
await multiSelectProvider.addItem(tag);
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
log_error(
|
|
88
|
-
"handleAction: localStorageToggleFlag event missing keyNamespace or tag",
|
|
89
|
-
{ keyNamespace, tag }
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
return ActionResult.Error;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return ActionResult.Success;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export async function sessionStorageToggleFlag(
|
|
99
|
-
context: Record<string, any>,
|
|
100
|
-
action: ActionType
|
|
101
|
-
) {
|
|
102
|
-
return storageToggleFlag(context, action, StorageType.session);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export async function localStorageToggleFlag(
|
|
106
|
-
context: Record<string, any>,
|
|
107
|
-
action: ActionType
|
|
108
|
-
) {
|
|
109
|
-
return storageToggleFlag(context, action, StorageType.local);
|
|
110
|
-
}
|