@jetthai/cuora 1.1.344 → 1.1.346-dev-1

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.
@@ -1,255 +0,0 @@
1
- import { assign, createMachine } from 'xstate'
2
- import payment from '@jetthai/cuora/src/api/payment.js'
3
- import { max } from '../../util/index.js'
4
-
5
- export default function (request, context) {
6
- const { paymentDetail = [], promotion, selectedAmount } = context || {}
7
- return createMachine(
8
- {
9
- id: 'crypto_cash',
10
- initial: 'loading',
11
- context: {
12
- paymentDetail,
13
- promotion,
14
- selectedAmount,
15
-
16
- cryptos: [],
17
- networks: [],
18
- sourceAddress: '',
19
- options: [],
20
-
21
- selectedCrypto: null,
22
- selectedNetwork: null,
23
-
24
- currentCategory: 'crypto',
25
-
26
- calculation: null,
27
- result: null,
28
- },
29
- states: {
30
- loading: {
31
- always: [
32
- {
33
- target: 'debouncing',
34
- actions: 'setInitData',
35
- },
36
- ],
37
- },
38
- loaded: {},
39
- debouncing: {
40
- entry: 'clearCalculation',
41
- after: {
42
- 1000: [
43
- {
44
- target: 'calculating',
45
- cond: 'isCalculable',
46
- },
47
- {
48
- target: 'loaded',
49
- },
50
- ],
51
- },
52
- },
53
- calculating: {
54
- invoke: {
55
- src: 'fetchCalculation',
56
- onDone: {
57
- target: 'calculated',
58
- actions: 'setCalculation',
59
- },
60
- onError: {
61
- target: 'calculated',
62
- },
63
- },
64
- },
65
- calculated: {
66
- on: {
67
- SUBMIT: [
68
- {
69
- target: 'submitting',
70
- cond: 'isSubmittable',
71
- },
72
- ],
73
- },
74
- },
75
- submitting: {
76
- invoke: {
77
- src: 'submitDeposit',
78
- onDone: {
79
- target: 'submitted',
80
- actions: 'setResult',
81
- },
82
- onError: {
83
- target: 'loaded',
84
- },
85
- },
86
- },
87
- submitted: {},
88
- },
89
- on: {
90
- SET_PROMOTION: {
91
- target: '.debouncing',
92
- actions: 'setPromotion',
93
- internal: false,
94
- },
95
- SET_CRYPTO: {
96
- target: '.debouncing',
97
- actions: 'setCrypto',
98
- internal: false,
99
- },
100
- SET_NETWORK: {
101
- target: '.debouncing',
102
- actions: 'setNetwork',
103
- internal: false,
104
- },
105
- SET_ADDRESS: {
106
- actions: 'setAddress',
107
- internal: false,
108
- },
109
- SET_AMOUNT: {
110
- target: '.debouncing',
111
- actions: 'setAmount',
112
- internal: false,
113
- },
114
- INPUT_AMOUNT: {
115
- target: '.debouncing',
116
- actions: 'inputAmount',
117
- internal: false,
118
- },
119
- },
120
- },
121
- {
122
- actions: {
123
- setInitData: assign((context, event) => {
124
- const { paymentDetail } = context
125
- const cryptos = paymentDetail
126
- .filter((element) => element.transferType === 'deposit' && element.category === context.currentCategory)
127
- .map((crypto) => ({
128
- ...crypto,
129
- label: crypto.channel,
130
- value: crypto.method,
131
- icon: crypto.channel,
132
- }))
133
- const defaultNetworks = cryptos[0].networkName.map((value, i) => {
134
- return {
135
- label: cryptos[0].networkOptional?.[i],
136
- value,
137
- icon: value,
138
- }
139
- })
140
- return {
141
- cryptos,
142
- networks: defaultNetworks,
143
- selectedCrypto: cryptos[0],
144
- selectedNetwork: defaultNetworks[0],
145
- options: cryptos[0].optional,
146
- }
147
- }),
148
- setCrypto: assign((context, event) => {
149
- const { cryptos } = context
150
- const selectedCrypto = cryptos.find((crypto) => crypto.method === event.data.value)
151
- const networks = selectedCrypto?.networkName.map((value, i) => {
152
- return {
153
- label: selectedCrypto?.networkOptional?.[i],
154
- value,
155
- icon: value,
156
- }
157
- })
158
- return {
159
- networks,
160
- selectedCrypto: selectedCrypto,
161
- selectedNetwork: networks[0],
162
- selectedAmount: null,
163
- calculation: null,
164
- options: selectedCrypto.optional,
165
- }
166
- }),
167
- setNetwork: assign((context, event) => {
168
- return { selectedNetwork: event.data }
169
- }),
170
- setAmount: assign((context, event) => {
171
- // 現金網可自由填額度,grandTotalEnable 欄位感覺不再需要?
172
- if (context.selectedCrypto?.grandTotalEnable === 1) {
173
- return { selectedAmount: parseFloat(context.selectedAmount || 0) + parseFloat(event.data) }
174
- } else {
175
- return { selectedAmount: event.data }
176
- }
177
- }),
178
- inputAmount: assign((context, event) => {
179
- return { selectedAmount: event.data }
180
- }),
181
- setAddress: assign((context, event) => {
182
- return {
183
- sourceAddress: event.data,
184
- }
185
- }),
186
- setCalculation: assign((context, event) => {
187
- return {
188
- calculation: event.data,
189
- }
190
- }),
191
- clearCalculation: assign((context, event) => {
192
- return {
193
- calculation: null,
194
- }
195
- }),
196
- setResult: assign((context, event) => {
197
- return {
198
- result: event.data,
199
- }
200
- }),
201
- setPromotion: assign((context, event) => {
202
- const { selectedCrypto, selectedAmount } = context
203
- const promotion = event.data
204
- return {
205
- promotion,
206
- selectedAmount: max(promotion.minimumAmount, selectedCrypto.minimum, selectedAmount, 0),
207
- calculation: null,
208
- }
209
- }),
210
- },
211
- guards: {
212
- isCalculable: (context, event) => {
213
- const { selectedCrypto, selectedNetwork, selectedAmount } = context
214
- return (
215
- selectedCrypto &&
216
- selectedNetwork &&
217
- selectedAmount &&
218
- selectedCrypto.minimum <= +selectedAmount &&
219
- +selectedAmount <= selectedCrypto.maximum
220
- )
221
- },
222
- isSubmittable: (context, event) => {
223
- const { selectedCrypto, selectedNetwork, selectedAmount } = context
224
- return selectedCrypto && selectedNetwork && selectedAmount
225
- },
226
- },
227
- services: {
228
- fetchCalculation: async (context, event) => {
229
- const { selectedCrypto, selectedAmount, promotion } = context
230
- return await request.send(
231
- payment.getDepositExpectedResult({
232
- method: selectedCrypto.method,
233
- amount: selectedAmount,
234
- cryptoCurrency: selectedCrypto.channel,
235
- promotionsNo: promotion?.promotionsNo,
236
- })
237
- )
238
- },
239
- submitDeposit: async (context, event) => {
240
- const { selectedCrypto, selectedNetwork, calculation, sourceAddress, promotion } = context
241
- return await request.send(
242
- payment.submitDepositTransactionCrypto({
243
- method: selectedCrypto.method,
244
- amount: calculation.actualAmount,
245
- cryptoCurrency: selectedCrypto.channel,
246
- cryptoNetwork: selectedNetwork.value,
247
- sourceAddress: sourceAddress,
248
- promotionsNo: promotion?.promotionsNo,
249
- })
250
- )
251
- },
252
- },
253
- }
254
- )
255
- }