schon/storefront/app/composables/user/useAvatarUpload.ts
Alexandr SaVBaD Waltz e65e7b7d73 **chore(storefront): apply consistent code formatting and improve readability**
Refactored multiple files for code styling consistency, using proper indentation and spacing to align with team standards. Improved readability and maintainability across composables, Apollo plugin, and localization files.

Enhancements:
- Standardized import and function indentation across all composables.
- Updated `biome.json` schema to the latest version (v2.4.4) for tool compatibility.
- Organized code blocks in Apollo plugin for better understandability.

No functional changes introduced—this is a non-breaking, code refinement commit.
2026-02-28 17:41:25 +03:00

56 lines
1.2 KiB
TypeScript

import { useNotification } from '@composables/notification';
import { UPLOAD_AVATAR } from '@graphql/mutations/user';
import type { IAvatarUploadResponse } from '@types';
export function useAvatarUpload() {
const { t } = useI18n();
const userStore = useUserStore();
const { mutate, onDone, error } = useMutation<IAvatarUploadResponse>(UPLOAD_AVATAR);
async function uploadAvatar(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0];
if (!file) return;
await mutate(
{
file,
},
{
context: {
hasUpload: true,
},
},
);
}
onDone(({ data }) => {
const user = data?.uploadAvatar.user;
if (user) {
userStore.setUser(user);
useNotification({
message: t('popup.success.avatarUpload'),
type: 'success',
});
}
});
watch(error, (err) => {
if (!err) return;
console.error('useAvatarUpload 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 {
uploadAvatar,
};
}