@@ -6,18 +6,13 @@ export const chatMachine = setup({
66 types : {
77 context : { } as {
88 question : Message ;
9- answer : Message ;
109 messageList : MessageObj [ ] ;
11- state : 'idle' | 'processing' | 'complete' ;
10+ state : 'idle' | 'processing' ;
1211 } ,
1312 events : { } as
1413 | {
1514 type : 'SUBMIT_EVENT' ;
1615 }
17- | {
18- type : 'RECEIVE_ANSWER' ;
19- answer : string ;
20- }
2116 | {
2217 type : 'TYPING_QUESTION' ;
2318 question : string ;
@@ -38,9 +33,6 @@ export const chatMachine = setup({
3833 switchStateToProcessing : assign ( {
3934 state : 'processing' ,
4035 } ) ,
41- switchStateToComplete : assign ( {
42- state : 'complete' ,
43- } ) ,
4436 assignQuestion : assign ( {
4537 question : ( { event } ) => {
4638 assertEvent ( event , 'TYPING_QUESTION' ) ;
@@ -63,16 +55,21 @@ export const chatMachine = setup({
6355 question : '' ,
6456 } ) ,
6557 assignErrorMessage : assign ( {
66- answer : ( { event } ) => {
67- return '답변을 받아오는데 실패했습니다.' ;
58+ messageList : ( { event, context } ) => {
59+ return [
60+ ...context . messageList ,
61+ {
62+ message : '답변을 받아오는데 실패했습니다.' ,
63+ isMine : false ,
64+ } ,
65+ ] ;
6866 } ,
6967 } ) ,
7068 } ,
7169} ) . createMachine ( {
7270 id : 'chatMachine' ,
7371 context : {
7472 question : '' ,
75- answer : '' ,
7673 messageList : [ ] ,
7774 state : 'idle' ,
7875 } ,
@@ -102,9 +99,8 @@ export const chatMachine = setup({
10299 src : 'submitQuestion' ,
103100 input : ( { context } ) => ( { question : context . question } ) ,
104101 onDone : {
105- target : 'RECEIVED ANSWER ' ,
102+ target : 'EMPTY QUESTION FIELD ' ,
106103 actions : [
107- 'switchStateToComplete' ,
108104 'resetQuestion' ,
109105 assign ( {
110106 messageList : ( { event, context } ) => {
@@ -114,19 +110,14 @@ export const chatMachine = setup({
114110 ] ;
115111 } ,
116112 } ) ,
113+ 'switchStateToIdle' ,
117114 ] ,
118115 } ,
119116 onError : {
120- target : 'RECEIVED ANSWER ' ,
121- actions : [ 'assignErrorMessage' ] ,
117+ target : 'EMPTY QUESTION FIELD ' ,
118+ actions : [ 'assignErrorMessage' , 'resetQuestion' , 'switchStateToIdle' ] ,
122119 } ,
123120 } ,
124121 } ,
125- 'RECEIVED ANSWER' : {
126- always : {
127- target : 'EMPTY QUESTION FIELD' ,
128- actions : [ 'switchStateToIdle' ] ,
129- } ,
130- } ,
131122 } ,
132123} ) ;
0 commit comments