- ottimizzato il caricamento del sito
- ottimizzato il caricamento del catalogo.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user