113 lines
3.7 KiB
TypeScript
Executable File
113 lines
3.7 KiB
TypeScript
Executable File
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 { 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 globalroutines from '../globalroutines/index';
|
|
|
|
export const useCatalogStore = defineStore('CatalogStore', {
|
|
state: (): ICatalogState => ({
|
|
catalogs: [{ _id: '', idapp: '', title: '' }],
|
|
}),
|
|
|
|
getters: {
|
|
getCatalogById: (state) => (id: string): ICatalog => {
|
|
return state.catalogs.find((cat: ICatalog) => cat._id === id) || null;
|
|
},
|
|
|
|
getCatalogsList: (state) => (): {label: string, value: string}[] => {
|
|
return state.catalogs.map((cat: ICatalog) => {
|
|
return {label: cat.title, value: cat._id};
|
|
});
|
|
},
|
|
getCatalogByIdPageAssigned: (state) => (idPage: string): ICatalog => {
|
|
return state.catalogs.find((cat: ICatalog) => cat.idPageAssigned === idPage) || null;
|
|
},
|
|
},
|
|
|
|
actions: {
|
|
async fetchCatalogById(id: string) {
|
|
// Controlla se è già in memoria
|
|
let cat = this.getCatalogById(id);
|
|
if (!cat || !cat.prodotti_caricati_inmem) {
|
|
// Carica dal server
|
|
const catalog = await this.loadCatalogById(id);
|
|
if (catalog) {
|
|
// Aggiorna lo store con i dati ricevuti
|
|
this.updateDataCatalog(catalog);
|
|
cat = this.getCatalogById(id);
|
|
}
|
|
}
|
|
return cat;
|
|
},
|
|
|
|
async loadCatalogById(id: string) {
|
|
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)
|
|
return res.data.catalog;
|
|
}
|
|
return null;
|
|
})
|
|
.catch((error) => {
|
|
console.log('error loadCatalogById', 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(catalog: ICatalog) {
|
|
if (catalog) {
|
|
// Update catalog from server
|
|
const indelem = this.catalogs.findIndex((reccatalog: ICatalogCompleto) => reccatalog._id === catalog._id);
|
|
if (indelem >= 0) {
|
|
this.catalogs[indelem] = { ...catalog };
|
|
this.catalogs[indelem].prodotti_caricati_inmem = true;
|
|
}
|
|
}
|
|
},
|
|
},
|
|
});
|