@jetthai/cuora 1.0.123 → 1.0.124

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/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.0.124](https://github.com/jetthai/cuora-module/compare/v1.0.123...v1.0.124) (2022-05-26)
6
+
5
7
  ### [1.0.123](https://github.com/jetthai/cuora-module/compare/v1.0.122...v1.0.123) (2022-05-23)
6
8
 
7
9
  ### [1.0.114-1](https://github.com/jetthai/cuora-module/compare/v1.0.117...v1.0.114-1) (2022-05-19)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.123",
3
+ "version": "1.0.124",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,195 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import payment from '../../api/payment'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'qrcode',
8
+ initial: 'loading',
9
+ context: {
10
+ qrcodes: null,
11
+ options: null,
12
+
13
+ selectedQrcode: null,
14
+ selectedAmount: null,
15
+
16
+ calculation: null,
17
+
18
+ paymentDetails: [],
19
+ currentMethodType: 'qrcode',
20
+ currentMethod: null,
21
+
22
+ result: null,
23
+ },
24
+ states: {
25
+ loading: {
26
+ invoke: {
27
+ src: 'fetchInitData',
28
+ onDone: {
29
+ target: 'loaded',
30
+ actions: 'setInitData',
31
+ },
32
+ onError: {
33
+ target: 'fail',
34
+ },
35
+ },
36
+ },
37
+ loaded: {
38
+ entry: 'reset',
39
+ on: {
40
+ SELECT_QRCODE: {
41
+ actions: 'setQrcode',
42
+ },
43
+ SELECT_AMOUNT: {
44
+ target: 'calculable',
45
+ actions: 'addAmount',
46
+ },
47
+ },
48
+ },
49
+ fail: {},
50
+ calculable: {
51
+ entry: 'clearCalculation',
52
+ on: {
53
+ SELECT_AMOUNT: {
54
+ actions: 'addAmount',
55
+ },
56
+ CALCULATE: {
57
+ target: 'calculating',
58
+ },
59
+ BACK: {
60
+ target: 'loaded',
61
+ },
62
+ },
63
+ },
64
+ calculating: {
65
+ invoke: {
66
+ src: 'fetchCalculation',
67
+ onDone: {
68
+ target: 'calculated',
69
+ actions: 'setCalculation',
70
+ },
71
+ onError: {
72
+ target: 'calculable',
73
+ },
74
+ },
75
+ },
76
+ calculated: {
77
+ on: {
78
+ SELECT_AMOUNT: {
79
+ target: 'calculable',
80
+ actions: 'addAmount',
81
+ },
82
+ CONFIRM: {
83
+ target: 'submitting',
84
+ },
85
+ BACK: {
86
+ target: 'loaded',
87
+ },
88
+ CLEAR_CALCULATION: {
89
+ target: 'calculable',
90
+ actions: 'clearCalculation',
91
+ },
92
+ },
93
+ },
94
+ submitting: {
95
+ invoke: {
96
+ src: 'submitDeposit',
97
+ onDone: {
98
+ target: 'submitted',
99
+ actions: 'setResult',
100
+ },
101
+ onError: {
102
+ target: 'calculated',
103
+ },
104
+ },
105
+ },
106
+ submitted: {
107
+ on: {
108
+ SELECT_QRCODE: {
109
+ target: 'loaded',
110
+ actions: 'setQrcode',
111
+ },
112
+ },
113
+ },
114
+ },
115
+ },
116
+ {
117
+ actions: {
118
+ reset: assign((context, event) => {
119
+ return {
120
+ selectedAmount: null,
121
+ calculation: null,
122
+ }
123
+ }),
124
+ setInitData: assign((context, event) => {
125
+ const paymentDetails = event.data
126
+ const qrcodes = paymentDetails.filter((element) => element.methodType === context.currentMethodType)
127
+
128
+ return {
129
+ paymentDetails,
130
+ qrcodes,
131
+ selectedQrcode: qrcodes[0],
132
+ currentMethod: qrcodes[0].method,
133
+ options: qrcodes[0].optional,
134
+ }
135
+ }),
136
+ setQrcode: assign((context, event) => {
137
+ return {
138
+ selectedQrcode: event.data,
139
+ currentMethod: event.data.method,
140
+ options: event.data.optional,
141
+ }
142
+ }),
143
+ addAmount: assign((context, event) => {
144
+ return { selectedAmount: (context.selectedAmount || 0) + event.data }
145
+ }),
146
+ setCalculation: assign((context, event) => {
147
+ return {
148
+ calculation: event.data,
149
+ }
150
+ }),
151
+ clearCalculation: assign((context, event) => {
152
+ return {
153
+ calculation: null,
154
+ }
155
+ }),
156
+ setResult: assign((context, event) => {
157
+ return {
158
+ result: event.data,
159
+ }
160
+ }),
161
+ },
162
+ guards: {
163
+ isSubmittable: (context, event) => {},
164
+ },
165
+ services: {
166
+ fetchInitData: async (context, event) => {
167
+ return await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
168
+ },
169
+ fetchBanksList: async (context, event) => {
170
+ const { paymentDetails, currentMethodType } = context
171
+ const method = paymentDetails.find((item) => item.methodType === currentMethodType)?.method
172
+ return await request.send(payment.getBankList(method))
173
+ },
174
+ fetchCalculation: async (context, event) => {
175
+ const { currentMethod, selectedAmount } = context
176
+ return await request.send(
177
+ payment.getDepositExpectedResult({
178
+ method: currentMethod,
179
+ amount: selectedAmount,
180
+ })
181
+ )
182
+ },
183
+ submitDeposit: async (context, event) => {
184
+ const { currentMethod, selectedAmount } = context
185
+ return await request.send(
186
+ payment.submitDepositTransactionQrcode({
187
+ method: currentMethod,
188
+ amount: selectedAmount,
189
+ })
190
+ )
191
+ },
192
+ },
193
+ }
194
+ )
195
+ }