2024-03-01 18:08:14 +00:00
|
|
|
import type { ItemSummary, LabelSummary, LocationSummary } from "~~/lib/api/types/data-contracts";
|
|
|
|
import type { UserClient } from "~~/lib/api/user";
|
2022-10-24 04:54:39 +00:00
|
|
|
|
|
|
|
type SearchOptions = {
|
|
|
|
immediate?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function useItemSearch(client: UserClient, opts?: SearchOptions) {
|
|
|
|
const query = ref("");
|
|
|
|
const locations = ref<LocationSummary[]>([]);
|
|
|
|
const labels = ref<LabelSummary[]>([]);
|
|
|
|
const results = ref<ItemSummary[]>([]);
|
2022-11-01 07:30:42 +00:00
|
|
|
const includeArchived = ref(false);
|
2022-10-24 04:54:39 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-11-01 07:30:42 +00:00
|
|
|
const { data, error } = await client.items.getAll({
|
|
|
|
q: query.value,
|
|
|
|
locations: locIds,
|
|
|
|
labels: labelIds,
|
|
|
|
includeArchived: includeArchived.value,
|
|
|
|
});
|
2022-10-24 04:54:39 +00:00
|
|
|
if (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
results.value = data.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts?.immediate) {
|
|
|
|
search();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
query,
|
|
|
|
results,
|
|
|
|
locations,
|
|
|
|
labels,
|
|
|
|
};
|
|
|
|
}
|