-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
protocolReducer.ts
69 lines (61 loc) · 2 KB
/
protocolReducer.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
import produce from 'immer';
import { PROTOCOL } from '@suite-actions/constants';
import type { Action } from '@suite-types';
import type { Network } from '@wallet-types';
export enum PROTOCOL_SCHEME {
BITCOIN = 'bitcoin',
AOPP = 'aopp',
}
export const PROTOCOL_TO_SYMBOL: { [key: string]: Network['symbol'] } = {
[PROTOCOL_SCHEME.BITCOIN]: 'btc',
};
export interface SendFormState {
address?: string;
amount?: number;
scheme?: PROTOCOL_SCHEME;
shouldFillSendForm?: boolean;
}
export interface AoppState {
message: string;
asset: Network['symbol'];
callback?: string;
format?: string;
}
export interface State {
sendForm: SendFormState;
aopp: Partial<AoppState> & {
shouldFill?: boolean;
};
}
export const initialState: State = {
sendForm: {},
aopp: {},
};
const protocolReducer = (state: State = initialState, action: Action): State =>
produce(state, draft => {
switch (action.type) {
case PROTOCOL.FILL_SEND_FORM:
draft.sendForm.shouldFillSendForm = action.payload;
break;
case PROTOCOL.SAVE_COIN_PROTOCOL:
draft.sendForm.address = action.payload.address;
draft.sendForm.scheme = action.payload.scheme;
draft.sendForm.amount = action.payload.amount;
draft.sendForm.shouldFillSendForm = false;
break;
case PROTOCOL.FILL_AOPP:
draft.aopp.shouldFill = action.payload;
break;
case PROTOCOL.SAVE_AOPP_PROTOCOL:
draft.aopp.message = action.payload.message;
draft.aopp.callback = action.payload.callback;
draft.aopp.asset = action.payload.asset;
draft.aopp.format = action.payload.format;
draft.aopp.shouldFill = false;
break;
case PROTOCOL.RESET:
return initialState;
// no default
}
});
export default protocolReducer;