schon/storefront/app/composables/search/useSearch.ts
Alexandr SaVBaD Waltz 8d7685ef67 feat(notification): integrate global notification plugin using ElNotification
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.
2026-03-01 15:30:47 +03:00

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,
};
}