-
Notifications
You must be signed in to change notification settings - Fork 22
/
actions.ts
72 lines (61 loc) · 1.52 KB
/
actions.ts
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
import type { LunaticState } from './type';
export enum ActionKind {
GO_PREVIOUS_PAGE = 'use-lunatic/go-previous',
GO_NEXT_PAGE = 'use-lunatic/go-next',
GO_TO_PAGE = 'use-lunatic/go-to-page',
HANDLE_CHANGES = 'use-lunatic/handle-changes',
}
export type ActionHandleChanges = {
type: ActionKind.HANDLE_CHANGES;
payload: {
responses: {
name: string;
value: unknown;
iteration?: number[];
[extra: string]: unknown;
}[];
};
};
export type ActionGoToPage = {
type: ActionKind.GO_TO_PAGE;
payload: Parameters<LunaticState['goToPage']>[0];
};
export type ActionGoNextPage = {
type: ActionKind.GO_NEXT_PAGE;
payload: object;
};
export type ActionGoPreviousPage = {
type: ActionKind.GO_PREVIOUS_PAGE;
payload: object;
};
export type Action =
| ActionGoNextPage
| ActionGoPreviousPage
| ActionGoToPage
| ActionHandleChanges;
export type PayloadForAction<T extends Action['type']> = (Action & {
type: T;
})['payload'];
const actionCreator =
<T extends ActionKind>(type: T) =>
(payload: (Action & { type: T })['payload']) => {
return {
type,
payload,
} as Action & { type: T };
};
export const goPreviousPageAction = () =>
({
type: ActionKind.GO_PREVIOUS_PAGE,
payload: {},
}) as const;
export const goNextPageAction = actionCreator(ActionKind.GO_NEXT_PAGE);
export const goToPageAction = actionCreator(ActionKind.GO_TO_PAGE);
export const handleChangesAction = (
responses: ActionHandleChanges['payload']['responses']
): Action => ({
type: ActionKind.HANDLE_CHANGES,
payload: {
responses,
},
});