@@ -3,17 +3,11 @@ import { __ } from '../../../Utils/i18nwrap'
33import bitsFetch from '../../../Utils/bitsFetch'
44
55export const handleInput = ( e , lineConf , setLineConf ) => {
6- const newConf = { ...lineConf }
7- const { name } = e . target
8- if ( e . target . value !== '' ) {
9- newConf [ name ] = e . target . value
10- } else {
11- delete newConf [ name ]
12- }
13- setLineConf ( { ...newConf } )
6+ const { name, value } = e . target
7+ setLineConf ( prev => ( { ...prev , ...( value ? { [ name ] : value } : { [ name ] : undefined } ) } ) )
148}
159
16- export const handleAuthorize = (
10+ export const handleAuthorize = async (
1711 confTmp ,
1812 setConf ,
1913 setError ,
@@ -22,199 +16,166 @@ export const handleAuthorize = (
2216 setSnackbar
2317) => {
2418 if ( ! confTmp . accessToken ) {
25- setError ( {
26- accessToken : ! confTmp . accessToken ? __ ( "Access Token can't be empty" , 'bit-integrations' ) : ''
27- } )
19+ setError ( { accessToken : __ ( "Access Token can't be empty" , 'bit-integrations' ) } )
2820 return
2921 }
3022
3123 setError ( { } )
3224 setIsLoading ( true )
3325
34- const tokenRequestParams = { accessToken : confTmp . accessToken }
35-
36- bitsFetch ( tokenRequestParams , 'line_authorization' )
37- . then ( result => result )
38- . then ( result => {
39- if ( result && result . success ) {
40- const newConf = { ...confTmp }
41- newConf . tokenDetails = result . data
42- setConf ( newConf )
43- setisAuthorized ( true )
44- setSnackbar ( { show : true , msg : __ ( 'Authorized Successfully' , 'bit-integrations' ) } )
45- } else if (
46- ( result && result . data && result . data . data ) ||
47- ( ! result . success && typeof result . data === 'string' )
48- ) {
49- setSnackbar ( {
50- show : true ,
51- msg : `${ __ ( 'Authorization failed Cause:' , 'bit-integrations' ) } ${ result . data . data || result . data } . ${ __ ( 'please try again' , 'bit-integrations' ) } `
52- } )
53- } else {
54- setSnackbar ( {
55- show : true ,
56- msg : __ ( 'Authorization failed. please try again' , 'bit-integrations' )
57- } )
58- }
59- setIsLoading ( false )
60- } )
61- }
62-
63- export const handleFieldMapping = ( event , index , conftTmp , setConf , type ) => {
64- setConf ( prevConf => {
65- const newConf = { ...prevConf }
66-
67- if ( ! Array . isArray ( newConf [ type ] ) ) {
68- newConf [ type ] = [ ]
69- }
70-
71- if ( ! newConf [ type ] [ index ] ) {
72- newConf [ type ] [ index ] = { }
73- }
26+ try {
27+ const result = await bitsFetch ( { accessToken : confTmp . accessToken } , 'line_authorization' )
7428
75- newConf [ type ] [ index ] [ event . target . name ] = event . target . value
29+ if ( result ?. success ) {
30+ setConf ( { ...confTmp , tokenDetails : result . data } )
31+ setisAuthorized ( true )
32+ setSnackbar ( { show : true , msg : __ ( 'Authorized Successfully' , 'bit-integrations' ) } )
33+ } else {
34+ const msg =
35+ result ?. data ?. data || ( ! result . success && typeof result . data === 'string' )
36+ ? `${ __ ( 'Authorization failed Cause:' , 'bit-integrations' ) } ${ result . data ?. data || result . data } . ${ __ ( 'please try again' , 'bit-integrations' ) } `
37+ : __ ( 'Authorization failed. please try again' , 'bit-integrations' )
7638
77- if ( event . target . value === 'custom' ) {
78- newConf [ type ] [ index ] . customValue = ''
39+ setSnackbar ( { show : true , msg } )
7940 }
80-
81- return newConf
82- } )
41+ } finally {
42+ setIsLoading ( false )
43+ }
8344}
8445
85- export const delFieldMap = ( index , confTmp , setConf , type ) => {
86- setConf ( prevConf => {
87- const fieldMap = prevConf [ type ] || [ ]
88- if ( fieldMap . length <= 1 ) return prevConf
89-
90- const target = fieldMap [ index ]
91- let updatedFieldMap
92-
93- if ( target ?. groupId ) {
94- updatedFieldMap = fieldMap . filter ( f => f . groupId !== target . groupId )
95- } else {
96- updatedFieldMap = fieldMap . filter ( ( _ , i ) => i !== index )
97- }
46+ const updateFieldMap = ( prevConf , type , index , updater ) => {
47+ const newConf = { ...prevConf }
48+ if ( ! Array . isArray ( newConf [ type ] ) ) newConf [ type ] = [ ]
49+ if ( ! newConf [ type ] [ index ] ) newConf [ type ] [ index ] = { }
50+ newConf [ type ] [ index ] = { ...newConf [ type ] [ index ] , ...updater ( newConf [ type ] [ index ] ) }
51+ return newConf
52+ }
9853
99- return {
100- ...prevConf ,
101- [ type ] : updatedFieldMap
102- }
103- } )
54+ export const handleFieldMapping = ( event , index , _ , setConf , type ) => {
55+ setConf ( prev =>
56+ updateFieldMap ( prev , type , index , ( ) => ( {
57+ [ event . target . name ] : event . target . value ,
58+ ...( event . target . value === 'custom' ? { customValue : '' } : { } )
59+ } ) )
60+ )
10461}
10562
106- export const handleCustomValue = ( event , index , conftTmp , setConf , type ) => {
107- setConf ( prevConf => {
108- const newConf = { ...prevConf }
109- newConf [ type ] [ index ] . customValue = event ?. target ?. value || event
110- return newConf
111- } )
63+ export const handleCustomValue = ( event , index , _ , setConf , type ) => {
64+ const value = event ?. target ?. value ?? event
65+ setConf ( prev => updateFieldMap ( prev , type , index , ( ) => ( { customValue : value } ) ) )
11266}
11367
114- export const generateMappedField = fields => {
115- const requiredFlds = fields . filter ( fld => fld . required === true )
68+ export const delFieldMap = ( index , _ , setConf , type ) => {
69+ setConf ( prev => {
70+ const fieldMap = prev [ type ] || [ ]
71+ if ( fieldMap . length <= 1 ) return prev
11672
117- return requiredFlds . length > 0
118- ? requiredFlds . map ( field => ( {
119- formField : '' ,
120- lineFormField : field . value
121- } ) )
122- : [ { formField : '' , lineFormField : '' } ]
73+ const updatedFieldMap = fieldMap [ index ] ?. groupId
74+ ? fieldMap . filter ( f => f . groupId !== fieldMap [ index ] . groupId )
75+ : fieldMap . filter ( ( _ , i ) => i !== index )
76+
77+ return { ... prev , [ type ] : updatedFieldMap }
78+ } )
12379}
12480
12581export const addFieldMap = ( i , confTmp , setConf , FieldMappings , mapKey ) => {
126- const newConf = { ...confTmp }
127-
12882 const groupId = Date . now ( ) + Math . random ( )
12983 const newFieldMap = FieldMappings . map ( field => ( {
13084 formField : '' ,
13185 lineFormField : field . value ,
13286 groupId
13387 } ) )
134- newConf [ mapKey ] . splice ( i , 0 , ...newFieldMap )
135- setConf ( { ...newConf } )
88+
89+ setConf ( prev => {
90+ const newConf = { ...prev }
91+ if ( ! Array . isArray ( newConf [ mapKey ] ) ) newConf [ mapKey ] = [ ]
92+ newConf [ mapKey ] . splice ( i , 0 , ...newFieldMap )
93+ return newConf
94+ } )
13695}
13796
138- export const validateLineConfiguration = lineConf => {
139- const isMessageFieldConfigured = ( ) => {
140- if ( ! lineConf . message_field_map || lineConf . message_field_map . length === 0 ) {
141- return false
142- }
97+ const isFieldMapConfigured = fieldMap =>
98+ Array . isArray ( fieldMap ) &&
99+ fieldMap . length > 0 &&
100+ fieldMap . every ( f => f && ( f . formField !== 'custom' ? f . formField ?. trim ( ) : f . customValue ?. trim ( ) ) )
143101
144- const messageField = lineConf . message_field_map [ 0 ]
145- if ( ! messageField ) return false
102+ const isMessageFieldConfigured = lineConf =>
103+ lineConf . message_field_map ?. [ 0 ] &&
104+ ( lineConf . message_field_map [ 0 ] . formField !== 'custom'
105+ ? lineConf . message_field_map [ 0 ] . formField ?. trim ( )
106+ : lineConf . message_field_map [ 0 ] . customValue ?. trim ( ) )
146107
147- if ( messageField . formField === 'custom' ) {
148- return messageField . customValue && messageField . customValue . trim ( ) !== ''
149- }
150- return messageField . formField && messageField . formField . trim ( ) !== ''
151- }
108+ export const validateLineConfiguration = lineConf => {
109+ let baseTypeValid = false
152110
153111 switch ( lineConf . messageType ) {
154112 case 'sendPushMessage' :
155- const hasRecipientId = lineConf . recipientId && lineConf . recipientId . trim ( ) !== ''
156- const hasMessageField = isMessageFieldConfigured ( )
157- return hasRecipientId && hasMessageField
158-
113+ baseTypeValid = lineConf . recipientId ?. trim ( ) && isMessageFieldConfigured ( lineConf )
114+ break
159115 case 'sendReplyMessage' :
160- const hasReplyToken = lineConf . replyToken && lineConf . replyToken . trim ( ) !== ''
161- const hasReplyMessageField = isMessageFieldConfigured ( )
162- return hasReplyToken && hasReplyMessageField
163-
116+ baseTypeValid = lineConf . replyToken ?. trim ( ) && isMessageFieldConfigured ( lineConf )
117+ break
164118 case 'sendBroadcastMessage' :
165- const hasBroadcastMessageField = isMessageFieldConfigured ( )
166- return hasBroadcastMessageField
167-
168- default :
169- return false
119+ baseTypeValid = isMessageFieldConfigured ( lineConf )
120+ break
170121 }
122+
123+ const attachmentsValid = [
124+ ! lineConf . sendEmojis || isFieldMapConfigured ( lineConf . emojis_field_map ) ,
125+ ! lineConf . sendSticker || isFieldMapConfigured ( lineConf . sticker_field_map ) ,
126+ ! lineConf . sendImage || isFieldMapConfigured ( lineConf . image_field_map ) ,
127+ ! lineConf . sendAudio || isFieldMapConfigured ( lineConf . audio_field_map ) ,
128+ ! lineConf . sendVideo || isFieldMapConfigured ( lineConf . video_field_map ) ,
129+ ! lineConf . sendLocation || isFieldMapConfigured ( lineConf . location_field_map )
130+ ] . every ( Boolean )
131+
132+ return baseTypeValid && attachmentsValid
171133}
172134
173135export const getLineValidationMessages = lineConf => {
174136 const messages = [ ]
175137
176- const isMessageFieldConfigured = ( ) => {
177- if ( ! lineConf . message_field_map || lineConf . message_field_map . length === 0 ) {
178- return false
179- }
180-
181- const messageField = lineConf . message_field_map [ 0 ]
182- if ( ! messageField ) return false
183-
184- if ( messageField . formField === 'custom' ) {
185- return messageField . customValue && messageField . customValue . trim ( ) !== ''
186- }
187- return messageField . formField && messageField . formField . trim ( ) !== ''
188- }
138+ const fieldMappingChecks = [
139+ [ 'sendEmojis' , 'emojis_field_map' , 'Emojis' ] ,
140+ [ 'sendSticker' , 'sticker_field_map' , 'Sticker' ] ,
141+ [ 'sendImage' , 'image_field_map' , 'Image' ] ,
142+ [ 'sendAudio' , 'audio_field_map' , 'Audio' ] ,
143+ [ 'sendVideo' , 'video_field_map' , 'Video' ] ,
144+ [ 'sendLocation' , 'location_field_map' , 'Location' ]
145+ ]
189146
190147 switch ( lineConf . messageType ) {
191148 case 'sendPushMessage' :
192- if ( ! lineConf . recipientId || lineConf . recipientId . trim ( ) === '' ) {
193- messages . push ( 'Recipient ID is required' )
194- }
195- if ( ! isMessageFieldConfigured ( ) ) {
196- messages . push ( 'Message field mapping is required' )
197- }
149+ if ( ! lineConf . recipientId ?. trim ( ) )
150+ messages . push ( __ ( 'Recipient ID is required' , 'bit-integrations' ) )
151+ if ( ! isMessageFieldConfigured ( lineConf ) )
152+ messages . push ( __ ( 'Message field mapping is required' , 'bit-integrations' ) )
198153 break
199-
200154 case 'sendReplyMessage' :
201- if ( ! lineConf . replyToken || lineConf . replyToken . trim ( ) === '' ) {
202- messages . push ( 'Reply Token is required' )
203- }
204- if ( ! isMessageFieldConfigured ( ) ) {
205- messages . push ( 'Message field mapping is required' )
206- }
155+ if ( ! lineConf . replyToken ?. trim ( ) ) messages . push ( __ ( 'Reply Token is required' , 'bit-integrations' ) )
156+ if ( ! isMessageFieldConfigured ( lineConf ) )
157+ messages . push ( __ ( 'Message field mapping is required' , 'bit-integrations' ) )
207158 break
208-
209159 case 'sendBroadcastMessage' :
210- if ( ! isMessageFieldConfigured ( ) ) {
211- messages . push ( 'Message field mapping is required' )
212- }
160+ if ( ! isMessageFieldConfigured ( lineConf ) )
161+ messages . push ( __ ( 'Message field mapping is required' , 'bit-integrations' ) )
213162 break
214-
215163 default :
216- messages . push ( 'Please select a message type' )
164+ messages . push ( __ ( 'Please select a message type' , 'bit-integrations' ) )
217165 }
218166
167+ fieldMappingChecks . forEach ( ( [ flag , mapKey , name ] ) => {
168+ if ( lineConf [ flag ] && ! isFieldMapConfigured ( lineConf [ mapKey ] ) ) {
169+ messages . push ( `${ name } field mapping is required` )
170+ }
171+ } )
172+
219173 return messages
220174}
175+
176+ export const generateMappedField = fields => {
177+ const requiredFlds = fields . filter ( f => f . required )
178+ return requiredFlds . length
179+ ? requiredFlds . map ( f => ( { formField : '' , lineFormField : f . value } ) )
180+ : [ { formField : '' , lineFormField : '' } ]
181+ }
0 commit comments