login
This commit is contained in:
255
stores/checkoutStore.ts
Normal file
255
stores/checkoutStore.ts
Normal file
@ -0,0 +1,255 @@
|
||||
import type { GenericResponse } from "~/types";
|
||||
import type { AddressesList } from "~/types/checkout";
|
||||
import { validation } from "../utils/validation";
|
||||
import { REGEX_PHONE } from "../utils/regex";
|
||||
|
||||
export const useCheckoutStore = defineStore("checkoutStore", () => {
|
||||
const { $toast } = useNuxtApp();
|
||||
|
||||
const addressesList = ref<AddressesList[]>();
|
||||
const activeAddress = ref<AddressesList | null>();
|
||||
async function restrictedAddress() {
|
||||
try {
|
||||
// const { data } = await useMyFetch<GenericResponse<object>>(
|
||||
// `/api/restricted/user/addresses`,
|
||||
// {
|
||||
// headers: {
|
||||
// "Content-Type": "application/json",
|
||||
// },
|
||||
// onErrorOccured: async (_, status) => {
|
||||
// throw createError({
|
||||
// statusCode: status,
|
||||
// statusMessage: `HTTP error: ${status}`,
|
||||
// });
|
||||
// },
|
||||
// }
|
||||
// );
|
||||
|
||||
const data = [
|
||||
{
|
||||
address: {
|
||||
city: "Bochnia",
|
||||
country_iso: "pl",
|
||||
name: "John",
|
||||
postcode: "32-700",
|
||||
street: "Karosek",
|
||||
surname: "Kornelsky",
|
||||
},
|
||||
address_id: 2,
|
||||
alias: "home",
|
||||
customer_id: 4,
|
||||
is_default: true,
|
||||
is_official: false,
|
||||
},
|
||||
];
|
||||
|
||||
addressesList.value = data;
|
||||
activeAddress.value = addressesList.value[0];
|
||||
} catch (error) {
|
||||
console.error("restrictedAddress error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
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 restrictedAddressOfficial() {
|
||||
try {
|
||||
// const { data } = await useMyFetch<GenericResponse<object>>(
|
||||
// `/api/restricted/user/address/official`,
|
||||
// {
|
||||
// headers: {
|
||||
// "Content-Type": "application/json",
|
||||
// },
|
||||
// onErrorOccured: async (_, status) => {
|
||||
// throw createError({
|
||||
// statusCode: status,
|
||||
// statusMessage: `HTTP error: ${status}`,
|
||||
// });
|
||||
// },
|
||||
// }
|
||||
// );
|
||||
|
||||
const data = {
|
||||
address: {
|
||||
city: "Bochnia",
|
||||
country_iso: "pl",
|
||||
name: "John",
|
||||
postcode: "32-700",
|
||||
street: "Karosek",
|
||||
surname: "Kornelsky",
|
||||
},
|
||||
address_id: 2,
|
||||
alias: "home",
|
||||
customer_id: 4,
|
||||
is_default: true,
|
||||
is_official: false,
|
||||
};
|
||||
|
||||
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_iso;
|
||||
// resolve this
|
||||
// accountPhoneNumber.value = useUserStore().fullUserData.phone_number;
|
||||
phoneNumber.value = "+36 789 3773 737";
|
||||
} catch (error) {
|
||||
console.error("restrictedAddressOfficial error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
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/official`,
|
||||
{
|
||||
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;
|
||||
restrictedAddress();
|
||||
} 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>("+43");
|
||||
const changePrefix = (item: any) => {
|
||||
currentPrefix.value = item;
|
||||
};
|
||||
const phoneValidation = ref<boolean | null>(null);
|
||||
async function sendForm() {
|
||||
let phoneNum = `${currentPrefix.value}${phoneNumber.value}`
|
||||
.replaceAll(" ", "")
|
||||
.trim();
|
||||
// if (vUseAccountPhoneNumber.value) {
|
||||
// phoneNum = phoneNumber.value;
|
||||
// }
|
||||
|
||||
phoneValidation.value = validation(phoneNum, 1, 49, REGEX_PHONE);
|
||||
try {
|
||||
const res = await useMyFetch<GenericResponse<object>>(
|
||||
`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: useUserStore().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;
|
||||
};
|
||||
|
||||
return {
|
||||
addressesList,
|
||||
activeAddress,
|
||||
isOpen,
|
||||
|
||||
userName,
|
||||
lastName,
|
||||
address,
|
||||
postCode,
|
||||
city,
|
||||
country,
|
||||
phoneNumber,
|
||||
accountPhoneNumber,
|
||||
vUseAccountPhoneNumber,
|
||||
|
||||
currentPrefix,
|
||||
vNewAddressName,
|
||||
vNewAddressSurname,
|
||||
vNewAddressAddress,
|
||||
vNewAddressCode,
|
||||
vNewAddressCity,
|
||||
vNewAddressCountry,
|
||||
|
||||
restrictedAddress,
|
||||
restrictedAddressOfficial,
|
||||
changeActive,
|
||||
uploadAddress,
|
||||
sendForm,
|
||||
};
|
||||
});
|
@ -56,7 +56,6 @@ export const useMenuStore = defineStore("menuStore", () => {
|
||||
const currencies = ref([] as Currency[]);
|
||||
const languages = ref([] as Language[]);
|
||||
|
||||
|
||||
const getLocales = async () => {
|
||||
const { data: countriesList } = await useMyFetch<GenericResponse<Country[]>>(`/api/public/country/list`);
|
||||
countries.value = countriesList;
|
||||
@ -96,7 +95,6 @@ export const useMenuStore = defineStore("menuStore", () => {
|
||||
console.warn("Root menu item not found");
|
||||
menu.value = [];
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import type { FrontPageSection } from "~/types/frontSection";
|
||||
|
||||
export const useStore = defineStore("store", () => {
|
||||
const currentPageID = ref("");
|
||||
|
||||
const { $toast } = useNuxtApp();
|
||||
|
||||
// calculator
|
||||
const monthlySavings = ref(137);
|
||||
@ -16,19 +16,13 @@ export const useStore = defineStore("store", () => {
|
||||
const totalInvestment: Ref<number> = ref(0);
|
||||
const minValue = ref();
|
||||
|
||||
// login
|
||||
const email = ref();
|
||||
const password = ref();
|
||||
|
||||
const components = ref({} as FrontPageSection[]);
|
||||
|
||||
|
||||
const getSections = async (id: string) => {
|
||||
const { data } = await useMyFetch<GenericResponse<FrontPageSection[]>>(
|
||||
`/api/public/front/sections/${id}`
|
||||
)
|
||||
components.value = data
|
||||
|
||||
);
|
||||
components.value = data;
|
||||
};
|
||||
|
||||
async function getComponents(): Promise<componentsListType[]> {
|
||||
@ -56,7 +50,6 @@ export const useStore = defineStore("store", () => {
|
||||
name: componentName,
|
||||
component: child.front_section,
|
||||
componentInstance: nonReactiveComponent,
|
||||
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Failed to load component ${componentName}`, error);
|
||||
@ -103,39 +96,12 @@ export const useStore = defineStore("store", () => {
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
minValue.value = data;
|
||||
} catch (error) {
|
||||
console.error("getList error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function logIn() {
|
||||
try {
|
||||
const { data } = await useMyFetch<GenericResponse<object>>(
|
||||
`/api/public/user/session/start`,
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
mail: email.value,
|
||||
password: password.value,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
onErrorOccured: (_, status) => {
|
||||
throw new Error(`HTTP error: ${status}`);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
minValue.value = data;
|
||||
} catch (error) {
|
||||
console.error("getList error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
currentPageID,
|
||||
components,
|
||||
@ -143,9 +109,6 @@ export const useStore = defineStore("store", () => {
|
||||
monthlySavings,
|
||||
storagePeriod,
|
||||
minValue,
|
||||
email,
|
||||
password,
|
||||
logIn,
|
||||
getCalculator,
|
||||
getComponents,
|
||||
getSections,
|
||||
|
105
stores/userStore.ts
Normal file
105
stores/userStore.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import type { GenericResponse } from "~/types";
|
||||
import type { Customer } from "~/types/user";
|
||||
|
||||
export const useUserStore = defineStore("userStore", () => {
|
||||
const store = useStore();
|
||||
|
||||
const fullUserData = ref<Customer | null>(null);
|
||||
const isLogged = ref<boolean>(true);
|
||||
const user = ref<string | null>(null);
|
||||
|
||||
async function checkIsLogged() {
|
||||
try {
|
||||
const { data } = await useMyFetch<
|
||||
GenericResponse<{ loggedin: boolean } | Customer>
|
||||
>(`/api/public/user`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
onErrorOccured: async (_, status) => {
|
||||
throw createError({
|
||||
statusCode: status,
|
||||
statusMessage: `HTTP error: ${status}`,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
if ("loggedin" in data && data.loggedin === true) {
|
||||
isLogged.value = true;
|
||||
user.value = null;
|
||||
fullUserData.value = null;
|
||||
} else if ("first_name" in data && "last_name" in data) {
|
||||
isLogged.value = true;
|
||||
user.value = `${data.first_name} ${data.last_name}`;
|
||||
fullUserData.value = data as Customer;
|
||||
} else {
|
||||
isLogged.value = false;
|
||||
user.value = null;
|
||||
fullUserData.value = null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("checkIsLogged error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// login
|
||||
const email = ref();
|
||||
const password = ref();
|
||||
const vLogin = ref<boolean>(true);
|
||||
const vCodeVerify = ref<boolean>(false);
|
||||
const vCode = ref<number | null>(null);
|
||||
const vEmail = ref<string>("");
|
||||
async function logIn() {
|
||||
try {
|
||||
const data = await useMyFetch<GenericResponse<object>>(
|
||||
`/api/public/user/session/start`,
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
mail: email.value,
|
||||
password: password.value,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
onErrorOccured: (_, status) => {
|
||||
throw new Error(`HTTP error: ${status}`);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (data.status === 200 || data.status === 201) {
|
||||
console.log(vCodeVerify.value);
|
||||
|
||||
// $toast.success("Address successfully added", {
|
||||
// autoClose: 5000,
|
||||
// dangerouslyHTMLString: true,
|
||||
// });
|
||||
vLogin.value = false;
|
||||
vCodeVerify.value = true;
|
||||
}
|
||||
// else {
|
||||
// $toast.error("Failed to add address. Please try again.", {
|
||||
// autoClose: 5000,
|
||||
// dangerouslyHTMLString: true,
|
||||
// });
|
||||
// }
|
||||
|
||||
store.minValue = data;
|
||||
} catch (error) {
|
||||
console.error("getList error:", error);
|
||||
}
|
||||
}
|
||||
return {
|
||||
isLogged,
|
||||
user,
|
||||
fullUserData,
|
||||
vCodeVerify,
|
||||
vCode,
|
||||
vEmail,
|
||||
email,
|
||||
password,
|
||||
logIn,
|
||||
checkIsLogged,
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user