UNPKG

@nvs-pinia/collection

Version:

Frequently used Pinia modules at Netvlies

197 lines (189 loc) 6.05 kB
import { useContextStore, REQUEST_TOKEN } from '@nvs-pinia/common'; export { useApiSettings, useAuthentication, useContextStore } from '@nvs-pinia/common'; import { ref, computed } from 'vue'; import { defineStore } from 'pinia'; import { useLogger, DynamicURL } from '@netvlies/utility-collection'; // Vendor const useNavigation = defineStore('nav', () => { // Composable const { warn } = useLogger(BUNDLE_NAME); // Refs const registered = ref({}); // Computed const itemsByLocationCode = computed(() => (code) => code in registered.value ? registered.value[code] || [] : []); // Methods function addByName(name, payload, createIfNotExists = false) { var _a; const items = Array.isArray(payload) ? payload : [payload]; if (name in registered.value) { registered.value[name] = [...((_a = registered.value[name]) !== null && _a !== void 0 ? _a : []), ...items]; } else { createIfNotExists ? register(name, items) : warn('Cannot add items to non-existing navigation'); } } function deregister(name) { if (name in registered.value) { delete registered.value[name]; } } function register(name, items) { registered.value[name] = items; } function flushAll() { registered.value = {}; } function flushByName(name) { if (name in registered.value) { registered.value[name] = null; } } return { addByName, deregister, flushAll, flushByName, registered, itemsByLocationCode, register, }; }); var API_PREFIX; (function (API_PREFIX) { API_PREFIX["ADMIN"] = "admin"; API_PREFIX["SHOP"] = "shop"; })(API_PREFIX || (API_PREFIX = {})); // Vendor const useSyliusShopOrder = defineStore('order', () => { // Composable const { $api, $endpoints } = useContextStore(); // Refs const data = ref(null); const loaded = ref(false); // Computed const itemCount = computed(() => { var _a; return ((_a = data.value) === null || _a === void 0 ? void 0 : _a.items.length) || 0; }); // Methods async function fetchByToken(options) { const { token, tokenLevel = REQUEST_TOKEN.SECURE, updateState = true } = options; if (updateState) { data.value = null; loaded.value = true; } const url = new DynamicURL(`${API_PREFIX.SHOP}${$endpoints.ORDER_BY_TOKEN}`) .setRouteParams(token) .resolve(); const orderResponse = await $api .get(url, { tokenLevel, }) .catch((error) => Promise.reject(error)); const { data: responseData } = orderResponse; if (responseData) { if (updateState) { data.value = responseData; loaded.value = true; } return data; } return null; } return { data, fetchByToken, itemCount, loaded, }; }); // Vendor const useSyliusShopTaxonItem = defineStore('shopTaxonItem', () => { // Composable const { $api, $endpoints } = useContextStore(); // Refs const item = ref({ data: null, loaded: false, }); // Methods async function fetchByCode(options) { const { code, updateState = true } = options; if (updateState) { item.value.data = null; item.value.loaded = false; } const url = new DynamicURL(`${API_PREFIX.SHOP}${$endpoints.TAXON_BY_CODE}`).setRouteParams(`${code}`); const response = await $api .get(url.resolve()) .catch((error) => Promise.reject(error)); const { data } = response; if (data) { if (updateState) { item.value.data = data; item.value.loaded = false; } return data; } return null; } return { fetchByCode, item, }; }); // Vendor const useSyliusShopTaxonList = defineStore('shopTaxonList', () => { // Composable const { $api, $endpoints } = useContextStore(); const nav = useNavigation(); // Refs const items = ref({ limit: 0, loaded: false, page: 0, pages: 0, results: null, total: 0, }); // Methods async function fetchItems(options) { const { nav: navOptions, updateState = true } = options || {}; if (updateState) { items.value.loaded = false; items.value.results = []; } const response = await $api .get(`${API_PREFIX.SHOP}${$endpoints.TAXONS}`) .catch((error) => Promise.reject(error)); const { data } = response; if (data) { if (updateState) { items.value.results = data; items.value.loaded = true; } if (navOptions) { const _navOptions = typeof navOptions === 'boolean' ? { location: 'taxon-menu', routeBase: { name: 'taxon-child', }, } : navOptions; nav.register(_navOptions.location, data.map((taxon) => ({ label: `${taxon.name}`, link: Object.assign(Object.assign({}, _navOptions.routeBase), { params: Object.assign(Object.assign({}, _navOptions.routeBase.params), { [_navOptions.taxonCodeParam || 'taxonCode']: taxon.code }) }), }))); } return data; } return null; } return { fetchItems, items, }; }); const BUNDLE_NAME = '🍍 NVS Pinia Collection'; export { BUNDLE_NAME, useNavigation, useSyliusShopOrder, useSyliusShopTaxonItem, useSyliusShopTaxonList }; //# sourceMappingURL=index.esm.js.map