@@ -21,6 +21,7 @@ import {
2121 closeDrawer ,
2222 closeWizard ,
2323 resetWizard ,
24+ setExpressProject ,
2425} from 'src/features/express/expressSlice' ;
2526import * as Yup from 'yup' ;
2627import styled from 'styled-components' ;
@@ -39,7 +40,6 @@ import {
3940 EXPRESS_1_CAMPAIGN_TYPE_ID ,
4041} from 'src/constants' ;
4142import { format } from 'date-fns' ;
42- import async from 'async' ;
4343import {
4444 createCrons ,
4545 createPages ,
@@ -204,162 +204,146 @@ export const ExpressWizardContainer = () => {
204204 } , [ activeStep ] ) ;
205205
206206 // Form actions
207- const handleSubmit = (
207+ const handleSubmit = async (
208208 values : WizardModel ,
209209 { setSubmitting, setStatus } : FormikHelpers < WizardModel >
210210 ) => {
211211 // Save submitted form values
212212 setFormValues ( values ) ;
213213
214- // eslint-disable-next-line consistent-return
215- const projectHandle = ( next : any ) => {
216- try {
217- // Create project if it doesn't exist
218- if (
219- project &&
220- project . id === - 1 &&
221- activeWorkspace &&
222- activeWorkspace . id
223- ) {
224- createProject ( {
225- body : {
226- name : project . name ,
227- customer_id : activeWorkspace . id ,
228- } ,
229- } )
230- . unwrap ( )
231- . then ( ( payload ) => {
232- next ( null , payload ) ;
233- } )
234- . catch ( ( error ) => next ( error ) ) ;
235- } else {
236- next ( null , project ) ;
237- }
238- } catch ( error ) {
239- return next ( error ) ;
240- }
241- } ;
242-
243- // eslint-disable-next-line consistent-return
244- const campaignHandle = ( prj : Project , next : any ) => {
245- try {
246- const fallBackDate = format ( new Date ( ) , BASE_DATE_FORMAT ) ;
247- // Create the campaign
248- createCampaign ( {
214+ const projectHandle = async ( ) => {
215+ // Create project if it doesn't exist
216+ if (
217+ project &&
218+ project . id === - 1 &&
219+ activeWorkspace &&
220+ activeWorkspace . id
221+ ) {
222+ const prj = await createProject ( {
249223 body : {
250- title : values . campaign_name || 'Express campaign' ,
251- start_date : values . campaign_date
252- ? format ( values . campaign_date , BASE_DATE_FORMAT )
253- : fallBackDate ,
254- end_date : values . campaign_date_end
255- ? format ( values . campaign_date_end , BASE_DATE_FORMAT )
256- : fallBackDate ,
257- close_date : values . campaign_date_end
258- ? format ( values . campaign_date_end , BASE_DATE_FORMAT )
259- : fallBackDate ,
260- customer_title : values . campaign_name ,
261- campaign_type_id : getExpressCPTypeId ( expressTypeMeta . slug ) ,
262- project_id : prj ?. id || - 1 ,
263- pm_id : activeWorkspace ?. csm . id || - 1 ,
264- platforms : getPlatform ( values ) ,
265- customer_id : activeWorkspace ?. id || - 1 ,
266-
267- express_slug : expressTypeMeta . slug ,
268- has_bug_form : values . has_bug_form ? 1 : 0 ,
269- has_bug_parade : values . has_bug_parade ? 1 : 0 ,
270- ...( values . use_cases && { use_cases : values . use_cases } ) ,
224+ name : project . name ,
225+ customer_id : activeWorkspace . id ,
271226 } ,
272- } )
273- . unwrap ( )
274- . then ( async ( payload ) => {
275- next ( null , payload ) ;
276- } )
277- . catch ( ( error ) => next ( error ) ) ;
278- } catch ( error ) {
279- return next ( error ) ;
227+ } ) . unwrap ( ) ;
228+
229+ if ( prj ) dispatch ( setExpressProject ( prj ) ) ;
230+
231+ return prj ;
280232 }
233+
234+ return project ;
281235 } ;
282236
283- // eslint-disable-next-line consistent-return
284- const zapierHandle = ( cp : Campaign , next : any ) => {
285- try {
286- // Post on webhook Zapier axios call
287- fetch ( expressTypeData . webhook_url ?? ZAPIER_WEBHOOK_TRIGGER , {
288- method : 'POST' ,
289- mode : 'no-cors' ,
290- headers : {
291- 'Content-Type' : 'application/json' ,
292- Accept : 'application/json' ,
237+ const campaignHandle = async ( prj : Project ) => {
238+ const fallBackDate = format ( new Date ( ) , BASE_DATE_FORMAT ) ;
239+ // Create the campaign
240+ const cp = await createCampaign ( {
241+ body : {
242+ title : values . campaign_name || 'Express campaign' ,
243+ start_date : values . campaign_date
244+ ? format ( values . campaign_date , BASE_DATE_FORMAT )
245+ : fallBackDate ,
246+ end_date : values . campaign_date_end
247+ ? format ( values . campaign_date_end , BASE_DATE_FORMAT )
248+ : fallBackDate ,
249+ close_date : values . campaign_date_end
250+ ? format ( values . campaign_date_end , BASE_DATE_FORMAT )
251+ : fallBackDate ,
252+ customer_title : values . campaign_name ,
253+ campaign_type_id : getExpressCPTypeId ( expressTypeMeta . slug ) ,
254+ project_id : prj ?. id || - 1 ,
255+ pm_id : activeWorkspace ?. csm . id || - 1 ,
256+ platforms : getPlatform ( values ) ,
257+ customer_id : activeWorkspace ?. id || - 1 ,
258+
259+ express_slug : expressTypeMeta . slug ,
260+ has_bug_form : values . has_bug_form ? 1 : 0 ,
261+ has_bug_parade : values . has_bug_parade ? 1 : 0 ,
262+ ...( values . use_cases && { use_cases : values . use_cases } ) ,
263+ } ,
264+ } ) . unwrap ( ) ;
265+
266+ return cp ;
267+ } ;
268+
269+ const zapierHandle = async ( cp : Campaign ) => {
270+ // Post on webhook Zapier axios call
271+ await fetch ( expressTypeData . webhook_url ?? ZAPIER_WEBHOOK_TRIGGER , {
272+ method : 'POST' ,
273+ mode : 'no-cors' ,
274+ headers : {
275+ 'Content-Type' : 'application/json' ,
276+ Accept : 'application/json' ,
277+ } ,
278+ body : JSON . stringify ( {
279+ cp : {
280+ ...values ,
281+ id : cp . id ,
282+ ...( values . campaign_date && {
283+ start_date : format ( values . campaign_date , BASE_DATE_FORMAT ) ,
284+ } ) ,
285+ ...( values . campaign_date_end && {
286+ end_date : format ( values . campaign_date_end , BASE_DATE_FORMAT ) ,
287+ } ) ,
288+ ...( values . campaign_date_end && {
289+ close_date : format ( values . campaign_date_end , BASE_DATE_FORMAT ) ,
290+ } ) ,
291+ ...( values . campaign_reason && {
292+ reason : reasonItems [ values . campaign_reason ] ,
293+ } ) ,
293294 } ,
294- body : JSON . stringify ( {
295- cp : {
296- ...values ,
297- id : cp . id ,
298- ...( values . campaign_date && {
299- start_date : format ( values . campaign_date , BASE_DATE_FORMAT ) ,
300- } ) ,
301- ...( values . campaign_date_end && {
302- end_date : format ( values . campaign_date_end , BASE_DATE_FORMAT ) ,
303- } ) ,
304- ...( values . campaign_date_end && {
305- close_date : format ( values . campaign_date_end , BASE_DATE_FORMAT ) ,
306- } ) ,
307- ...( values . campaign_reason && {
308- reason : reasonItems [ values . campaign_reason ] ,
309- } ) ,
310- } ,
311- user : userData ,
312- workspace : activeWorkspace ,
313- } ) ,
314- } )
315- . then ( ( ) => next ( null , cp ) )
316- . catch ( ( error ) => next ( error ) ) ;
317- } catch ( error ) {
318- return next ( error ) ;
319- }
295+ user : userData ,
296+ workspace : activeWorkspace ,
297+ } ) ,
298+ } ) ;
320299 } ;
321300
322- // eslint-disable-next-line consistent-return
323- const wordpressHandle = ( cp : Campaign , next : any ) => {
301+ const wordpressHandle = async ( cp : Campaign ) => {
302+ // Post on webhook WordPress axios call
324303 try {
325- // Post on webhook WordPress axios call
326304 if ( ! values . use_cases ) {
327- createUseCases ( cp . id ) ;
305+ await createUseCases ( cp . id ) ;
328306 }
329307
330- createPages ( cp . id ) ;
331- createCrons ( cp . id ) ;
332- createTasks ( cp . id ) ;
333- return next ( null ) ;
334- } catch ( error ) {
335- return next ( null ) ; // Skip error handling
308+ await createPages ( cp . id ) ;
309+ await createCrons ( cp . id ) ;
310+ await createTasks ( cp . id ) ;
311+ } catch ( e ) {
312+ // eslint-disable-next-line no-console
313+ console . log ( 'Something went wrong with wp' , e ) ;
336314 }
337315 } ;
338316
339- return async . waterfall (
340- [ projectHandle , campaignHandle , zapierHandle , wordpressHandle ] ,
341- ( err : any ) => {
342- if ( err ) {
343- // eslint-disable-next-line no-console
344- console . error ( 'Submission error:' , err ) ;
345-
346- setSubmitting ( false ) ;
317+ try {
318+ const prj = await projectHandle ( ) ;
347319
348- // TODO: Show error message modal
349- setStatus ( { submitError : true } ) ;
350-
351- // Send error to GTM
352- sendGTMEvent ( {
353- event : 'generic_error' ,
354- content : JSON . stringify ( err ) ,
355- } ) ;
356- } else {
357- // eslint-disable-next-line no-console
358- console . log ( 'done' ) ;
320+ if ( prj ) {
321+ const cp = await campaignHandle ( prj ) ;
322+ if ( cp ) {
323+ await zapierHandle ( cp ) ;
324+ await wordpressHandle ( cp ) ;
359325 onNext ( ) ;
326+ } else {
327+ throw Error ( 'Something went wrong with campaign' ) ;
360328 }
329+ } else {
330+ throw Error ( 'Something went wrong with project' ) ;
361331 }
362- ) ;
332+ } catch ( e ) {
333+ // eslint-disable-next-line no-console
334+ console . error ( 'Submission error:' , e ) ;
335+
336+ setSubmitting ( false ) ;
337+
338+ // TODO: Show error message modal
339+ setStatus ( { submitError : true } ) ;
340+
341+ // Send error to GTM
342+ sendGTMEvent ( {
343+ event : 'generic_error' ,
344+ content : JSON . stringify ( e ) ,
345+ } ) ;
346+ }
363347 } ;
364348
365349 const closeExpressWizard = ( ) => {
0 commit comments