2022-10-13 05:13:07 +00:00
|
|
|
<script setup lang="ts">
|
2022-11-02 05:58:46 +00:00
|
|
|
import { watchPostEffect } from "vue";
|
|
|
|
import { ItemSummary, LabelSummary, LocationOutCount } from "~~/lib/api/types/data-contracts";
|
2022-10-13 05:13:07 +00:00
|
|
|
import { useLabelStore } from "~~/stores/labels";
|
|
|
|
import { useLocationStore } from "~~/stores/locations";
|
|
|
|
|
|
|
|
definePageMeta({
|
2022-10-15 20:15:55 +00:00
|
|
|
middleware: ["auth"],
|
2022-10-13 05:13:07 +00:00
|
|
|
});
|
2022-10-15 20:15:55 +00:00
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
useHead({
|
|
|
|
title: "Homebox | Home",
|
|
|
|
});
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
const searchLocked = ref(false);
|
2022-10-13 05:13:07 +00:00
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
const api = useUserApi();
|
2022-10-13 05:13:07 +00:00
|
|
|
const loading = useMinLoader(2000);
|
|
|
|
const results = ref<ItemSummary[]>([]);
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
const query = useRouteQuery("q", "");
|
|
|
|
const advanced = useRouteQuery("advanced", false);
|
|
|
|
const includeArchived = useRouteQuery("archived", false);
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
async function search() {
|
2022-11-02 05:58:46 +00:00
|
|
|
if (searchLocked.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
loading.value = true;
|
|
|
|
|
|
|
|
const locations = selectedLocations.value.map(l => l.id);
|
|
|
|
const labels = selectedLabels.value.map(l => l.id);
|
|
|
|
|
2022-11-01 07:30:42 +00:00
|
|
|
const { data, error } = await api.items.getAll({
|
2022-11-02 05:59:57 +00:00
|
|
|
q: query.value || "",
|
2022-11-01 07:30:42 +00:00
|
|
|
locations,
|
|
|
|
labels,
|
|
|
|
includeArchived: includeArchived.value,
|
|
|
|
});
|
2022-10-13 05:13:07 +00:00
|
|
|
if (error) {
|
|
|
|
loading.value = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.value = data.items;
|
|
|
|
loading.value = false;
|
|
|
|
}
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
const route = useRoute();
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const queryParamsInitialized = ref(false);
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
// Wait until locations and labels are loaded
|
|
|
|
let maxRetry = 10;
|
|
|
|
while (!labels.value || !locations.value) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
if (maxRetry-- < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
searchLocked.value = true;
|
|
|
|
const qLoc = route.query.loc as string[];
|
|
|
|
if (qLoc) {
|
|
|
|
selectedLocations.value = locations.value.filter(l => qLoc.includes(l.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
const qLab = route.query.lab as string[];
|
|
|
|
if (qLab) {
|
|
|
|
selectedLabels.value = labels.value.filter(l => qLab.includes(l.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
queryParamsInitialized.value = true;
|
|
|
|
searchLocked.value = false;
|
|
|
|
|
|
|
|
// trigger search if no changes
|
|
|
|
if (!qLab && !qLoc) {
|
|
|
|
search();
|
|
|
|
}
|
2022-10-13 05:13:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const locationsStore = useLocationStore();
|
2022-11-02 19:54:43 +00:00
|
|
|
const locations = computed(() => locationsStore.allLocations);
|
2022-10-13 05:13:07 +00:00
|
|
|
|
|
|
|
const labelStore = useLabelStore();
|
|
|
|
const labels = computed(() => labelStore.labels);
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
const selectedLocations = ref<LocationOutCount[]>([]);
|
|
|
|
const selectedLabels = ref<LabelSummary[]>([]);
|
|
|
|
|
|
|
|
watchPostEffect(() => {
|
|
|
|
if (!queryParamsInitialized.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const labelIds = selectedLabels.value.map(l => l.id);
|
|
|
|
router.push({
|
|
|
|
query: {
|
|
|
|
...router.currentRoute.value.query,
|
|
|
|
lab: labelIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
watchPostEffect(() => {
|
|
|
|
if (!queryParamsInitialized.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const locIds = selectedLocations.value.map(l => l.id);
|
|
|
|
router.push({
|
|
|
|
query: {
|
|
|
|
...router.currentRoute.value.query,
|
|
|
|
loc: locIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2022-10-13 05:13:07 +00:00
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
if (!advanced.value) {
|
|
|
|
selectedLocations.value = [];
|
|
|
|
selectedLabels.value = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-02 05:58:46 +00:00
|
|
|
watchDebounced([selectedLocations, selectedLabels, query], search, { debounce: 250, maxWait: 1000 });
|
2022-11-01 07:30:42 +00:00
|
|
|
watch(includeArchived, search);
|
2022-10-13 05:13:07 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<BaseContainer class="mb-16">
|
|
|
|
<FormTextField v-model="query" placeholder="Search" />
|
|
|
|
<div class="flex mt-1">
|
|
|
|
<label class="ml-auto label cursor-pointer">
|
|
|
|
<input v-model="advanced" type="checkbox" class="toggle toggle-primary" />
|
2022-12-30 01:19:15 +00:00
|
|
|
<span class="label-text text-base-content ml-2"> Filters </span>
|
2022-10-13 05:13:07 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<BaseCard v-if="advanced" class="my-1 overflow-visible">
|
|
|
|
<template #title> Filters </template>
|
|
|
|
<template #subtitle>
|
|
|
|
Location and label filters use the 'OR' operation. If more than one is selected only one will be required for a
|
|
|
|
match
|
|
|
|
</template>
|
|
|
|
<div class="px-4 pb-4">
|
|
|
|
<FormMultiselect v-model="selectedLabels" label="Labels" :items="labels ?? []" />
|
2022-10-14 16:30:23 +00:00
|
|
|
<FormMultiselect v-model="selectedLocations" label="Locations" :items="locations ?? []" />
|
2022-11-01 07:30:42 +00:00
|
|
|
<div class="flex pb-2 pt-5">
|
|
|
|
<label class="label cursor-pointer mr-auto">
|
|
|
|
<input v-model="includeArchived" type="checkbox" class="toggle toggle-primary" />
|
|
|
|
<span class="label-text ml-4"> Include Archived Items </span>
|
|
|
|
</label>
|
|
|
|
<Spacer />
|
|
|
|
</div>
|
2022-10-13 05:13:07 +00:00
|
|
|
</div>
|
|
|
|
</BaseCard>
|
|
|
|
<section class="mt-10">
|
|
|
|
<BaseSectionHeader class="mb-5"> Items </BaseSectionHeader>
|
|
|
|
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
|
|
<TransitionGroup name="list">
|
|
|
|
<ItemCard v-for="item in results" :key="item.id" :item="item" />
|
|
|
|
</TransitionGroup>
|
|
|
|
<div class="hidden first:inline text-xl">No Items Found</div>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</BaseContainer>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="css">
|
|
|
|
.list-move,
|
|
|
|
.list-enter-active,
|
|
|
|
.list-leave-active {
|
|
|
|
transition: all 0.25s ease;
|
|
|
|
}
|
|
|
|
|
|
|
|
.list-enter-from,
|
|
|
|
.list-leave-to {
|
|
|
|
opacity: 0;
|
|
|
|
transform: translateY(30px);
|
|
|
|
}
|
|
|
|
|
|
|
|
.list-leave-active {
|
|
|
|
position: absolute;
|
|
|
|
}
|
|
|
|
</style>
|