1- /* eslint-disable no-else-return */
21import { __ } from '../../../Utils/i18nwrap'
32import bitsFetch from '../../../Utils/bitsFetch'
43
@@ -108,34 +107,48 @@ const getFieldTypeGroup = lineFormField => {
108107}
109108
110109const generateGroupId = ( fieldType , existingFieldMap ) => {
110+ if ( fieldType === 'audio' ) {
111+ const existingAudioGroups = [
112+ ...new Set (
113+ existingFieldMap . filter ( field => field . fieldType === 'audio' ) . map ( field => field . groupId )
114+ )
115+ ]
116+ return `audio_${ existingAudioGroups . length + 1 } `
117+ }
111118 const existingGroups = [
112119 ...new Set (
113120 existingFieldMap
114121 . filter ( field => getFieldTypeGroup ( field . lineFormField ) === fieldType )
115122 . map ( field => field . groupId )
116123 )
117124 ]
118-
119125 return `${ fieldType } _${ existingGroups . length + 1 } `
120126}
121127
122128export const addFieldMap = ( i , confTmp , setConf , FieldMappings , mapKey ) => {
123129 setConf ( prev => {
124130 const newConf = { ...prev }
125131 if ( ! Array . isArray ( newConf [ mapKey ] ) ) newConf [ mapKey ] = [ ]
126-
127- const newFieldMap = FieldMappings . map ( field => {
128- const fieldType = getFieldTypeGroup ( field . value )
129- const groupId = generateGroupId ( fieldType , newConf [ mapKey ] )
130-
131- return {
132+ if ( mapKey === 'audio_field_map' ) {
133+ const fieldType = 'audio'
134+ const groupId = generateGroupId ( 'audio' , newConf [ mapKey ] )
135+ const newFieldMap = FieldMappings . map ( field => ( {
132136 formField : '' ,
133137 lineFormField : field . value ,
134138 groupId,
135- fieldType
136- }
137- } )
138-
139+ fieldType : 'audio'
140+ } ) )
141+ newConf [ mapKey ] . splice ( i , 0 , ...newFieldMap )
142+ return newConf
143+ }
144+ let fieldType = getFieldTypeGroup ( FieldMappings [ 0 ] . value )
145+ let groupId = generateGroupId ( fieldType , newConf [ mapKey ] )
146+ const newFieldMap = FieldMappings . map ( field => ( {
147+ formField : '' ,
148+ lineFormField : field . value ,
149+ groupId,
150+ fieldType
151+ } ) )
139152 newConf [ mapKey ] . splice ( i , 0 , ...newFieldMap )
140153 return newConf
141154 } )
@@ -195,13 +208,11 @@ export const getLineValidationMessages = lineConf => {
195208 case 'sendPushMessage' :
196209 if ( ! lineConf . recipientId ?. trim ( ) )
197210 messages . push ( __ ( 'Recipient ID is required' , 'bit-integrations' ) )
198-
199211 if ( ! isMessageFieldConfigured ( lineConf ) )
200212 messages . push ( __ ( 'Message field mapping is required' , 'bit-integrations' ) )
201213 break
202214 case 'sendReplyMessage' :
203215 if ( ! lineConf . replyToken ?. trim ( ) ) messages . push ( __ ( 'Reply Token is required' , 'bit-integrations' ) )
204-
205216 if ( ! isMessageFieldConfigured ( lineConf ) )
206217 messages . push ( __ ( 'Message field mapping is required' , 'bit-integrations' ) )
207218 break
@@ -222,20 +233,20 @@ export const getLineValidationMessages = lineConf => {
222233 return messages
223234}
224235
225- export const generateMappedField = fields => {
236+ export const generateMappedField = ( fields , fieldType = null ) => {
226237 const requiredFlds = fields . filter ( f => f . required )
227238 if ( requiredFlds . length ) {
228239 const typeToGroupId = { }
229240 return requiredFlds . map ( f => {
230- const fieldType = getFieldTypeGroup ( f . value )
231- if ( ! typeToGroupId [ fieldType ] ) {
232- typeToGroupId [ fieldType ] = `${ fieldType } _1`
241+ const actualFieldType = fieldType || getFieldTypeGroup ( f . value )
242+ if ( ! typeToGroupId [ actualFieldType ] ) {
243+ typeToGroupId [ actualFieldType ] = `${ actualFieldType } _1`
233244 }
234245 return {
235246 formField : '' ,
236247 lineFormField : f . value ,
237- groupId : typeToGroupId [ fieldType ] ,
238- fieldType
248+ groupId : typeToGroupId [ actualFieldType ] ,
249+ fieldType : actualFieldType
239250 }
240251 } )
241252 }
0 commit comments