Fixes: 1) Correct mutation name from `setlanguage` to `setLanguage` for consistency; 2) Improve product listing reactivity by addressing missing initialization in `useStore`; 3) Replace generic product queries with parametrized `useProducts` for modularity; 4) Resolve minor typos, missing semicolons, and code formatting inconsistencies. Extra: 1) Refactor feedback-related types, composables, and GraphQL utilities for modularity; 2) Update styles, Vue templates, and related scripts with enhanced formatting; 3) Remove unused methods like `getProducts`, standardizing query reactivity; 4) Cleanup and organize imports across multiple files.
76 lines
No EOL
1.8 KiB
TypeScript
76 lines
No EOL
1.8 KiB
TypeScript
import {SWITCH_LANGUAGE} from "@/graphql/mutations/languages.js";
|
|
import {useAppConfig} from "~/composables/config";
|
|
import type {IUserResponse, LocaleDefinition} from "~/types";
|
|
import {DEFAULT_LOCALE} from "@intlify/core-base";
|
|
|
|
export function useLanguageSwitch() {
|
|
const userStore = useUserStore();
|
|
const router = useRouter();
|
|
const { $apollo } = useNuxtApp() as any;
|
|
|
|
const { COOKIES_LOCALE_KEY } = useAppConfig();
|
|
const switchLocalePath = useSwitchLocalePath();
|
|
|
|
const cookieLocale = useCookie(
|
|
COOKIES_LOCALE_KEY,
|
|
{
|
|
default: () => DEFAULT_LOCALE,
|
|
path: '/'
|
|
}
|
|
);
|
|
|
|
const isAuthenticated = computed(() => userStore.isAuthenticated)
|
|
const userUuid = computed(() => userStore.user?.uuid);
|
|
|
|
const { mutate, loading, error } = useMutation<IUserResponse>(SWITCH_LANGUAGE);
|
|
|
|
let isSwitching = false;
|
|
|
|
async function switchLanguage(
|
|
locale: string
|
|
) {
|
|
if (isSwitching || cookieLocale.value === locale) return;
|
|
|
|
try {
|
|
isSwitching = true;
|
|
|
|
cookieLocale.value = locale;
|
|
|
|
await $apollo.defaultClient.clearStore();
|
|
|
|
await router.push({path: switchLocalePath(cookieLocale.value as LocaleDefinition['code'])});
|
|
|
|
if (isAuthenticated.value) {
|
|
const result = await mutate({
|
|
uuid: userUuid.value,
|
|
locale
|
|
});
|
|
|
|
if (result?.data?.updateUser) {
|
|
userStore.setUser(result.data.updateUser.user);
|
|
}
|
|
}
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
await $apollo.defaultClient.refetchQueries({
|
|
include: 'active'
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error('Error switching language:', error);
|
|
} finally {
|
|
isSwitching = false;
|
|
}
|
|
}
|
|
|
|
watch(error, (err) => {
|
|
if (err) {
|
|
console.error('useLanguageSwitch error:', err)
|
|
}
|
|
});
|
|
|
|
return {
|
|
switchLanguage,
|
|
loading
|
|
};
|
|
} |