Fixes: 1) Replace `ElNotification` with `useNotification` across all components and composables; 2) Add missing semicolons, consistent formatting, and type annotations in multiple files; 3) Resolve non-reactive elements in wishlist and cart state management; Extra: 1) Update i18n translations with new strings for promocodes, balance, authentication, and profile settings; 2) Refactor SCSS styles including variable additions and component-specific tweaks; 3) Remove redundant queries, unused imports, and `storePage.ts` file for cleanup.
80 lines
No EOL
2.1 KiB
TypeScript
80 lines
No EOL
2.1 KiB
TypeScript
import { REFRESH } from '@/graphql/mutations/auth';
|
|
import { useAppConfig } from '~/composables/config';
|
|
import { useLocaleRedirect } from '~/composables/languages';
|
|
import { useWishlist } from '~/composables/wishlist';
|
|
import { usePendingOrder } from '~/composables/orders';
|
|
import { useUserStore } from '~/stores/user';
|
|
import { isGraphQLError } from '~/utils/error';
|
|
import {DEFAULT_LOCALE} from "~/config/constants";
|
|
import {useNotification} from "~/composables/notification";
|
|
import {usePromocodes} from "~/composables/promocodes";
|
|
|
|
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 usePendingOrder(data.user.email);
|
|
await usePromocodes();
|
|
//TODO: combine three requests
|
|
}
|
|
|
|
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
|
|
};
|
|
} |