Skip to content

Commit c579098

Browse files
committed
refactor: formMachine 로직 수정
필요없는 이벤트와 엑션을 제거했습니다. issue: #19
1 parent f60f511 commit c579098

File tree

2 files changed

+83
-192
lines changed

2 files changed

+83
-192
lines changed

src/features/order/models/form.machine.ts

Lines changed: 74 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ export const formMachine = setup({
1616
holdings?: number;
1717
},
1818
events: {} as
19-
| { type: 'SWITCH_TRADE_TYPE'; tradeType: '매수' | '매도' }
20-
| { type: 'SWITCH_ORDER_TYPE'; orderType: '지정가' | '시장가' }
21-
| { type: 'CHANGE_SELL_PRICE'; price: number }
22-
| { type: 'CHANGE_SELL_QUANTITY'; quantity: number }
23-
| { type: 'CHANGE_BUY_PRICE'; price: number }
24-
| { type: 'CHANGE_BUY_QUANTITY'; quantity: number }
19+
| { type: 'SWITCH_TRADE_TYPE' }
20+
| { type: 'SWITCH_ORDER_TYPE' }
21+
| { type: 'CHANGE_PRICE'; price: number }
22+
| { type: 'CHANGE_QUANTITY'; quantity: number }
2523
| { type: 'INCREMENT_PRICE' }
2624
| { type: 'DECREMENT_PRICE' }
2725
| { type: 'INCREMENT_QUANTITY' }
@@ -61,9 +59,9 @@ export const formMachine = setup({
6159
errorMessage: string;
6260
};
6361
}) => {
64-
if (input.errorMessage) throw new Error(input.errorMessage);
62+
if (input.errorMessage.length > 0) throw new Error(input.errorMessage);
6563
return orderApi.order({
66-
ticker: 'TRUMP',
64+
ticker: input.ticker,
6765
side: input.side,
6866
orderType: input.orderType,
6967
orderSize: input.orderSize,
@@ -74,119 +72,93 @@ export const formMachine = setup({
7472
},
7573
actions: {
7674
switchTradeType: assign({
77-
tradeType: ({ event }) => {
78-
assertEvent(event, 'SWITCH_TRADE_TYPE');
79-
return event.tradeType;
75+
tradeType: ({ context }) => {
76+
return context.tradeType === '매수' ? '매도' : '매수';
8077
},
8178
}),
8279
switchOrderType: assign({
83-
orderType: ({ event }) => {
84-
assertEvent(event, 'SWITCH_ORDER_TYPE');
85-
return event.orderType;
80+
orderType: ({ context }) => {
81+
return context.orderType === '지정가' ? '시장가' : '지정가';
8682
},
87-
price: ({ event, context }) => {
88-
assertEvent(event, 'SWITCH_ORDER_TYPE');
89-
83+
price: ({ context }) => {
9084
if (context.tradeType === '매도') return undefined;
9185

9286
return context.price;
9387
},
94-
quantity: ({ event, context }) => {
95-
assertEvent(event, 'SWITCH_ORDER_TYPE');
96-
88+
quantity: ({ context }) => {
9789
if (context.tradeType === '매수') return undefined;
9890

9991
return context.quantity;
10092
},
10193
}),
102-
changeBuyPrice: assign({
94+
changePrice: assign({
10395
price: ({ event }) => {
104-
assertEvent(event, 'CHANGE_BUY_PRICE');
96+
assertEvent(event, 'CHANGE_PRICE');
10597
return event.price;
10698
},
10799

108100
message: ({ event, context }) => {
109-
assertEvent(event, 'CHANGE_BUY_PRICE');
101+
assertEvent(event, 'CHANGE_PRICE');
110102

111103
const quantity = context.quantity || 0;
104+
const price = event.price;
112105

113-
if (!context.deposit) return '예수금이 없습니다.';
114-
115-
if (event.price <= 0) return '구매가격이 없습니다.';
106+
if (context.tradeType === '매수') {
107+
if (!context.deposit) return '예수금이 없습니다.';
116108

117-
if (context.orderType === '시장가') return '';
118-
119-
if (quantity <= 0) return '구매수량이 없습니다.';
120-
121-
if (event.price * quantity > context.deposit)
122-
return '구매가격 * 수량이 예수금보다 작아야 합니다.';
123-
124-
return '';
125-
},
126-
}),
127-
changeBuyQuantity: assign({
128-
quantity: ({ event }) => {
129-
assertEvent(event, 'CHANGE_BUY_QUANTITY');
130-
return event.quantity;
131-
},
109+
if (price <= 0) return '구매가격이 없습니다.';
132110

133-
message: ({ event, context }) => {
134-
assertEvent(event, 'CHANGE_BUY_QUANTITY');
111+
if (context.orderType === '시장가') return '';
135112

136-
const price = context.price || 0;
113+
if (quantity <= 0) return '구매수량이 없습니다.';
137114

138-
if (!context.deposit) return '예수금이 없습니다.';
115+
if (price * quantity > context.deposit)
116+
return '구매가격 * 수량이 예수금보다 작아야 합니다.';
117+
} else {
118+
if (!context.holdings) return '보유하신 수량이 없습니다.';
139119

140-
if (event.quantity <= 0) return '구매수량이 없습니다.';
120+
if (context.holdings < quantity)
121+
return '보유하신 수량보다 많은 수량은 매도할 수 없습니다.';
141122

142-
if (price <= 0) return '구매가격이 없습니다.';
123+
if (context.orderType === '시장가') return '';
143124

144-
if (price * event.quantity > context.deposit)
145-
return '구매가격 * 수량이 예수금보다 작아야 합니다.';
125+
if (price <= 0) return '0이하의 가격에는 매도할 수 없습니다.';
126+
}
146127

147128
return '';
148129
},
149130
}),
150-
changeSellQuantity: assign({
131+
changeQuantity: assign({
151132
quantity: ({ event }) => {
152-
assertEvent(event, 'CHANGE_SELL_QUANTITY');
133+
assertEvent(event, 'CHANGE_QUANTITY');
153134
return event.quantity;
154135
},
155136

156137
message: ({ event, context }) => {
157-
assertEvent(event, 'CHANGE_SELL_QUANTITY');
138+
assertEvent(event, 'CHANGE_QUANTITY');
158139

140+
const quantity = event.quantity;
159141
const price = context.price || 0;
160142

161-
if (!context.holdings) return '보유하신 수량이 없습니다.';
162-
163-
if (context.holdings < event.quantity)
164-
return '보유하신 수량보다 많은 수량은 매도할 수 없습니다.';
165-
166-
if (context.orderType === '시장가') return '';
143+
if (context.tradeType === '매수') {
144+
if (!context.deposit) return '예수금이 없습니다.';
167145

168-
if (price <= 0) return '0이하의 가격에는 매도할 수 없습니다.';
169-
170-
return '';
171-
},
172-
}),
173-
changeSellPrice: assign({
174-
price: ({ event }) => {
175-
assertEvent(event, 'CHANGE_SELL_PRICE');
176-
return event.price;
177-
},
146+
if (quantity <= 0) return '구매수량이 없습니다.';
178147

179-
message: ({ event, context }) => {
180-
assertEvent(event, 'CHANGE_SELL_PRICE');
148+
if (price <= 0) return '구매가격이 없습니다.';
181149

182-
const quantity = context.quantity || 0;
150+
if (price * quantity > context.deposit)
151+
return '구매가격 * 수량이 예수금보다 작아야 합니다.';
152+
} else {
153+
if (!context.holdings) return '보유하신 수량이 없습니다.';
183154

184-
if (!context.holdings) return '보유하신 수량이 없습니다.';
155+
if (context.holdings < quantity)
156+
return '보유하신 수량보다 많은 수량은 매도할 수 없습니다.';
185157

186-
if (context.holdings < quantity)
187-
return '보유하신 수량보다 많은 수량은 매도할 수 없습니다.';
158+
if (context.orderType === '시장가') return '';
188159

189-
if (event.price <= 0) return '0이하의 가격에는 매도할 수 없습니다.';
160+
if (price <= 0) return '0이하의 가격에는 매도할 수 없습니다.';
161+
}
190162

191163
return '';
192164
},
@@ -236,7 +208,7 @@ export const formMachine = setup({
236208
}).createMachine({
237209
/** @xstate-layout N4IgpgJg5mDOIC5QBcBOBDCYBiB7VAtgLLoDGAFgJYB2YAdAKIEAOyAngAQBCArp3oQDEAZQDqASQAqAYQASAfUkAlAIIARBooCaABQYBtAAwBdRKGa5YlZJVzUzIAB6IAzABYArHTcAOAJwePh5+fgDsAIzhAGw+ADQgbK4+hnR+hm5+4cF+AEyZOVEAvoXxaJg4+MRkVLSMLOzcfBwCBCISMgoA8koaStp6RqZIIBZWNnYOzgihPnSGhoEuHoahLoYuOaHxiQibXuGGPgehUVk+oTnhxaUYWC0kFDT0TKycvPyVgnIqAHIA4pouABVLTyHRKcTSAwmByjay2ezDKbuLy+AJBEIRaJxBKISKrOYXGYBAJ5GbXEBlO6VB41Z71N5NFpfWS-AHyYGggCKQN+kikWkGsMs8ImSNcnm8-kC2SxMW2eJWoTooT8yXCPjcnkMOS1FKpFUItKedVeHGEYAANpbmp8xFI5IpVBp+tChuYReNEaApj4XMrljMPLqQwdwgrdv6VVFQh41m5wqEk1Edfrboaqo9ai8GhbrbahPaOvJur1XULhnCvZNFTE6AcdcswkFdRGDn4XHR3NFwm5Qm5Nm5DFcSpT0-dqiac5w8zbmd9-pphAwADIrsEQqEVj1jBE1hDhHL+OgeGYHTxHCJRHJtw7hOg+KJDqLLFO6qIuNPlCdZ+lm2cFq0C7ssua7yDyfICtuIyenu4oHkefgnme6SBIm0Q3riB6Jsq4QuD4frXg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7ZOz4RCDEAiYkxI5TxKRTsJ18L4LWHhd68S1IRxFv4tJ8UnFJVOLMGIRElidP0X4aSbkHLyVIssTwY9PE1K+tQmqKTALpIVniU8O8YhBxfAmTIbhenKhiLqLU7sS7FGKEAA */
238210
id: 'tradeFormMachine',
239-
initial: 'Empty Buy Form',
211+
initial: 'Empty Form',
240212

241213
context: (params) => {
242214
const ticker =
@@ -252,23 +224,24 @@ export const formMachine = setup({
252224
};
253225
},
254226
states: {
255-
'Empty Buy Form': {
227+
'Empty Form': {
256228
on: {
257229
SWITCH_TRADE_TYPE: {
258-
target: 'Empty Sell Form',
230+
target: 'Empty Form',
259231
actions: 'switchTradeType',
232+
reenter: true,
260233
},
261234
SWITCH_ORDER_TYPE: {
262-
target: 'Empty Buy Form',
235+
target: 'Empty Form',
263236
actions: 'switchOrderType',
264237
},
265-
CHANGE_BUY_PRICE: {
266-
target: 'Changed Buy Form',
267-
actions: 'changeBuyPrice',
238+
CHANGE_PRICE: {
239+
target: 'Changed Form',
240+
actions: 'changePrice',
268241
},
269-
CHANGE_BUY_QUANTITY: {
270-
target: 'Changed Buy Form',
271-
actions: 'changeBuyQuantity',
242+
CHANGE_QUANTITY: {
243+
target: 'Changed Form',
244+
actions: 'changeQuantity',
272245
},
273246
},
274247
invoke: {
@@ -287,112 +260,38 @@ export const formMachine = setup({
287260
},
288261
},
289262
},
290-
'Empty Sell Form': {
291-
on: {
292-
SWITCH_TRADE_TYPE: {
293-
target: 'Empty Buy Form',
294-
actions: 'switchTradeType',
295-
},
296-
SWITCH_ORDER_TYPE: {
297-
target: 'Empty Sell Form',
298-
actions: 'switchOrderType',
299-
},
300-
CHANGE_SELL_PRICE: {
301-
target: 'Changed Sell Form',
302-
actions: 'changeSellPrice',
303-
},
304-
CHANGE_SELL_QUANTITY: {
305-
target: 'Changed Sell Form',
306-
actions: 'changeSellQuantity',
307-
},
308-
},
309-
invoke: {
310-
src: 'loadDepositAndHoldings',
311-
input: ({ context }) => ({ ticker: context.ticker }),
312-
onDone: {
313-
actions: assign({
314-
deposit: ({ event }) => event.output.deposit,
315-
holdings: ({ event }) => event.output.holdings,
316-
}),
317-
},
318-
onError: {
319-
actions: assign({
320-
message: '사용자의 계좌정보를 받아오는데 실패했습니다.',
321-
}),
322-
},
323-
},
324-
},
325-
'Changed Buy Form': {
326-
on: {
327-
SWITCH_TRADE_TYPE: {
328-
target: 'Empty Sell Form',
329-
actions: ['resetForm', 'switchTradeType'],
330-
},
331-
SWITCH_ORDER_TYPE: {
332-
target: 'Changed Buy Form',
333-
actions: 'switchOrderType',
334-
},
335-
CHANGE_BUY_PRICE: {
336-
target: 'Changed Buy Form',
337-
actions: 'changeBuyPrice',
338-
},
339-
CHANGE_BUY_QUANTITY: {
340-
target: 'Changed Buy Form',
341-
actions: 'changeBuyQuantity',
342-
},
343-
INCREMENT_PRICE: {
344-
target: 'Changed Buy Form',
345-
actions: 'incrementPrice',
346-
},
347-
DECREMENT_PRICE: {
348-
target: 'Changed Buy Form',
349-
actions: 'decrementPrice',
350-
},
351-
INCREMENT_QUANTITY: {
352-
target: 'Changed Buy Form',
353-
actions: 'incrementQuantity',
354-
},
355-
DECREMENT_QUANTITY: {
356-
target: 'Changed Buy Form',
357-
actions: 'decrementQuantity',
358-
},
359-
SUBMIT_FORM: {
360-
target: 'Submitting Form',
361-
},
362-
},
363-
},
364-
'Changed Sell Form': {
263+
'Changed Form': {
365264
on: {
366265
SWITCH_TRADE_TYPE: {
367-
target: 'Empty Buy Form',
266+
target: 'Empty Form',
368267
actions: ['resetForm', 'switchTradeType'],
369268
},
370269
SWITCH_ORDER_TYPE: {
371-
target: 'Changed Sell Form',
270+
target: 'Changed Form',
372271
actions: 'switchOrderType',
373272
},
374-
CHANGE_SELL_PRICE: {
375-
target: 'Changed Sell Form',
376-
actions: 'changeSellPrice',
273+
CHANGE_PRICE: {
274+
target: 'Changed Form',
275+
actions: 'changePrice',
377276
},
378-
CHANGE_SELL_QUANTITY: {
379-
target: 'Changed Sell Form',
380-
actions: 'changeSellQuantity',
277+
CHANGE_QUANTITY: {
278+
target: 'Changed Form',
279+
actions: 'changeQuantity',
381280
},
382281
INCREMENT_PRICE: {
383-
target: 'Changed Sell Form',
282+
target: 'Changed Form',
384283
actions: 'incrementPrice',
385284
},
386285
DECREMENT_PRICE: {
387-
target: 'Changed Sell Form',
286+
target: 'Changed Form',
388287
actions: 'decrementPrice',
389288
},
390289
INCREMENT_QUANTITY: {
391-
target: 'Changed Sell Form',
290+
target: 'Changed Form',
392291
actions: 'incrementQuantity',
393292
},
394293
DECREMENT_QUANTITY: {
395-
target: 'Changed Sell Form',
294+
target: 'Changed Form',
396295
actions: 'decrementQuantity',
397296
},
398297
SUBMIT_FORM: {
@@ -404,7 +303,7 @@ export const formMachine = setup({
404303
invoke: {
405304
src: 'submitForm',
406305
input: ({ context }) => ({
407-
ticker: '',
306+
ticker: context.ticker,
408307
side: context.tradeType === '매수' ? 'bid' : 'ask',
409308
orderType: context.orderType === '지정가' ? 'limit' : 'market',
410309
orderSize: context.quantity,
@@ -427,15 +326,15 @@ export const formMachine = setup({
427326
'Showing Success Message': {
428327
after: {
429328
100: {
430-
target: 'Empty Buy Form',
329+
target: 'Empty Form',
431330
actions: ['resetForm'],
432331
},
433332
},
434333
},
435334
'Showing Error Message': {
436335
after: {
437336
100: {
438-
target: 'Empty Buy Form',
337+
target: 'Empty Form',
439338
actions: ['resetForm'],
440339
},
441340
},

0 commit comments

Comments
 (0)