@applicaster/zapp-react-native-utils 16.0.0-rc.75 → 16.0.0-rc.77
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 +16 -0
- package/actionsExecutor/actions/__tests__/goBack.test.ts +62 -0
- package/actionsExecutor/actions/__tests__/goHome.test.ts +19 -0
- package/actionsExecutor/actions/goBack.ts +60 -0
- package/actionsExecutor/actions/goHome.ts +31 -0
- package/actionsExecutor/actions/index.ts +4 -0
- package/actionsExecutor/consts.ts +2 -0
- package/package.json +2 -2
|
@@ -18,6 +18,8 @@ import { createLogger } from "../logger";
|
|
|
18
18
|
import {
|
|
19
19
|
appRestartAction,
|
|
20
20
|
confirmDialogAction,
|
|
21
|
+
createGoBackAction,
|
|
22
|
+
createGoHomeAction,
|
|
21
23
|
createNavigateToScreenAction,
|
|
22
24
|
dismissBottomSheetAction,
|
|
23
25
|
localStorageRemoveAction,
|
|
@@ -189,6 +191,20 @@ export function withActionExecutor(Component) {
|
|
|
189
191
|
);
|
|
190
192
|
}, [navigator, rivers, contentTypes]);
|
|
191
193
|
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
return _actionExecutor.registerAction(
|
|
196
|
+
ACTION_TYPES.GO_BACK,
|
|
197
|
+
createGoBackAction(navigator)
|
|
198
|
+
);
|
|
199
|
+
}, [navigator]);
|
|
200
|
+
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
return _actionExecutor.registerAction(
|
|
203
|
+
ACTION_TYPES.GO_HOME,
|
|
204
|
+
createGoHomeAction(navigator)
|
|
205
|
+
);
|
|
206
|
+
}, [navigator]);
|
|
207
|
+
|
|
192
208
|
return (
|
|
193
209
|
<ActionExecutorContext.Provider value={handlers}>
|
|
194
210
|
<Component {...props} />
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createGoBackAction } from "../goBack";
|
|
2
|
+
import { ActionResult } from "../../ActionExecutor";
|
|
3
|
+
|
|
4
|
+
function createNavigator(canGoBack = true) {
|
|
5
|
+
return {
|
|
6
|
+
goBack: jest.fn(),
|
|
7
|
+
canGoBack: jest.fn().mockReturnValue(canGoBack),
|
|
8
|
+
} as unknown as QuickBrickAppNavigator;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("goBack", () => {
|
|
12
|
+
it("goes back with fallbackToHome enabled when no options are provided", async () => {
|
|
13
|
+
const navigator = createNavigator();
|
|
14
|
+
|
|
15
|
+
const result = await createGoBackAction(navigator)({ type: "goBack" });
|
|
16
|
+
|
|
17
|
+
expect(navigator.goBack).toHaveBeenCalledWith(true, false, false);
|
|
18
|
+
expect(result).toBe(ActionResult.Success);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("forwards backToTop", async () => {
|
|
22
|
+
const navigator = createNavigator();
|
|
23
|
+
|
|
24
|
+
const action = {
|
|
25
|
+
type: "goBack",
|
|
26
|
+
options: { backToTop: true },
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const result = await createGoBackAction(navigator)(action);
|
|
30
|
+
|
|
31
|
+
expect(navigator.goBack).toHaveBeenCalledWith(true, false, true);
|
|
32
|
+
expect(result).toBe(ActionResult.Success);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("goes back without falling back to home when the stack allows it", async () => {
|
|
36
|
+
const navigator = createNavigator(true);
|
|
37
|
+
|
|
38
|
+
const action = {
|
|
39
|
+
type: "goBack",
|
|
40
|
+
options: { fallbackToHome: false },
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const result = await createGoBackAction(navigator)(action);
|
|
44
|
+
|
|
45
|
+
expect(navigator.goBack).toHaveBeenCalledWith(false, false, false);
|
|
46
|
+
expect(result).toBe(ActionResult.Success);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("does nothing when there is nothing to go back to and fallbackToHome is off", async () => {
|
|
50
|
+
const navigator = createNavigator(false);
|
|
51
|
+
|
|
52
|
+
const action = {
|
|
53
|
+
type: "goBack",
|
|
54
|
+
options: { fallbackToHome: false },
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const result = await createGoBackAction(navigator)(action);
|
|
58
|
+
|
|
59
|
+
expect(navigator.goBack).not.toHaveBeenCalled();
|
|
60
|
+
expect(result).toBe(ActionResult.Error);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createGoHomeAction } from "../goHome";
|
|
2
|
+
import { ActionResult } from "../../ActionExecutor";
|
|
3
|
+
|
|
4
|
+
function createNavigator() {
|
|
5
|
+
return {
|
|
6
|
+
goHome: jest.fn(),
|
|
7
|
+
} as unknown as QuickBrickAppNavigator;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe("goHome", () => {
|
|
11
|
+
it("navigates to the home screen", async () => {
|
|
12
|
+
const navigator = createNavigator();
|
|
13
|
+
|
|
14
|
+
const result = await createGoHomeAction(navigator)({ type: "goHome" });
|
|
15
|
+
|
|
16
|
+
expect(navigator.goHome).toHaveBeenCalledWith();
|
|
17
|
+
expect(result).toBe(ActionResult.Success);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { ActionResult } from "../ActionExecutor";
|
|
3
|
+
import { ActionHandler } from "../types";
|
|
4
|
+
import { createLogger } from "../../logger";
|
|
5
|
+
|
|
6
|
+
const { log_info } = createLogger({
|
|
7
|
+
subsystem: "ActionExecutorContext",
|
|
8
|
+
category: "General",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for the goBack action.
|
|
13
|
+
*/
|
|
14
|
+
export interface GoBackOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Navigate to the home screen when there is no screen to go back to.
|
|
17
|
+
* Defaults to true.
|
|
18
|
+
*/
|
|
19
|
+
fallbackToHome?: boolean;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Go back to the first screen of the stack instead of the previous one.
|
|
23
|
+
* Defaults to false.
|
|
24
|
+
*/
|
|
25
|
+
backToTop?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates a go back action handler with the provided dependencies.
|
|
30
|
+
* This factory pattern allows the action to access navigation state at runtime.
|
|
31
|
+
*
|
|
32
|
+
* @param navigator - The app navigator performing the back navigation
|
|
33
|
+
* @returns An action handler that navigates back
|
|
34
|
+
*/
|
|
35
|
+
export function createGoBackAction(
|
|
36
|
+
navigator: QuickBrickAppNavigator
|
|
37
|
+
): ActionHandler<GoBackOptions> {
|
|
38
|
+
return async function goBackAction(action): Promise<ActionResult> {
|
|
39
|
+
const fallbackToHome = action.options?.fallbackToHome ?? true;
|
|
40
|
+
const backToTop = action.options?.backToTop ?? false;
|
|
41
|
+
|
|
42
|
+
// With fallbackToHome on, the navigator goes home on an empty stack by
|
|
43
|
+
// itself. The guard only matters when the caller opted out of that.
|
|
44
|
+
if (!fallbackToHome && !navigator.canGoBack()) {
|
|
45
|
+
log_info(
|
|
46
|
+
"goBack: no screen to go back to and fallbackToHome is disabled, ignoring"
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return ActionResult.Error;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
log_info(
|
|
53
|
+
`goBack: navigating back, fallbackToHome: ${fallbackToHome}, backToTop: ${backToTop}`
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
navigator.goBack(fallbackToHome, false, backToTop);
|
|
57
|
+
|
|
58
|
+
return ActionResult.Success;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { ActionResult } from "../ActionExecutor";
|
|
3
|
+
import { ActionHandler } from "../types";
|
|
4
|
+
import { createLogger } from "../../logger";
|
|
5
|
+
|
|
6
|
+
const { log_info } = createLogger({
|
|
7
|
+
subsystem: "ActionExecutorContext",
|
|
8
|
+
category: "General",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a go home action handler with the provided dependencies.
|
|
13
|
+
* This factory pattern allows the action to access navigation state at runtime.
|
|
14
|
+
*
|
|
15
|
+
* The navigator's `initialLaunch` flag is intentionally not exposed: it selects
|
|
16
|
+
* the offline home river during app bootstrap and is not a per-action choice.
|
|
17
|
+
*
|
|
18
|
+
* @param navigator - The app navigator performing the navigation
|
|
19
|
+
* @returns An action handler that navigates to the home screen
|
|
20
|
+
*/
|
|
21
|
+
export function createGoHomeAction(
|
|
22
|
+
navigator: QuickBrickAppNavigator
|
|
23
|
+
): ActionHandler {
|
|
24
|
+
return async function goHomeAction(): Promise<ActionResult> {
|
|
25
|
+
log_info("goHome: navigating to the home screen");
|
|
26
|
+
|
|
27
|
+
navigator.goHome();
|
|
28
|
+
|
|
29
|
+
return ActionResult.Success;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -27,6 +27,10 @@ export { screenToggleFlagAction } from "./screenToggleFlag";
|
|
|
27
27
|
|
|
28
28
|
export { createNavigateToScreenAction } from "./navigateToScreen";
|
|
29
29
|
|
|
30
|
+
export { createGoBackAction } from "./goBack";
|
|
31
|
+
|
|
32
|
+
export { createGoHomeAction } from "./goHome";
|
|
33
|
+
|
|
30
34
|
export { showToastAction } from "./showToast";
|
|
31
35
|
|
|
32
36
|
export { openBottomSheetAction } from "./openBottomSheet";
|
|
@@ -9,6 +9,8 @@ export const ACTION_TYPES = {
|
|
|
9
9
|
PRESENT_SUB_MENU: "presentSubMenu",
|
|
10
10
|
SEND_CLOUD_EVENT: "sendCloudEvent",
|
|
11
11
|
NAVIGATE_TO_SCREEN: "navigateToScreen",
|
|
12
|
+
GO_BACK: "goBack",
|
|
13
|
+
GO_HOME: "goHome",
|
|
12
14
|
SHOW_TOAST: "showToast",
|
|
13
15
|
LOCAL_STORAGE_SET: "localStorageSet",
|
|
14
16
|
LOCAL_STORAGE_REMOVE: "localStorageRemove",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "16.0.0-rc.
|
|
3
|
+
"version": "16.0.0-rc.77",
|
|
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-rc.
|
|
30
|
+
"@applicaster/applicaster-types": "16.0.0-rc.77",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|