94 lines
2.3 KiB
JavaScript
94 lines
2.3 KiB
JavaScript
import { ref, watch } from "vue";
|
|
|
|
import { searchCatalog } from "@/services/catalog-service.js";
|
|
|
|
export function useCatalogSearch({
|
|
initialQuery = "",
|
|
initialCategory = "",
|
|
initialConditionId = "",
|
|
initialSetName = "",
|
|
initialEventName = "",
|
|
initialTag = "",
|
|
initialMinPriceMinor,
|
|
initialMaxPriceMinor,
|
|
initialSort = "recent",
|
|
initialPage = 1,
|
|
} = {}) {
|
|
const query = ref(initialQuery);
|
|
const category = ref(initialCategory);
|
|
const conditionId = ref(initialConditionId);
|
|
const setName = ref(initialSetName);
|
|
const eventName = ref(initialEventName);
|
|
const tag = ref(initialTag);
|
|
const minPriceMinor = ref(initialMinPriceMinor);
|
|
const maxPriceMinor = ref(initialMaxPriceMinor);
|
|
const sort = ref(initialSort);
|
|
const page = ref(initialPage);
|
|
const result = ref({
|
|
items: [],
|
|
pagination: { page: 1, limit: 20, total: 0, totalPages: 0 },
|
|
});
|
|
const isLoading = ref(false);
|
|
const error = ref(null);
|
|
let requestId = 0;
|
|
|
|
async function load() {
|
|
const currentRequestId = ++requestId;
|
|
isLoading.value = true;
|
|
error.value = null;
|
|
|
|
try {
|
|
const nextResult = await searchCatalog({
|
|
query: query.value,
|
|
category: category.value,
|
|
conditionId: conditionId.value,
|
|
setName: setName.value,
|
|
eventName: eventName.value,
|
|
tag: tag.value,
|
|
minPriceMinor: minPriceMinor.value,
|
|
maxPriceMinor: maxPriceMinor.value,
|
|
sort: sort.value,
|
|
page: page.value,
|
|
});
|
|
if (currentRequestId === requestId) result.value = nextResult;
|
|
} catch (requestError) {
|
|
if (currentRequestId === requestId) error.value = requestError;
|
|
} finally {
|
|
if (currentRequestId === requestId) isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
watch(
|
|
[query, category, conditionId, setName, eventName, tag, minPriceMinor, maxPriceMinor, sort],
|
|
() => {
|
|
if (page.value !== 1) page.value = 1;
|
|
else load();
|
|
},
|
|
);
|
|
watch(page, load);
|
|
|
|
function goToPage(nextPage) {
|
|
if (nextPage >= 1 && nextPage <= result.value.pagination.totalPages) {
|
|
page.value = nextPage;
|
|
}
|
|
}
|
|
|
|
return {
|
|
query,
|
|
category,
|
|
conditionId,
|
|
setName,
|
|
eventName,
|
|
tag,
|
|
minPriceMinor,
|
|
maxPriceMinor,
|
|
sort,
|
|
page,
|
|
result,
|
|
isLoading,
|
|
error,
|
|
load,
|
|
goToPage,
|
|
};
|
|
}
|