reducer.ts
1.89 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
import React, { Reducer } from 'react';
import { WebViewNavigation } from 'react-native-webview';
import { ImmerReducer } from '@huse/immer';
import {
WebViewError,
WebViewProgressEvent,
WebViewNativeProgressEvent,
WebViewNativeEvent,
} from 'react-native-webview/lib/WebViewTypes';
export type WebviewActionTypes =
| 'ChangeNavigationState'
| 'OnLoadError'
| 'OnLoadProgress'
| 'LoadText';
type PayloadAction<Type extends WebviewActionTypes, Payload> = {
type: Type;
payload: Payload;
};
export type WebviewActions =
| PayloadAction<'ChangeNavigationState', WebViewNavigation>
| PayloadAction<'LoadText', string>
| PayloadAction<'OnLoadError', WebViewError>
| PayloadAction<'OnLoadProgress', WebViewNativeProgressEvent>;
type PayloadOf<
A extends { type: string; payload: any },
T extends string
> = A extends {
type: T;
payload: infer R;
}
? R
: never;
const createAction = <
T extends WebviewActionTypes,
P extends PayloadOf<WebviewActions, T>
>(
type: T
) => (payload: P) => ({
type,
payload,
});
export const webActions = {
changeNavigationState: createAction('ChangeNavigationState'),
onLoadError: createAction('OnLoadError'),
onLoadProgress: createAction('OnLoadProgress'),
loadText: createAction('LoadText'),
};
export type WebviewState = WebViewNativeEvent & {
domain?: string;
code?: number;
description?: string;
progress: number;
};
export const defaultState: WebviewState = {
canGoBack: false,
canGoForward: false,
url: 'https://about:blank',
title: '',
lockIdentifier: 0,
loading: false,
progress: 0,
};
export const reducer: ImmerReducer<WebviewState, WebviewActions> = (
state,
action
) => {
switch (action.type) {
case 'ChangeNavigationState':
case 'OnLoadProgress':
case 'OnLoadError':
return { ...state, ...action.payload };
case 'LoadText':
state.url = action.payload;
break;
}
};