@jetthai/cuora 1.0.59 → 1.0.61

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,10 @@
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.61](https://github.com/jetthai/cuora-module/compare/v1.0.60...v1.0.61) (2021-12-22)
6
+
7
+ ### [1.0.60](https://github.com/jetthai/cuora-module/compare/v1.0.59...v1.0.60) (2021-12-20)
8
+
5
9
  ### [1.0.59](https://github.com/jetthai/cuora-module/compare/v1.0.58...v1.0.59) (2021-12-17)
6
10
 
7
11
  ### [1.0.58](https://github.com/jetthai/cuora-module/compare/v1.0.57...v1.0.58) (2021-12-17)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetthai/cuora",
3
- "version": "1.0.59",
3
+ "version": "1.0.61",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,168 @@
1
+ import { createMachine, assign } from 'xstate'
2
+ import api from '../../api/activity'
3
+
4
+ export default function (request) {
5
+ const { appSecret } = request.config
6
+ return createMachine(
7
+ {
8
+ id: 'diamond_shop_activity',
9
+ context: {
10
+ activityNo: null,
11
+ activityDetail: null,
12
+ awards: [],
13
+ exchangeList: [],
14
+ exchangeAward: 0,
15
+ exchangePoint: 0,
16
+ },
17
+ initial: 'idle',
18
+ states: {
19
+ idle: {
20
+ on: {
21
+ SET_ACTIVITY_NO: {
22
+ target: 'loading',
23
+ actions: 'setActivityNo',
24
+ },
25
+ },
26
+ },
27
+ loading: {
28
+ invoke: {
29
+ src: 'fetchActivityDetail',
30
+ onDone: {
31
+ target: 'loaded',
32
+ actions: ['setActivityDetail', 'setAwardList'],
33
+ },
34
+ onError: {
35
+ target: 'idle',
36
+ },
37
+ },
38
+ },
39
+ loaded: {
40
+ on: {
41
+ EXCHANGE: {
42
+ target: 'exchanging',
43
+ cond: 'isExchangeable',
44
+ },
45
+ ADD_TO_CART: {
46
+ actions: ['addProduct', 'count'],
47
+ },
48
+ CLEAR_CART: {
49
+ actions: ['clearCart', 'count'],
50
+ },
51
+ COLLECT: {
52
+ target: 'collecting',
53
+ cond: 'hasUnclaimedReward',
54
+ },
55
+ },
56
+ },
57
+ exchanging: {
58
+ invoke: {
59
+ src: 'exchange',
60
+ onDone: {
61
+ target: 'collecting',
62
+ actions: ['clearCart', 'count'],
63
+ },
64
+ onError: {
65
+ target: 'loaded',
66
+ },
67
+ },
68
+ },
69
+ collecting: {
70
+ invoke: {
71
+ src: 'collect',
72
+ onDone: {
73
+ target: 'collected',
74
+ },
75
+ onError: {
76
+ target: 'collected',
77
+ },
78
+ },
79
+ },
80
+ collected: {
81
+ after: {
82
+ 0: {
83
+ target: 'loaded',
84
+ },
85
+ },
86
+ },
87
+ },
88
+ },
89
+ {
90
+ actions: {
91
+ setActivityNo: assign((context, event) => {
92
+ return {
93
+ activityNo: event.data,
94
+ }
95
+ }),
96
+ setActivityDetail: assign((context, event) => {
97
+ return {
98
+ activityDetail: event.data,
99
+ }
100
+ }),
101
+ setAwardList: assign((context, event) => {
102
+ return {
103
+ awardList: event.data?.awards || [],
104
+ }
105
+ }),
106
+ addProduct: assign((context, event) => {
107
+ return {
108
+ exchangeList: [...context.exchangeList, event.data],
109
+ }
110
+ }),
111
+ clearCart: assign((context, event) => {
112
+ return {
113
+ exchangeList: [],
114
+ }
115
+ }),
116
+ count: assign((context, event) => {
117
+ const { exchangeList } = context
118
+ const { exchangeAward, exchangePoint } = exchangeList.reduce(
119
+ ({ exchangeAward, exchangePoint }, item) => {
120
+ return {
121
+ exchangeAward: exchangeAward + item.award,
122
+ exchangePoint: exchangePoint + item.points,
123
+ }
124
+ },
125
+ { exchangeAward: 0, exchangePoint: 0 }
126
+ )
127
+ return {
128
+ exchangeAward,
129
+ exchangePoint,
130
+ }
131
+ }),
132
+ },
133
+ guards: {
134
+ hasUnclaimedReward: (context, event) => {
135
+ return context.activityDetail?.unclaimedReward?.length > 0
136
+ },
137
+ isExchangeable: (context, event) => {
138
+ const { exchangeList } = context
139
+ return exchangeList.length > 0
140
+ },
141
+ },
142
+ services: {
143
+ fetchActivityDetail: async (context, event) => {
144
+ const { activityNo } = context
145
+ const { data } = await request.send(api.getActivityDetail({ activityNo, appSecret }))
146
+ return data
147
+ },
148
+ exchange: async (context, event) => {
149
+ const { activityNo, exchangeList } = context
150
+ return await Promise.all(
151
+ exchangeList.map((item) =>
152
+ request.send(
153
+ api.exchange({
154
+ exchangeType: 1,
155
+ activityNo,
156
+ exchangeAward: item.award,
157
+ })
158
+ )
159
+ )
160
+ )
161
+ },
162
+ collect: async (context, event) => {
163
+ return await request.send(api.collect({ appSecret }))
164
+ },
165
+ },
166
+ }
167
+ )
168
+ }
@@ -7,12 +7,25 @@ export default function (request) {
7
7
  {
8
8
  id: 'ranking_activity',
9
9
  context: {
10
+ rankActivities: null,
10
11
  activityNo: null,
11
12
  activityDetail: null,
12
13
  activityMap: {},
13
14
  },
14
- initial: 'idle',
15
+ initial: 'fetching',
15
16
  states: {
17
+ fetching: {
18
+ invoke: {
19
+ src: 'fetchActivityList',
20
+ onDone: {
21
+ target: 'checking',
22
+ actions: 'setRankActivities',
23
+ },
24
+ onError: {
25
+ target: 'idle',
26
+ },
27
+ },
28
+ },
16
29
  idle: {
17
30
  on: {
18
31
  SET_ACTIVITY_NO: {
@@ -40,6 +53,15 @@ export default function (request) {
40
53
  },
41
54
  {
42
55
  actions: {
56
+ setRankActivities: assign((context, event) => {
57
+ const rankActivities = event.data.filter((item) => item.activityType === 2 || item.activityType === 200)
58
+ // 2: ranking_activity
59
+ // 200: cl_activity
60
+ return {
61
+ rankActivities,
62
+ activityNo: rankActivities?.[0]?.activityNo,
63
+ }
64
+ }),
43
65
  setActivityNo: assign((context, event) => {
44
66
  return {
45
67
  activityNo: event.data,
@@ -65,6 +87,10 @@ export default function (request) {
65
87
  },
66
88
  },
67
89
  services: {
90
+ fetchActivityList: async (context, event) => {
91
+ const { data } = await request.send(api.getActivityList({ appSecret, include: 'cl' }))
92
+ return data
93
+ },
68
94
  fetchActivityDetail: async (context, event) => {
69
95
  const { activityNo } = context
70
96
  const { data } = await request.send(api.getActivityDetail({ activityNo, appSecret }))
@@ -56,6 +56,7 @@ export default function (request) {
56
56
  id: 'inbox',
57
57
  initial: 'loading',
58
58
  context: {
59
+ isRewardListEnable: true,
59
60
  tabs: [],
60
61
  currentTab: null,
61
62
  inbox: {},
@@ -222,8 +223,8 @@ export default function (request) {
222
223
  return tabs
223
224
  },
224
225
  fetchMessages: async (context, event) => {
225
- const { currentTab, inbox } = JSON.parse(JSON.stringify(context))
226
- if (currentTab === 'REWARD') {
226
+ const { isRewardListEnable, currentTab, inbox } = JSON.parse(JSON.stringify(context))
227
+ if (isRewardListEnable && currentTab === 'REWARD') {
227
228
  const { data } = await request.send(activity.getRewardList({ count: 30, appSecret }))
228
229
  inbox[currentTab] = { data, page: 0, totalPage: 1 }
229
230
  } else {