1
1
import { Component , ElementRef , EventEmitter , Input , OnInit , Output } from '@angular/core' ;
2
+ import { Subscription } from 'rxjs' ;
3
+ import { IntentService } from 'src/app/chatbot-design-studio/services/intent.service' ;
4
+ import { TYPE_UPDATE_ACTION } from 'src/app/chatbot-design-studio/utils' ;
2
5
import { ActionSendWhatsapp } from 'src/app/models/action-model' ;
3
6
import { Intent } from 'src/app/models/intent-model' ;
4
7
import { DashboardService } from 'src/app/services/dashboard.service' ;
@@ -17,7 +20,19 @@ export class CdsActionSendWhatsappComponent implements OnInit {
17
20
@Input ( ) intentSelected : Intent ;
18
21
@Input ( ) previewMode : boolean = true ;
19
22
@Output ( ) updateAndSaveAction = new EventEmitter ( ) ;
20
-
23
+ @Output ( ) onConnectorChange = new EventEmitter < { type : 'create' | 'delete' , fromId : string , toId : string } > ( )
24
+
25
+ listOfIntents : Array < { name : string , value : string , icon ?:string } > ;
26
+
27
+ // Connectors
28
+ idIntentSelected : string ;
29
+ idConnectorTrue : string ;
30
+ idConnectorFalse : string ;
31
+ isConnectedTrue : boolean = false ;
32
+ isConnectedFalse : boolean = false ;
33
+ connector : any ;
34
+ private subscriptionChangedConnector : Subscription ;
35
+
21
36
project_id : string ;
22
37
23
38
templates_list = [ ] ;
@@ -32,18 +47,97 @@ export class CdsActionSendWhatsappComponent implements OnInit {
32
47
33
48
constructor (
34
49
private dashboardService : DashboardService ,
50
+ private intentService : IntentService ,
35
51
private whatsapp : WhatsappService ,
36
52
public el : ElementRef
37
53
) { }
38
54
39
55
ngOnInit ( ) : void {
56
+ this . logger . debug ( "[ACTION-SEND WHATSAPP] action detail: " , this . action ) ;
57
+ this . subscriptionChangedConnector = this . intentService . isChangedConnector$ . subscribe ( ( connector : any ) => {
58
+ this . logger . debug ( '[ACTION-SEND WHATSAPP] isChangedConnector -->' , connector ) ;
59
+ this . connector = connector ;
60
+ this . updateConnector ( ) ;
61
+ } ) ;
62
+ this . initialize ( ) ;
63
+ }
64
+
65
+ /** */
66
+ ngOnDestroy ( ) {
67
+ if ( this . subscriptionChangedConnector ) {
68
+ this . subscriptionChangedConnector . unsubscribe ( ) ;
69
+ }
70
+ }
71
+
72
+ private checkConnectionStatus ( ) {
73
+ if ( this . action . trueIntent ) {
74
+ this . isConnectedTrue = true ;
75
+ } else {
76
+ this . isConnectedTrue = false ;
77
+ }
78
+ if ( this . action . falseIntent ) {
79
+ this . isConnectedFalse = true ;
80
+ } else {
81
+ this . isConnectedFalse = false ;
82
+ }
83
+ }
84
+
85
+ initializeConnector ( ) {
86
+ this . idIntentSelected = this . intentSelected . intent_id ;
87
+ this . idConnectorTrue = this . idIntentSelected + '/' + this . action . _tdActionId + '/true' ;
88
+ this . idConnectorFalse = this . idIntentSelected + '/' + this . action . _tdActionId + '/false' ;
89
+ this . listOfIntents = this . intentService . getListOfIntents ( ) ;
90
+ this . checkConnectionStatus ( ) ;
91
+ }
92
+
93
+ private updateConnector ( ) {
94
+ try {
95
+ const array = this . connector . fromId . split ( "/" ) ;
96
+ const idAction = array [ 1 ] ;
97
+ if ( idAction === this . action . _tdActionId ) {
98
+ if ( this . connector . deleted ) {
99
+ if ( array [ array . length - 1 ] === 'true' ) {
100
+ this . action . trueIntent = null
101
+ this . isConnectedTrue = false
102
+ }
103
+ if ( array [ array . length - 1 ] === 'false' ) {
104
+ this . action . falseIntent = null
105
+ this . isConnectedFalse = false ;
106
+ }
107
+ if ( this . connector . save ) this . updateAndSaveAction . emit ( { type : TYPE_UPDATE_ACTION . CONNECTOR , element : this . connector } ) ;
108
+ } else {
109
+ this . logger . debug ( '[ACTION-SEND WHATSAPP] updateConnector' , this . connector . toId , this . connector . fromId , this . action , array [ array . length - 1 ] ) ;
110
+ if ( array [ array . length - 1 ] === 'true' ) {
111
+ this . isConnectedTrue = true ;
112
+ this . action . trueIntent = '#' + this . connector . toId ;
113
+ if ( this . connector . save ) this . updateAndSaveAction . emit ( { type : TYPE_UPDATE_ACTION . CONNECTOR , element : this . connector } ) ;
114
+ }
115
+ if ( array [ array . length - 1 ] === 'false' ) {
116
+ this . isConnectedFalse = true ;
117
+ if ( this . action . falseIntent !== '#' + this . connector . toId ) {
118
+ this . action . falseIntent = '#' + this . connector . toId ;
119
+ if ( this . connector . save ) this . updateAndSaveAction . emit ( { type : TYPE_UPDATE_ACTION . CONNECTOR , element : this . connector } ) ;
120
+ }
121
+ }
122
+ }
123
+
124
+ }
125
+ } catch ( error ) {
126
+ this . logger . error ( '[ACTION-SEND WHATSAPP] updateConnector error: ' , error ) ;
127
+ }
128
+ }
129
+
130
+ private initialize ( ) {
40
131
this . project_id = this . dashboardService . projectID
41
132
this . action . payload . id_project = this . project_id
42
133
if ( this . previewMode == false ) {
43
134
this . logger . log ( "Whatsapp static project_id: " , this . project_id ) ;
44
135
this . showLoader = true ;
45
136
this . getTemplates ( ) ;
46
137
}
138
+ if ( this . intentSelected ) {
139
+ this . initializeConnector ( ) ;
140
+ }
47
141
}
48
142
49
143
getTemplates ( ) {
@@ -62,9 +156,9 @@ export class CdsActionSendWhatsappComponent implements OnInit {
62
156
63
157
} , error : ( error ) => {
64
158
this . showLoader = false ;
65
- this . logger . log ( "[SEND WHATSAPP] error get templates: " , error ) ;
159
+ this . logger . log ( "[ACTION- SEND WHATSAPP] error get templates: " , error ) ;
66
160
} , complete : ( ) => {
67
- this . logger . log ( "[SEND WHATSAPP] get templates completed: " ) ;
161
+ this . logger . log ( "[ACTION- SEND WHATSAPP] get templates completed: " ) ;
68
162
if ( this . action . templateName ) {
69
163
this . selected_template = this . templates_list . find ( t => t . name === this . action . templateName ) ;
70
164
}
@@ -73,7 +167,7 @@ export class CdsActionSendWhatsappComponent implements OnInit {
73
167
}
74
168
75
169
onChangeSelect ( event ) {
76
- this . logger . debug ( "[ACTION SEND WHATSAPP] onChangeSelect event" , event ) ;
170
+ this . logger . debug ( "[ACTION- SEND WHATSAPP] onChangeSelect event" , event ) ;
77
171
this . selected_template = event ;
78
172
this . action . templateName = this . selected_template . name ;
79
173
this . action . payload . template . name = this . selected_template . name ;
@@ -100,14 +194,43 @@ export class CdsActionSendWhatsappComponent implements OnInit {
100
194
this . updateAndSaveAction . emit ( ) ;
101
195
}
102
196
103
- this . logger . debug ( "[ACTION WHATSAPP] Action updated " , this . action . payload ) ;
197
+ this . logger . debug ( "[ACTION-SEND WHATSAPP] Action updated " , this . action . payload ) ;
198
+ }
199
+ }
200
+
201
+ onChangeBlockSelect ( event :{ name : string , value : string } , type : 'trueIntent' | 'falseIntent' ) {
202
+ if ( event ) {
203
+ this . action [ type ] = event . value
204
+
205
+ switch ( type ) {
206
+ case 'trueIntent' :
207
+ this . onConnectorChange . emit ( { type : 'create' , fromId : this . idConnectorTrue , toId : this . action . trueIntent } )
208
+ break ;
209
+ case 'falseIntent' :
210
+ this . onConnectorChange . emit ( { type : 'create' , fromId : this . idConnectorFalse , toId : this . action . falseIntent } )
211
+ break ;
212
+ }
213
+ this . updateAndSaveAction . emit ( { type : TYPE_UPDATE_ACTION . ACTION , element : this . action } ) ;
214
+ }
215
+ }
216
+
217
+ onResetBlockSelect ( event :{ name : string , value : string } , type : 'trueIntent' | 'falseIntent' ) {
218
+ switch ( type ) {
219
+ case 'trueIntent' :
220
+ this . onConnectorChange . emit ( { type : 'delete' , fromId : this . idConnectorTrue , toId : this . action . trueIntent } )
221
+ break ;
222
+ case 'falseIntent' :
223
+ this . onConnectorChange . emit ( { type : 'delete' , fromId : this . idConnectorFalse , toId : this . action . falseIntent } )
224
+ break ;
104
225
}
226
+ this . action [ type ] = null ;
227
+ this . updateAndSaveAction . emit ( { type : TYPE_UPDATE_ACTION . ACTION , element : this . action } ) ;
105
228
}
106
229
107
230
onReceiverEmitted ( event , index ) {
108
231
// update receiver
109
232
this . action . payload . receiver_list [ index ] = event ;
110
- this . logger . log ( "[ACTION WHATSAPP] Action updated " , this . action . payload ) ;
233
+ this . logger . log ( "[ACTION-SEND WHATSAPP] Action updated " , this . action . payload ) ;
111
234
this . updateAndSaveAction . emit ( )
112
235
}
113
236
0 commit comments