From 5745a5c0dc61d55e05cded64ed0e935f9bb01070 Mon Sep 17 00:00:00 2001 From: Ahmed Awan Date: Mon, 22 Apr 2024 14:51:24 -0500 Subject: [PATCH] remove `FilterMenuQuotaSource` and use `FilterMenuDropdown` instead --- client/src/components/Common/FilterMenu.vue | 13 +-- .../components/Common/FilterMenuDropdown.vue | 94 ++++++++++++---- .../Common/FilterMenuQuotaSource.vue | 101 ------------------ client/src/utils/filtering.ts | 2 +- 4 files changed, 80 insertions(+), 130 deletions(-) delete mode 100644 client/src/components/Common/FilterMenuQuotaSource.vue diff --git a/client/src/components/Common/FilterMenu.vue b/client/src/components/Common/FilterMenu.vue index 9fc5982054a9..0f0596e2f422 100644 --- a/client/src/components/Common/FilterMenu.vue +++ b/client/src/components/Common/FilterMenu.vue @@ -15,7 +15,6 @@ import FilterMenuDropdown from "@/components/Common/FilterMenuDropdown.vue"; import FilterMenuInput from "@/components/Common/FilterMenuInput.vue"; import FilterMenuMultiTags from "@/components/Common/FilterMenuMultiTags.vue"; import FilterMenuObjectStore from "@/components/Common/FilterMenuObjectStore.vue"; -import FilterMenuQuotaSource from "@/components/Common/FilterMenuQuotaSource.vue"; import FilterMenuRanged from "@/components/Common/FilterMenuRanged.vue"; library.add(faAngleDoubleUp, faQuestion, faSearch); @@ -260,15 +259,11 @@ function updateFilterText(newFilterText: string) { :filter="getValidFilter(filter)" :filters="filters" @change="onOption" /> - ; + +type FilterValue = QuotaUsageUnwrapped | string | boolean | undefined; + type DatalistItem = { value: string; text: string }; interface Props { + type?: FilterType; name: string; error?: string; filter: ValidFilter; filters: { - [k: string]: FilterType; + [k: string]: FilterValue; }; identifier: string; } @@ -26,12 +36,25 @@ interface Props { const props = defineProps(); const emit = defineEmits<{ - (e: "change", name: string, value: FilterType): void; + (e: "change", name: string, value: FilterValue): void; }>(); -const propValue = computed(() => props.filters[props.name]); +const propValue = computed(() => props.filters[props.name]); -const localValue = ref(propValue.value); +const localValue = ref(propValue.value); + +watch( + () => localValue.value, + () => { + emit("change", props.name, localValue.value); + } +); +watch( + () => propValue.value, + () => { + localValue.value = propValue.value; + } +); // datalist refs const datalist = computed<(DatalistItem[] | string[]) | undefined>(() => props.filter.datalist); @@ -56,20 +79,39 @@ function onHelp(_: string, value: string) { localValue.value = value; } -watch( - () => localValue.value, - () => { - emit("change", props.name, localValue.value); +// Quota Source refs and operations +const quotaUsages = ref([] as QuotaUsage[]); +const errorMessage = ref(); +async function loadQuotaUsages() { + try { + quotaUsages.value = await fetch(); + + // if the propValue is a string, find the corresponding QuotaUsage object and update the localValue + if (propValue.value && typeof propValue.value === "string") { + localValue.value = quotaUsages.value.find( + (quotaUsage) => props.filter.handler.converter!(quotaUsage) === propValue.value + ); + } + } catch (e) { + errorMessage.value = errorMessageAsString(e); } -); -watch( - () => propValue.value, - () => { - localValue.value = propValue.value; +} +const hasMultipleQuotaSources = computed(() => { + return !!(quotaUsages.value && quotaUsages.value.length > 1); +}); +onMounted(async () => { + if (props.type === "QuotaSource") { + await loadQuotaUsages(); } -); +}); +function isQuotaUsageVal(value: FilterValue): value is QuotaUsageUnwrapped { + return !!(value && value instanceof Object && "rawSourceLabel" in value); +} const dropDownText = computed(() => { + if (props.type === "QuotaSource" && isQuotaUsageVal(localValue.value)) { + return localValue.value.sourceLabel; + } if (localValue.value) { const stringMatch = stringDatalist.value.find((item) => item === localValue.value); const objectMatch = objectDatalist.value.find((item) => item.value === localValue.value); @@ -82,13 +124,13 @@ const dropDownText = computed(() => { return "(any)"; }); -function setValue(val: string | undefined) { +function setValue(val: string | QuotaUsage | undefined) { localValue.value = val; }