- ottimizzato il caricamento del sito

- ottimizzato il caricamento del catalogo.
This commit is contained in:
Surya Paolo
2025-05-15 18:22:37 +02:00
parent 50015712f5
commit 9e0634da05
17 changed files with 410565 additions and 143 deletions

View File

@@ -1,41 +1,103 @@
import { defineStore } from 'pinia'
import { defineStore } from 'pinia';
import type {
ICatalogState
} from '@src/model';
import {
IAccount,
ICircuit, ICatalog, IGlobalState, IGroupShort, IMyCircuit, IMyGroup, IUserFields
} from '@src/model'
import { tools } from '@tools'
import translate from '@src/globalroutines/util'
import type { ICatalogState } from '@src/model';
import { IAccount, ICircuit, ICatalog, IGlobalState, IGroupShort, IMyCircuit, IMyGroup, IUserFields } from '@src/model';
import { tools } from '@tools';
import translate from '@src/globalroutines/util';
import * as Types from '@src/store/Api/ApiTypes'
import { useGlobalStore } from '@store/globalStore'
import { serv_constants } from '@store/Modules/serv_constants'
import { Api } from '@api'
import { toolsext } from '@store/Modules/toolsext'
import { static_data } from '@src/db/static_data'
import { useProducts } from 'app/src/store/Products';
import { useUserStore } from 'app/src/store/UserStore';
import * as Types from '@src/store/Api/ApiTypes';
import { useGlobalStore } from '@store/globalStore';
import { serv_constants } from '@store/Modules/serv_constants';
import { Api } from '@api';
import { toolsext } from '@store/Modules/toolsext';
import { static_data } from '@src/db/static_data';
import { shared_consts } from '@src/common/shared_vuejs'
import { costanti } from '@costanti'
import { shared_consts } from '@src/common/shared_vuejs';
import { costanti } from '@costanti';
import globalroutines from '../globalroutines/index'
import globalroutines from '../globalroutines/index';
export const useCatalogStore = defineStore('CatalogStore', {
state: (): ICatalogState => ({
catalogs: [{ _id: '', idapp: '', title: '' }]
catalogs: [{ _id: '', idapp: '', title: '' }],
}),
getters: {
getCatalogById: (state) => (id: string) => {
return state.catalogs.find((cat: ICatalog) => cat._id === id);
},
getCatalogById: (state) => async (id: string) => {
// carica in memoria il catalogo singolo
// se non lo trovo, allora lo carica dal server
let cat = state.catalogs.find((cat: ICatalog) => cat._id === id);
if ((cat && !cat.prodotti_caricati_inmem) || !cat) {
// Lo carica dal server
cat = await this.loadCatalogById(id);
}
return cat;
},
},
actions: {
async loadCatalogById(id: string) {
const userStore = useUserStore();
const globalStore = useGlobalStore();
let ris = null;
ris = await Api.SendReq('/catalogs/id/' + id, 'GET', null)
.then((res) => {
console.log('catalogs', res.data.catalog);
if (res.data.catalog) {
// console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
this.updateDataCatalog(res);
return res.data.product;
} else {
return null;
}
})
.catch((error) => {
console.log('error loadCatalogById', error);
userStore.setErrorCatch(error);
return new Types.AxiosError(serv_constants.RIS_CODE_ERR, null, toolsext.ERR_GENERICO, error);
});
return ris;
},
async loadProductsOnlyByIdPageCatalog(idPage: string, forzacaricamento: boolean = false) {
// controlla se è stata già caricata in memoria
const productStore = useProducts();
const reccat = this.catalogs.find((cat: ICatalog) => cat.idPageAssigned === idPage);
if (reccat) {
const mycat = await this.loadCatalogById(reccat._id);
if (mycat) {
// Aggiorna la lista products con questo array del server "reccat.lista_prodotti"
productStore.updateProductsByArray(mycat.lista_prodotti);
}
return reccat.lista_prodotti;
} else {
return [];
}
},
updateDataCatalog(res: any) {
if (res && res.data.catalog) {
// Update catalog from server
const indelem = this.catalogs.findIndex((catalog: ICatalogCompleto) => catalog._id === res.data.catalog._id);
if (indelem >= 0) {
this.catalogs[indelem] = { ...res.data.catalog };
this.catalogs[indelem].prodotti_caricati_inmem = true;
}
}
},
},
})
});

View File

@@ -108,24 +108,24 @@ function getRecordProductEmpty(): IProduct {
bookableAvailableQty: 0,
minBuyQty: 1,
minStepQty: 1,
maxBookableSinglePersQty: 0,
// maxBookableSinglePersQty: 0,
stockQty: 0,
stockBloccatiQty: 0,
bookedQtyOrdered: 0,
bookedQtyConfirmed: 0,
// stockBloccatiQty: 0,
// bookedQtyOrdered: 0,
// bookedQtyConfirmed: 0,
qtyToReachForGas: 0,
// qtyToReachForGas: 0,
maxbookableGASQty: 0,
bookedGASQtyOrdered: 0,
bookedGASQtyConfirmed: 0,
bookableGASBloccatiQty: 0,
// maxbookableGASQty: 0,
// bookedGASQtyOrdered: 0,
// bookedGASQtyConfirmed: 0,
// bookableGASBloccatiQty: 0,
canBeShipped: false,
QuantitaOrdinateInAttesa: 0,
QuantitaPrenotateInAttesa: 0,
canBeBuyOnline: false,
// canBeShipped: false,
// QuantitaOrdinateInAttesa: 0,
// QuantitaPrenotateInAttesa: 0,
// canBeBuyOnline: false,
};
}
@@ -978,19 +978,22 @@ export const useProducts = defineStore('Products', {
return ris;
},
async loadProductById(id: string) {
async loadProductById(id: string, forzacaricamento?: boolean) {
const userStore = useUserStore();
const globalStore = useGlobalStore();
//if (!globalStore.site.confpages.enableEcommerce)
// return null
// if (this.userActive._id === '') {
// return new Types.AxiosError(0, null, 0, '')
// }
let ris = null;
let myprod = null;
if (this.products && !forzacaricamento) {
// cerca su this.products
myprod = this.products.find((prod: IProduct) => prod._id === id);
if (myprod) {
return myprod
}
}
ris = await Api.SendReq('/products/id/' + id, 'GET', null)
.then((res) => {
console.log('product', res.data.product);
@@ -1932,9 +1935,9 @@ export const useProducts = defineStore('Products', {
arr.push({ label: mylabel, value: myelem._id });
}
};
}
return arr
return arr;
},
getOptCatalogoPrintTemplate() {
// Ottieni l'array delle pagine che in cataloghi sono dimensioni_def.isTemplate
@@ -1958,9 +1961,9 @@ export const useProducts = defineStore('Products', {
arr.push({ label: mylabel, value: myelem._id });
}
};
}
return arr
return arr;
},
getSchedeOpt(arrschede: ISchedaSingola[], tag?: string): any[] {
@@ -2159,6 +2162,14 @@ export const useProducts = defineStore('Products', {
});
},
updateProductsByArray: (state: IProductsState) => (arrrec: IProduct[]) => {
if (arrrec && arrrec.length > 0) {
// update products array from this array
const myarr = state.products.filter((rec) => !arrrec.find((newrec) => newrec._id === rec._id));
state.products = myarr.concat(arrrec);
}
},
getPathByPage(idpag: string) {
let linkpage = '';