Added a global `notify` method via Nuxt plugin to replace `useNotification`. Improved messaging structure by embedding progress bars and handled dynamic durations. Updated usage across composables and components for consistency. - Replaced `useNotification` with `$notify` in all applicable files. - Updated `app.config.ts` to support customizable notification positions. - Refactored affected composables for simplified notification calls. - Enhanced progress indicator display within notifications. Breaking Changes: `useNotification` is removed, requiring migration to the new `$notify` API.
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { SEARCH } from '@graphql/mutations/search';
|
|
import type { ISearchResponse, ISearchResults } from '@types';
|
|
|
|
export function useSearch() {
|
|
const { t } = useI18n();
|
|
const { $notify } = useNuxtApp();
|
|
|
|
const searchResults = ref<ISearchResults | null>(null);
|
|
|
|
const { mutate, loading, error } = useMutation<ISearchResponse>(SEARCH);
|
|
|
|
async function search(query: string) {
|
|
searchResults.value = null;
|
|
const result = await mutate({
|
|
query,
|
|
});
|
|
|
|
if (result?.data?.search) {
|
|
const limitedResults = {
|
|
brands: result.data.search.results.brands?.slice(0, 7) || [],
|
|
categories: result.data.search.results.categories?.slice(0, 7) || [],
|
|
posts: result.data.search.results.posts?.slice(0, 7) || [],
|
|
products: result.data.search.results.products?.slice(0, 7) || [],
|
|
};
|
|
|
|
searchResults.value = limitedResults;
|
|
return {
|
|
results: limitedResults,
|
|
};
|
|
}
|
|
}
|
|
|
|
watch(error, (err) => {
|
|
if (!err) return;
|
|
console.error('useSearch error:', err);
|
|
let message = t('popup.errors.defaultError');
|
|
if (isGraphQLError(err)) {
|
|
message = err.graphQLErrors?.[0]?.message || message;
|
|
} else {
|
|
message = err.message;
|
|
}
|
|
$notify({
|
|
message,
|
|
type: 'error',
|
|
title: t('popup.errors.main'),
|
|
});
|
|
});
|
|
|
|
return {
|
|
search,
|
|
loading,
|
|
searchResults,
|
|
};
|
|
}
|