schon/storefront/app/composables/user/useUserUpdating.ts
Alexandr SaVBaD Waltz 2ea18eb8a6 feat(storefront): refactor i18n and cart/wishlist handling for improved user experience
Refactored i18n configuration, replacing `DEFAULT_LOCALE` with `DEFAULT_LOCALE_FALLBACK` and enhancing environment-based locale validation. Improved cookie persistence for cart and wishlist, ensuring fallback handling for unauthenticated users.

Enhancements:
- Added `createProjectKey` utility for consistent project key generation.
- Reworked cart and wishlist composables (`useOrderOverwrite`, `useWishlistOverwrite`) to decouple product identifier and handle cookies robustly.
- Centralized `DEFAULT_LOCALE` logic for better maintainability.
- Refined `useOrderSync` and `useWishlistSync` for clean synchronization across auth states.
- Updated SCSS in hero and header styles for alignment corrections.

Breaking Changes: `DEFAULT_LOCALE` constant removed; replaced with runtime config and fallback logic. Consumers must adapt to `DEFAULT_LOCALE_FALLBACK` and `$appHelpers.DEFAULT_LOCALE`.
2026-02-28 22:38:45 +03:00

108 lines
2.6 KiB
TypeScript

import { useLogout } from '@composables/auth';
import { useLocaleRedirect } from '@composables/languages';
import { useNotification } from '@composables/notification';
import { UPDATE_USER } from '@graphql/mutations/user';
import type { IUserUpdatingResponse } from '@types';
export function useUserUpdating() {
const userStore = useUserStore();
const { t } = useI18n();
const { $appHelpers } = useNuxtApp();
const { mutate, loading, error } = useMutation<IUserUpdatingResponse>(UPDATE_USER);
const { checkAndRedirect } = useLocaleRedirect();
const { logout } = useLogout();
const cookieLocale = useCookie($appHelpers.COOKIES_LOCALE_KEY, {
default: () => $appHelpers.DEFAULT_LOCALE,
path: '/',
});
const userUuid = computed(() => userStore.user?.uuid);
const userEmail = computed(() => userStore.user?.email);
async function updateUser(
firstName: string,
lastName: string,
email: string,
phoneNumber: string,
password: string,
confirmPassword: string,
) {
const fields = {
uuid: userUuid.value,
firstName,
lastName,
email,
phoneNumber,
password,
confirmPassword,
};
const params = Object.fromEntries(
Object.entries(fields).filter(([_, value]) => value !== undefined && value !== null && value !== ''),
);
// if (('password' in params && !('passwordConfirm' in params)) ||
// (!('password' in params) && 'passwordConfirm' in params)) {
// ElNotification({
// title: t('popup.errors.main'),
// message: t('popup.errors.noDataToUpdate'),
// type: 'error'
// });
// }
if (Object.keys(params).length === 0) {
useNotification({
message: t('popup.errors.noDataToUpdate'),
type: 'error',
});
}
const result = await mutate(params);
const data = result?.data?.updateUser;
if (data) {
if (userEmail.value !== email) {
await logout();
useNotification({
message: t('popup.success.emailUpdate'),
type: 'info',
});
} else {
userStore.setUser(data.user);
useNotification({
message: t('popup.success.userUpdate'),
type: 'success',
});
if (data.user.language !== cookieLocale.value) {
await checkAndRedirect(data.user.language);
}
}
}
}
watch(error, (err) => {
if (!err) return;
console.error('useUserUpdating 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 {
updateUser,
loading,
};
}