diff --git a/app/javascript/dashboard/composables/whatsapp/useWhatsappApiRequest.js b/app/javascript/dashboard/composables/whatsapp/useWhatsappApiRequest.js new file mode 100644 index 000000000..ed6c2e875 --- /dev/null +++ b/app/javascript/dashboard/composables/whatsapp/useWhatsappApiRequest.js @@ -0,0 +1,52 @@ +export function useWhatsappApiRequest() { + const createSignupPayload = (authCode, store, businessDataParam) => { + const accountId = store.getters.getCurrentAccountId; + return { + account_id: accountId, + code: authCode.value, + business_id: businessDataParam.business_id, + waba_id: businessDataParam.waba_id, + phone_number_id: businessDataParam.phone_number_id, + }; + }; + + const createFetchOptions = (payload, authHeaders) => { + return { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRF-Token': document + .querySelector('meta[name="csrf-token"]') + ?.getAttribute('content'), + ...authHeaders.value, + }, + body: JSON.stringify(payload), + }; + }; + + const makeSignupRequest = async (payload, authHeaders) => { + const options = createFetchOptions(payload, authHeaders); + return fetch('/whatsapp/embedded_signup', options); + }; + + const processApiResponse = async ( + response, + authCode, + handleSignupSuccess + ) => { + const responseData = await response.json(); + + if (response.ok) { + authCode.value = null; + handleSignupSuccess(responseData); + } else { + throw new Error(responseData.message || responseData.error); + } + }; + + return { + createSignupPayload, + makeSignupRequest, + processApiResponse, + }; +} diff --git a/app/javascript/dashboard/composables/whatsapp/useWhatsappAuth.js b/app/javascript/dashboard/composables/whatsapp/useWhatsappAuth.js new file mode 100644 index 000000000..888d1a03b --- /dev/null +++ b/app/javascript/dashboard/composables/whatsapp/useWhatsappAuth.js @@ -0,0 +1,33 @@ +import { computed } from 'vue'; +import Auth from 'dashboard/api/auth'; + +export function useWhatsappAuth() { + const authHeaders = computed(() => { + if (!Auth.hasAuthCookie()) return {}; + + const { + 'access-token': accessToken, + 'token-type': tokenType, + client, + expiry, + uid, + } = Auth.getAuthData(); + + return { + 'access-token': accessToken, + 'token-type': tokenType, + client, + expiry, + uid, + }; + }); + + const validateAuthRequirements = (authCodeReceived, authCode) => { + return authCodeReceived.value && authCode.value; + }; + + return { + authHeaders, + validateAuthRequirements, + }; +} diff --git a/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupApi.js b/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupApi.js index a93405f27..ba51e9f37 100644 --- a/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupApi.js +++ b/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupApi.js @@ -1,5 +1,6 @@ -import { computed } from 'vue'; -import Auth from 'dashboard/api/auth'; +import { useWhatsappAuth } from './useWhatsappAuth'; +import { useWhatsappApiRequest } from './useWhatsappApiRequest'; +import { useWhatsappSignupFlow } from './useWhatsappSignupFlow'; export function useWhatsappSignupApi({ authCodeReceived, @@ -12,91 +13,33 @@ export function useWhatsappSignupApi({ handleSignupError, handleSignupSuccess, }) { - const authHeaders = computed(() => { - if (Auth.hasAuthCookie()) { - const { - 'access-token': accessToken, - 'token-type': tokenType, - client, - expiry, - uid, - } = Auth.getAuthData(); - return { - 'access-token': accessToken, - 'token-type': tokenType, - client, - expiry, - uid, - }; - } - return {}; - }); + const { authHeaders, validateAuthRequirements } = useWhatsappAuth(); - const validateAuthRequirements = () => { - return authCodeReceived.value && authCode.value; - }; + const { createSignupPayload, makeSignupRequest, processApiResponse } = + useWhatsappApiRequest(); - const createSignupPayload = businessDataParam => { - const accountId = store.getters.getCurrentAccountId; - return { - account_id: accountId, - code: authCode.value, - business_id: businessDataParam.business_id, - waba_id: businessDataParam.waba_id, - phone_number_id: businessDataParam.phone_number_id, - }; - }; - - const createFetchOptions = payload => { - return { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-CSRF-Token': document - .querySelector('meta[name="csrf-token"]') - ?.getAttribute('content'), - ...authHeaders.value, - }, - body: JSON.stringify(payload), - }; - }; - - const setProcessingState = () => { - currentStep.value = 'processing'; - isProcessing.value = true; - processingMessage.value = t( - 'INBOX_MGMT.ADD.WHATSAPP.EMBEDDED_SIGNUP.PROCESSING' - ); - }; - - const handleApiResponse = async response => { - const responseData = await response.json(); - - if (response.ok) { - authCode.value = null; - handleSignupSuccess(responseData); - } else { - throw new Error(responseData.message || responseData.error); - } - }; + const { setProcessingState, handleAuthError, handleRequestError } = + useWhatsappSignupFlow({ + currentStep, + isProcessing, + processingMessage, + t, + }); const completeSignupFlow = async businessDataParam => { - if (!validateAuthRequirements()) { - handleSignupError({ - error: t('INBOX_MGMT.ADD.WHATSAPP.EMBEDDED_SIGNUP.AUTH_NOT_COMPLETED'), - }); + if (!validateAuthRequirements(authCodeReceived, authCode)) { + handleAuthError(handleSignupError, t); return; } setProcessingState(); try { - const payload = createSignupPayload(businessDataParam); - const options = createFetchOptions(payload); - const response = await fetch('/whatsapp/embedded_signup', options); - await handleApiResponse(response); + const payload = createSignupPayload(authCode, store, businessDataParam); + const response = await makeSignupRequest(payload, authHeaders); + await processApiResponse(response, authCode, handleSignupSuccess); } catch (error) { - handleSignupError({ error: error.message }); + handleRequestError(error, handleSignupError); } }; diff --git a/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupFlow.js b/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupFlow.js new file mode 100644 index 000000000..237e1ea14 --- /dev/null +++ b/app/javascript/dashboard/composables/whatsapp/useWhatsappSignupFlow.js @@ -0,0 +1,32 @@ +export function useWhatsappSignupFlow({ + currentStep, + isProcessing, + processingMessage, + t, +}) { + const setProcessingState = () => { + currentStep.value = 'processing'; + isProcessing.value = true; + processingMessage.value = t( + 'INBOX_MGMT.ADD.WHATSAPP.EMBEDDED_SIGNUP.PROCESSING' + ); + }; + + const handleAuthError = (handleSignupError, translator) => { + handleSignupError({ + error: translator( + 'INBOX_MGMT.ADD.WHATSAPP.EMBEDDED_SIGNUP.AUTH_NOT_COMPLETED' + ), + }); + }; + + const handleRequestError = (error, handleSignupError) => { + handleSignupError({ error: error.message }); + }; + + return { + setProcessingState, + handleAuthError, + handleRequestError, + }; +}