259 lines
7.1 KiB
TypeScript
259 lines
7.1 KiB
TypeScript
import type { GenericResponse } from "~/types";
|
|
import type { AddressesList, UserAddressOfficial } from "~/types/checkout";
|
|
import { validation } from "../utils/validation";
|
|
import { REGEX_PHONE } from "../utils/regex";
|
|
|
|
export const useCheckoutStore = defineStore("checkoutStore", () => {
|
|
const { $toast } = useNuxtApp();
|
|
const menuStore = useMenuStore();
|
|
const selectedIso = ref(menuStore.selectedCountry);
|
|
|
|
// get address list
|
|
const addressesList = ref<AddressesList[]>();
|
|
const activeAddress = ref<AddressesList | null>();
|
|
async function getAddressList() {
|
|
try {
|
|
const { data } = await useMyFetch<GenericResponse<AddressesList[]>>(
|
|
`/api/restricted/user/addresses`,
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
onErrorOccured: async (_, status) => {
|
|
throw createError({
|
|
statusCode: status,
|
|
statusMessage: `HTTP error: ${status}`,
|
|
});
|
|
},
|
|
}
|
|
);
|
|
|
|
addressesList.value = data;
|
|
activeAddress.value = addressesList.value[0];
|
|
} catch (error) {
|
|
console.error("restrictedAddress error:", error);
|
|
}
|
|
}
|
|
|
|
// get user data
|
|
const userName = ref("");
|
|
const lastName = ref("");
|
|
const address = ref("");
|
|
const postCode = ref("");
|
|
const city = ref("");
|
|
const country = ref("");
|
|
const phoneNumber = ref("");
|
|
const accountPhoneNumber = ref("");
|
|
async function getUserData() {
|
|
try {
|
|
const { data } = await useMyFetch<GenericResponse<UserAddressOfficial>>(
|
|
`/api/restricted/user/address/official`,
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
onErrorOccured: async (_, status) => {
|
|
throw createError({
|
|
statusCode: status,
|
|
statusMessage: `HTTP error: ${status}`,
|
|
});
|
|
},
|
|
}
|
|
);
|
|
|
|
userName.value = data.address.name;
|
|
lastName.value = data.address.surname;
|
|
address.value = data.address.street;
|
|
postCode.value = data.address.postcode;
|
|
city.value = data.address.city;
|
|
country.value = data.address.country.country_lang[0].name;
|
|
} catch (error) {
|
|
console.error("getUserData error:", error);
|
|
}
|
|
}
|
|
|
|
// upload new address
|
|
const vNewAddressName = ref("");
|
|
const vNewAddressSurname = ref("");
|
|
const vNewAddressAddress = ref("");
|
|
const vNewAddressCode = ref("");
|
|
const vNewAddressCity = ref("");
|
|
const vNewAddressCountry = ref();
|
|
const vUseAccountPhoneNumber = ref(false);
|
|
const isOpen = ref<boolean>(false);
|
|
async function uploadAddress() {
|
|
try {
|
|
const res = await useMyFetch<GenericResponse<object>>(
|
|
`/api/restricted/user/address`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
address: {
|
|
city: vNewAddressCity.value,
|
|
country_iso: vNewAddressCountry.value.iso_code,
|
|
name: vNewAddressName.value,
|
|
postcode: vNewAddressCode.value,
|
|
street: vNewAddressAddress.value,
|
|
surname: vNewAddressSurname.value,
|
|
},
|
|
}),
|
|
onErrorOccured: async (_, status) => {
|
|
throw createError({
|
|
statusCode: status,
|
|
statusMessage: `HTTP error: ${status}`,
|
|
});
|
|
},
|
|
}
|
|
);
|
|
|
|
if (res.status === 200) {
|
|
$toast.success("Address successfully added", {
|
|
autoClose: 5000,
|
|
dangerouslyHTMLString: true,
|
|
});
|
|
isOpen.value = false;
|
|
getAddressList();
|
|
} else {
|
|
$toast.error("Failed to add address. Please try again.", {
|
|
autoClose: 5000,
|
|
dangerouslyHTMLString: true,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("uploadAddress error:", error);
|
|
}
|
|
}
|
|
|
|
const currentPrefix = ref<string | number>(
|
|
menuStore.selectedCountry.call_prefix
|
|
);
|
|
const changePrefix = (item: string) => {
|
|
currentPrefix.value = item;
|
|
};
|
|
const phoneValidation = ref<boolean | null>(null);
|
|
|
|
const userStore = useUserStore();
|
|
// send form
|
|
async function sendForm() {
|
|
let phoneNum = vUseAccountPhoneNumber.value
|
|
? accountPhoneNumber.value
|
|
: `${currentPrefix.value}${phoneNumber.value}`.replaceAll(" ", "").trim();
|
|
// if (vUseAccountPhoneNumber.value) {
|
|
// phoneNum = phoneNumber.value;
|
|
// }
|
|
|
|
phoneValidation.value = validation(phoneNum, 1, 49, REGEX_PHONE);
|
|
if (!phoneValidation.value && !vUseAccountPhoneNumber.value) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await useMyFetch<GenericResponse<object>>(
|
|
`/api/restricted/cart/checkout/delivery`,
|
|
{
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
address: {
|
|
city: activeAddress.value?.address.city,
|
|
country_iso: activeAddress.value?.address.country_iso,
|
|
name: activeAddress.value?.address.name,
|
|
postcode: activeAddress.value?.address.postcode,
|
|
street: activeAddress.value?.address.street,
|
|
surname: activeAddress.value?.address.surname,
|
|
},
|
|
phone_number: phoneNum,
|
|
email: userStore.fullUserData?.email,
|
|
}),
|
|
onErrorOccured: async (_, status) => {
|
|
throw createError({
|
|
statusCode: status,
|
|
statusMessage: `HTTP error: ${status}`,
|
|
});
|
|
},
|
|
}
|
|
);
|
|
|
|
if (res.status === 200) {
|
|
$toast.success("Form successfully sent", {
|
|
autoClose: 5000,
|
|
dangerouslyHTMLString: true,
|
|
});
|
|
// redirectToSummary();
|
|
} else {
|
|
$toast.error("Failed to send form. Please try again.", {
|
|
autoClose: 5000,
|
|
dangerouslyHTMLString: true,
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("uploadAddress error:", error);
|
|
}
|
|
}
|
|
|
|
const changeActive = (item: any) => {
|
|
activeAddress.value = item;
|
|
};
|
|
|
|
async function getCheckout() {
|
|
try {
|
|
const res = await useMyFetch<GenericResponse<object>>(
|
|
`/api/restricted/cart/checkout`,
|
|
{
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
onErrorOccured: async (_, status) => {
|
|
throw createError({
|
|
statusCode: status,
|
|
statusMessage: `HTTP error: ${status}`,
|
|
});
|
|
},
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.error("uploadAddress error:", error);
|
|
}
|
|
}
|
|
|
|
return {
|
|
addressesList,
|
|
activeAddress,
|
|
isOpen,
|
|
selectedIso,
|
|
phoneValidation,
|
|
|
|
userName,
|
|
lastName,
|
|
address,
|
|
postCode,
|
|
city,
|
|
country,
|
|
phoneNumber,
|
|
accountPhoneNumber,
|
|
vUseAccountPhoneNumber,
|
|
|
|
currentPrefix,
|
|
vNewAddressName,
|
|
vNewAddressSurname,
|
|
vNewAddressAddress,
|
|
vNewAddressCode,
|
|
vNewAddressCity,
|
|
vNewAddressCountry,
|
|
|
|
changePrefix,
|
|
getCheckout,
|
|
getAddressList,
|
|
getUserData,
|
|
changeActive,
|
|
uploadAddress,
|
|
sendForm,
|
|
};
|
|
});
|