schon/storefront/composables/user/useAvatarUpload.ts
Alexandr SaVBaD Waltz 4957039fc5 Features: 1) Integrate advanced Apollo link setup including error handling, authentication, and custom link chaining; 2) Replace apollo-upload-link.ts with revised client link configuration in apollo.ts; 3) Add @types/apollo-upload-client and @types/extract-files for enhanced TypeScript support;
Fixes: 1) Remove deprecated and redundant logic from `useAvatarUpload`; 2) Correct non-functional avatar upload and improve template handling in `settings.vue`;

Extra: 1) Cleanup unused imports, comments, and SCSS styles across files; 2) Simplify plugin configuration and migration to consolidated link logic; 3) Update package dependencies with precise resolution in `package-lock.json`.
2025-07-11 19:25:03 +03:00

48 lines
No EOL
1.2 KiB
TypeScript

import type {IAvatarUploadResponse,} from "~/types";
import {UPLOAD_AVATAR} from "~/graphql/mutations/user";
import {isGraphQLError} from "~/utils/error";
import {useNotification} from "~/composables/notification";
export function useAvatarUpload() {
const { t } = useI18n();
const userStore = useUserStore();
const { mutate, onDone, loading, error } = useMutation<IAvatarUploadResponse>(UPLOAD_AVATAR);
async function uploadAvatar(event: Event) {
const file = (event.target as HTMLInputElement).files?.[0];
if (!file) return;
await mutate({ file });
}
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
};
}