Whole site refactor. Added pinia stores and authentication capability.
This commit is contained in:
27
src/stores/categories.store.ts
Normal file
27
src/stores/categories.store.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Category } from '@/main'
|
||||
import { ref } from 'vue'
|
||||
import { axiosInstance } from '@/main'
|
||||
|
||||
export const useCategoriesStore = defineStore('categories', () => {
|
||||
const categories = ref<Array<Category>>([]);
|
||||
|
||||
async function fetchCategories() {
|
||||
const response = await axiosInstance.get('/towary', {withCredentials: true});
|
||||
categories.value = response.data;
|
||||
|
||||
if(categories.value != undefined) {
|
||||
for (const category of categories.value) {
|
||||
for (const product of category.Towary) {
|
||||
product.Options = new Array(product.Twr_JM);
|
||||
product.ChosenOption = product.Twr_JM;
|
||||
if (product.Twr_JMZ != null) {
|
||||
product.Options.push(product.Twr_JMZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {categories, fetchCategories}
|
||||
})
|
||||
21
src/stores/contractors.store.ts
Normal file
21
src/stores/contractors.store.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Contractor } from '@/main'
|
||||
import { ref } from 'vue'
|
||||
import { axiosInstance } from '@/main'
|
||||
|
||||
|
||||
export const useContractorsStore = defineStore('contractors', () => {
|
||||
const contractors = ref<Array<Contractor>>([]);
|
||||
contractors.value=[];
|
||||
const contractor = ref<Contractor>();
|
||||
|
||||
async function fetchContractors() {
|
||||
const response = await axiosInstance.get('/kontrahenci', {withCredentials: true});
|
||||
console.log(response);
|
||||
console.log(contractors.value);
|
||||
contractors.value = [];
|
||||
contractors.value.push(...response.data);
|
||||
}
|
||||
|
||||
return {contractors, contractor, fetchContractors}
|
||||
})
|
||||
71
src/stores/orders.store.ts
Normal file
71
src/stores/orders.store.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Category, Contractor, Order } from '@/main'
|
||||
import { type Ref, ref } from 'vue'
|
||||
import { axiosInstance } from '@/main'
|
||||
|
||||
export const useOrdersStore = defineStore('orders', () => {
|
||||
const orders = ref<Array<Order>>([]);
|
||||
const order = ref<Order>();
|
||||
const uuid = ref<string>();
|
||||
const deliveryDate = ref<Date>();
|
||||
const orderDate = ref<Date>();
|
||||
|
||||
async function fetchOrders() {
|
||||
orders.value=[];
|
||||
let ordersTemp : Array<Order> = await fetchOrdersInBuffer();
|
||||
orders.value.push(...ordersTemp);
|
||||
ordersTemp = await fetchOrdersOutOfBuffer();
|
||||
orders.value.push(...ordersTemp);
|
||||
}
|
||||
|
||||
async function fetchOrdersOutOfBuffer() : Promise<Array<Order>> {
|
||||
const response = await axiosInstance.get('/zamowienia', {withCredentials: true});
|
||||
const ordersTemp : Array<Order> = response.data;
|
||||
return ordersTemp;
|
||||
}
|
||||
|
||||
async function fetchOrdersInBuffer() : Promise<Array<Order>> {
|
||||
const response = await axiosInstance.get('/zamowienia/bufor', {withCredentials: true});
|
||||
const ordersTemp : Array<Order> = response.data;
|
||||
return ordersTemp;
|
||||
}
|
||||
|
||||
async function fetchOrder(uuid : string) {
|
||||
|
||||
}
|
||||
|
||||
async function loadOrder(uuidString: string, confirmed: boolean, contractor: Ref<Contractor|undefined>, contractors: Ref<Array<Contractor>>, categories: Ref<Array<Category>>) {
|
||||
const response = await axiosInstance.get('/zamowienie/' + uuidString);
|
||||
const tempOrder = response.data;
|
||||
|
||||
console.log(tempOrder);
|
||||
|
||||
if(confirmed) {
|
||||
tempOrder.MZN_Bufor = 0;
|
||||
}
|
||||
|
||||
contractor.value = <Contractor>contractors.value?.find((contractor) => contractor.Knt_KntId == tempOrder.MZN_PodID);
|
||||
deliveryDate.value = new Date(tempOrder.MZN_DataDos);
|
||||
orderDate.value = new Date(tempOrder.MZN_DataZam);
|
||||
uuid.value = uuidString;
|
||||
order.value = tempOrder;
|
||||
|
||||
if(categories.value == undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(const orderProduct of tempOrder.MZamElem){
|
||||
for(const category of categories.value) {
|
||||
const product = category.Towary.find(product => (product.Twr_TwrId == orderProduct.MZE_TwrId));
|
||||
if(product != undefined && orderProduct.MZE_TwrCena != null) {
|
||||
product.Twr_Cena = orderProduct.MZE_TwrCena.slice(0, -2);
|
||||
product.Quantity = orderProduct.MZE_TwrIlosc.slice(0, -2);
|
||||
category.isVisible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {orders, order, uuid, deliveryDate, orderDate, fetchOrders, fetchOrdersInBuffer, fetchOrdersOutOfBuffer, loadOrder}
|
||||
})
|
||||
55
src/stores/siteControl.store.ts
Normal file
55
src/stores/siteControl.store.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { defineStore, storeToRefs } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useOrdersStore } from '@/stores/orders.store'
|
||||
import type { Order } from '@/main'
|
||||
import { useContractorsStore } from '@/stores/contractors.store'
|
||||
import { useCategoriesStore } from '@/stores/categories.store'
|
||||
|
||||
export const useSiteControlStore = defineStore('siteControl', () => {
|
||||
const isForm = ref<boolean>(true);
|
||||
const isOrders = ref<boolean>(false);
|
||||
const showConfirmationModal = ref<boolean>(false);
|
||||
const isDarkTheme = ref<boolean>(false);
|
||||
const isLoading = ref<boolean>(true);
|
||||
|
||||
function switchToFrom() {
|
||||
if(!isForm.value) {
|
||||
isForm.value = true;
|
||||
isOrders.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function switchToOrders() {
|
||||
if(!isOrders.value) {
|
||||
const orderStore = useOrdersStore();
|
||||
const { orders } = storeToRefs(orderStore);
|
||||
isLoading.value = true;
|
||||
isForm.value = false;
|
||||
isOrders.value = true;
|
||||
orders.value = new Array<Order>();
|
||||
orders.value.push(... await orderStore.fetchOrdersInBuffer());
|
||||
orders.value.push(... await orderStore.fetchOrdersOutOfBuffer());
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function checkTheme() {
|
||||
isDarkTheme.value = !!window?.matchMedia?.('(prefers-color-scheme:dark)')?.matches;
|
||||
}
|
||||
|
||||
async function viewOrder(uuid : string) {
|
||||
const orderStore = useOrdersStore();
|
||||
const contractorsStore = useContractorsStore();
|
||||
const categoriesStore = useCategoriesStore();
|
||||
isForm.value = true;
|
||||
isOrders.value = false;
|
||||
isLoading.value = true;
|
||||
window.scrollTo(0, 0);
|
||||
const { contractor, contractors } = storeToRefs(contractorsStore);
|
||||
const { categories } = storeToRefs(categoriesStore);
|
||||
await orderStore.loadOrder(uuid, false, contractor, contractors, categories);
|
||||
isLoading.value=false;
|
||||
}
|
||||
|
||||
return {isForm, isOrders, isLoading, showConfirmationModal, isDarkTheme, switchToFrom, switchToOrders, checkTheme, viewOrder};
|
||||
})
|
||||
Reference in New Issue
Block a user