forked from verdnatura/salix-front
perf: #7260 ItemsFilterPanel
This commit is contained in:
parent
ff9ff45cbc
commit
8e67a73d90
|
@ -9,6 +9,8 @@ import VnSelect from 'components/common/VnSelect.vue';
|
||||||
import VnFilterPanelChip from 'components/ui/VnFilterPanelChip.vue';
|
import VnFilterPanelChip from 'components/ui/VnFilterPanelChip.vue';
|
||||||
|
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { getParamWhere } from 'src/filters';
|
||||||
|
import { useRoute } from 'vue-router';
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
|
@ -26,28 +28,33 @@ const props = defineProps({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const itemCategories = ref([]);
|
const route = useRoute();
|
||||||
const selectedCategoryFk = ref(null);
|
|
||||||
const selectedTypeFk = ref(null);
|
|
||||||
const itemTypesOptions = ref([]);
|
const itemTypesOptions = ref([]);
|
||||||
const suppliersOptions = ref([]);
|
const suppliersOptions = ref([]);
|
||||||
const tagOptions = ref([]);
|
const tagOptions = ref([]);
|
||||||
const tagValues = ref([]);
|
const tagValues = ref([]);
|
||||||
|
const categoryList = ref(null);
|
||||||
|
const selectedCategoryFk = ref(getParamWhere(route.query.table, 'categoryFk', false));
|
||||||
|
const selectedTypeFk = ref(getParamWhere(route.query.table, 'typeFk', false));
|
||||||
|
// const selectedCategoryFk = ref(
|
||||||
|
// JSON.parse(route?.query?.table ?? '{}').categoryFk ?? null
|
||||||
|
// );
|
||||||
|
// const selectedTypeFk = ref(JSON.parse(route?.query?.table ?? '{}').typeFk ?? null);
|
||||||
|
// const categoryList = computed(() => {
|
||||||
|
// return (itemCategories.value || [])
|
||||||
|
// .filter((category) => category.display)
|
||||||
|
// .map((category) => ({
|
||||||
|
// ...category,
|
||||||
|
// icon: `vn:${(category.icon || '').split('-')[1]}`,
|
||||||
|
// }));
|
||||||
|
// });
|
||||||
|
|
||||||
const categoryList = computed(() => {
|
const selectedCategory = computed(() => {
|
||||||
return (itemCategories.value || [])
|
return (categoryList.value || []).find(
|
||||||
.filter((category) => category.display)
|
|
||||||
.map((category) => ({
|
|
||||||
...category,
|
|
||||||
icon: `vn:${(category.icon || '').split('-')[1]}`,
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
const selectedCategory = computed(() =>
|
|
||||||
(itemCategories.value || []).find(
|
|
||||||
(category) => category?.id === selectedCategoryFk.value
|
(category) => category?.id === selectedCategoryFk.value
|
||||||
)
|
);
|
||||||
);
|
});
|
||||||
|
|
||||||
const selectedType = computed(() => {
|
const selectedType = computed(() => {
|
||||||
return (itemTypesOptions.value || []).find(
|
return (itemTypesOptions.value || []).find(
|
||||||
|
@ -87,7 +94,7 @@ const applyTags = (params, search) => {
|
||||||
search();
|
search();
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchItemTypes = async (id) => {
|
const fetchItemTypes = async (id = selectedCategoryFk.value) => {
|
||||||
try {
|
try {
|
||||||
const filter = {
|
const filter = {
|
||||||
fields: ['id', 'name', 'categoryFk'],
|
fields: ['id', 'name', 'categoryFk'],
|
||||||
|
@ -134,15 +141,19 @@ const removeTag = (index, params, search) => {
|
||||||
(tagValues.value || []).splice(index, 1);
|
(tagValues.value || []).splice(index, 1);
|
||||||
applyTags(params, search);
|
applyTags(params, search);
|
||||||
};
|
};
|
||||||
|
const setCategoryList = (data) => {
|
||||||
|
categoryList.value = (data || [])
|
||||||
|
.filter((category) => category.display)
|
||||||
|
.map((category) => ({
|
||||||
|
...category,
|
||||||
|
icon: `vn:${(category.icon || '').split('-')[1]}`,
|
||||||
|
}));
|
||||||
|
fetchItemTypes();
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<FetchData
|
<FetchData url="ItemCategories" limit="30" auto-load @on-fetch="setCategoryList" />
|
||||||
url="ItemCategories"
|
|
||||||
limit="30"
|
|
||||||
auto-load
|
|
||||||
@on-fetch="(data) => (itemCategories = data)"
|
|
||||||
/>
|
|
||||||
<FetchData
|
<FetchData
|
||||||
url="Suppliers"
|
url="Suppliers"
|
||||||
limit="30"
|
limit="30"
|
||||||
|
@ -262,6 +273,7 @@ const removeTag = (index, params, search) => {
|
||||||
/>
|
/>
|
||||||
</QItemSection>
|
</QItemSection>
|
||||||
<QItemSection class="col">
|
<QItemSection class="col">
|
||||||
|
{{ value }}
|
||||||
<VnSelect
|
<VnSelect
|
||||||
v-if="!value?.selectedTag?.isFree && value.valueOptions"
|
v-if="!value?.selectedTag?.isFree && value.valueOptions"
|
||||||
:label="t('components.itemsFilterPanel.value')"
|
:label="t('components.itemsFilterPanel.value')"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// parsing JSON safely
|
|
||||||
function parseJSON(str, fallback) {
|
function parseJSON(str, fallback) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(str ?? '{}');
|
return JSON.parse(str ?? '{}');
|
||||||
|
@ -7,13 +6,15 @@ function parseJSON(str, fallback) {
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export default function (route, param) {
|
export default function (route, param, inFilter = true) {
|
||||||
// catch route query params
|
const params = parseJSON(route?.query?.params ?? route, {});
|
||||||
const params = parseJSON(route?.query?.params, {});
|
let where = null;
|
||||||
|
if (!inFilter) {
|
||||||
// extract and parse filter from params
|
where = params;
|
||||||
const { filter: filterStr = '{}' } = params;
|
} else {
|
||||||
const where = parseJSON(filterStr, {})?.where;
|
const { filter: filterStr = '{}' } = params;
|
||||||
|
where = parseJSON(filterStr, {})?.where;
|
||||||
|
}
|
||||||
if (where && where[param] !== undefined) {
|
if (where && where[param] !== undefined) {
|
||||||
return where[param];
|
return where[param];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue