schon/storefront/composables/auth/useRefresh.ts
Alexandr SaVBaD Waltz 52b32bd608 Features: 1) Introduce useUserBaseData composable to fetch and manage user's wishlist, orders, and promocodes; 2) Add reusable useOrders and useOrderOverwrite composables with advanced filtering and pagination; 3) Implement order.vue component for detailed order displays with UI enhancements;
Fixes: 1) Replace deprecated context usage in `useAvatarUpload` mutation; 2) Resolve incorrect locale parsing in `useDate` utility and fix non-reactive cart state in `profile/cart.vue`; 3) Update stale imports and standardize type naming across composables;

Extra: 1) Refactor i18n strings including order status and search-related texts; 2) Replace temporary workarounds with `apollo-upload-client` configuration and add `apollo-upload-link.ts` plugin; 3) Cleanup redundant files, comments, and improve SCSS structure with new variables and placeholders.
2025-07-11 18:39:13 +03:00

81 lines
No EOL
2 KiB
TypeScript

import { REFRESH } from '@/graphql/mutations/auth';
import { useAppConfig } from '~/composables/config';
import { useLocaleRedirect } from '~/composables/languages';
import { useUserStore } from '~/stores/user';
import { isGraphQLError } from '~/utils/error';
import {DEFAULT_LOCALE} from "~/config/constants";
import {useNotification} from "~/composables/notification";
import {useUserBaseData} from "~/composables/user";
export function useRefresh() {
const { t } = useI18n();
const userStore = useUserStore();
const { COOKIES_REFRESH_TOKEN_KEY, COOKIES_LOCALE_KEY } = useAppConfig();
const { checkAndRedirect } = useLocaleRedirect();
const { mutate, loading, error } = useMutation(REFRESH);
async function refresh() {
const cookieRefresh = useCookie(
COOKIES_REFRESH_TOKEN_KEY,
{
default: () => '',
path: '/'
}
);
const cookieLocale = useCookie(
COOKIES_LOCALE_KEY,
{
default: () => DEFAULT_LOCALE,
path: '/'
}
);
if (!cookieRefresh.value) {
return;
}
const result = await mutate({ refreshToken: cookieRefresh.value });
const data = result?.data?.refreshJwtToken;
if (!data) {
return;
}
userStore.setUser(data.user);
if (data.user.language !== cookieLocale.value) {
await checkAndRedirect(data.user.language);
}
cookieRefresh.value = data.refreshToken
// await useWishlist();
// await useOrders({
// userEmail: data.user.email,
// status: "PENDING"
// });
// await usePromocodes();
await useUserBaseData(data.user.email);
}
watch(error, (err) => {
if (!err) return;
console.error('useRefresh error:', err);
let message = t('popup.errors.defaultError');
if (isGraphQLError(err)) {
message = err.graphQLErrors?.[0]?.message || message;
} else {
message = err.message;
}
useNotification({
message,
type: 'error',
title: t('popup.errors.main')
});
})
return {
refresh,
loading
};
}