schon/storefront/app/composables/user/useUserUpdating.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

107 lines
2.5 KiB
TypeScript

import { useLogout } from '@composables/auth';
import { useLocaleRedirect } from '@composables/languages';
import { UPDATE_USER } from '@graphql/mutations/user';
import type { IUserUpdatingResponse } from '@types';
export function useUserUpdating() {
const userStore = useUserStore();
const { t } = useI18n();
const { $appHelpers, $notify } = 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) {
$notify({
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();
$notify({
message: t('popup.success.emailUpdate'),
type: 'info',
});
} else {
userStore.setUser(data.user);
$notify({
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;
}
$notify({
message,
type: 'error',
title: t('popup.errors.main'),
});
});
return {
updateUser,
loading,
};
}