chore: fix issues
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user