From b8374d8cc8c166fae896ed8a0564a5c05ead1c79 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 23 Oct 2022 19:47:29 -0800 Subject: [PATCH] extract search functionality --- frontend/components/Form/Autocomplete.vue | 149 ++++++++++++++++++++++ frontend/composables/use-item-search.ts | 36 ++++++ frontend/pages/item/[id]/edit.vue | 21 ++- frontend/pages/item/[id]/index.vue | 32 ++++- 4 files changed, 231 insertions(+), 7 deletions(-) create mode 100644 frontend/components/Form/Autocomplete.vue create mode 100644 frontend/composables/use-item-search.ts diff --git a/frontend/components/Form/Autocomplete.vue b/frontend/components/Form/Autocomplete.vue new file mode 100644 index 0000000..28dd6f3 --- /dev/null +++ b/frontend/components/Form/Autocomplete.vue @@ -0,0 +1,149 @@ + + + diff --git a/frontend/composables/use-item-search.ts b/frontend/composables/use-item-search.ts new file mode 100644 index 0000000..9944edd --- /dev/null +++ b/frontend/composables/use-item-search.ts @@ -0,0 +1,36 @@ +import { ItemSummary, LabelSummary, LocationSummary } from "~~/lib/api/types/data-contracts"; +import { UserClient } from "~~/lib/api/user"; + +type SearchOptions = { + immediate?: boolean; +}; + +export function useItemSearch(client: UserClient, opts?: SearchOptions) { + const query = ref(""); + const locations = ref([]); + const labels = ref([]); + const results = ref([]); + + watchDebounced(query, search, { debounce: 250, maxWait: 1000 }); + async function search() { + const locIds = locations.value.map(l => l.id); + const labelIds = labels.value.map(l => l.id); + + const { data, error } = await client.items.getAll({ q: query.value, locations: locIds, labels: labelIds }); + if (error) { + return; + } + results.value = data.items; + } + + if (opts?.immediate) { + search(); + } + + return { + query, + results, + locations, + labels, + }; +} diff --git a/frontend/pages/item/[id]/edit.vue b/frontend/pages/item/[id]/edit.vue index d5930d5..48b3123 100644 --- a/frontend/pages/item/[id]/edit.vue +++ b/frontend/pages/item/[id]/edit.vue @@ -4,6 +4,7 @@ import { useLabelStore } from "~~/stores/labels"; import { useLocationStore } from "~~/stores/locations"; import { capitalize } from "~~/lib/strings"; + import Autocomplete from "~~/components/Form/Autocomplete.vue"; definePageMeta({ middleware: ["auth"], @@ -37,6 +38,10 @@ } } + if (data.parent) { + parent.value = data.parent; + } + return data; }); @@ -49,7 +54,7 @@ ...item.value, locationId: item.value.location?.id, labelIds: item.value.labels.map(l => l.id), - parentId: null, + parentId: parent.value ? parent.value.id : null, }; const { error } = await api.items.update(itemId.value, payload); @@ -258,7 +263,6 @@ async function updateAttachment() { editState.loading = true; - console.log(editState.type); const { error, data } = await api.items.updateAttachment(itemId.value, editState.id, { title: editState.title, type: editState.type, @@ -308,6 +312,9 @@ timeValue: null, }); } + + const { query, results } = useItemSearch(api, { immediate: false }); + const parent = ref();