@@ -366,10 +366,11 @@ const onSubmit = form.handleSubmit(async (values) => {
366366})
367367
368368const handleSave = async (values ) => {
369- if (! areRulesValid ()) {
369+ const validationError = getRulesValidationError ()
370+ if (validationError) {
370371 emitter .emit (EMITTER_EVENTS .SHOW_TOAST , {
371- variant: ' destructive ' ,
372- description: t ( ' admin.automation.invalid ' )
372+ variant: ' warning ' ,
373+ description: validationError
373374 })
374375 return
375376 }
@@ -401,58 +402,65 @@ const handleSave = async (values) => {
401402 }
402403}
403404
404- // TODO: Maybe we can do some vee validate magic here .
405- const areRulesValid = () => {
405+ // Returns a specific validation error message, or empty string if valid .
406+ const getRulesValidationError = () => {
406407 // Must have groups.
407408 if (rule .value .rules [0 ].groups .length == 0 ) {
408- return false
409+ return t ( ' admin.automation.validation.addCondition ' )
409410 }
410411
411- // At least one group should have at least one rule
412+ // At least one group should have at least one rule.
412413 const group1HasRules = rule .value .rules [0 ].groups [0 ].rules .length > 0
413414 const group2HasRules = rule .value .rules [0 ].groups [1 ].rules .length > 0
414415 if (! group1HasRules && ! group2HasRules) {
415- return false
416+ return t ( ' admin.automation.validation.addCondition ' )
416417 }
417418
418- // For both groups, each rule should have value , operator and field .
419+ // For both groups, each rule should have field , operator, and value .
419420 for (const group of rule .value .rules [0 ].groups ) {
420421 for (const rule of group .rules ) {
421- if (! rule .field || ! rule .operator ) {
422- return false
422+ if (! rule .field ) {
423+ return t (' admin.automation.validation.selectField' )
424+ }
425+ if (! rule .operator ) {
426+ return t (' admin.automation.validation.selectOperator' )
423427 }
424- // For 'set' and ` not set` operator, value is not required.
428+ // For 'set' and ' not set' operator, value is not required.
425429 if (rule .operator !== OPERATOR .SET && rule .operator !== OPERATOR .NOT_SET && ! rule .value ) {
426- return false
430+ return t ( ' admin.automation.validation.setConditionValue ' )
427431 }
428432 }
429433 }
430434
431- // Must have atleast one action.
435+ // Must have at least one action.
432436 if (rule .value .rules [0 ].actions .length == 0 ) {
433- return false
437+ return t ( ' admin.automation.validation.addAction ' )
434438 }
435439
436- // Make sure each action has value.
440+ // Make sure each action has a type and value.
437441 for (const action of rule .value .rules [0 ].actions ) {
442+ if (! action .type ) {
443+ return t (' admin.automation.validation.selectActionType' )
444+ }
445+
438446 // CSAT action does not require value, set dummy value.
439447 if (action .type === ' send_csat' ) {
440448 action .value = [' 0' ]
441449 }
442450
443451 // Empty array, no value selected.
444452 if (action .value .length === 0 ) {
445- return false
453+ return t ( ' admin.automation.validation.setActionValue ' )
446454 }
447455
448456 // Check if all values are present.
449457 for (const key in action .value ) {
450458 if (! action .value [key]) {
451- return false
459+ return t ( ' admin.automation.validation.setActionValue ' )
452460 }
453461 }
454462 }
455- return true
463+ return ' '
456464}
457465
458466onMounted (async () => {
0 commit comments