frontend/src/api/merchImages.js
2025-10-26 21:32:35 +03:00

65 lines
1.6 KiB
JavaScript

import { apiClient } from '@/services/apiClient.js'
import { useAuthStore } from '@/stores/authStore.js'
import { IMAGE_STORAGE_URL } from '@/main.js'
export const useMerchImagesApi = () => {
const authStore = useAuthStore()
const uploadImage = async (uuid, file) => {
const formData = new FormData()
formData.append('file', file)
formData.append('imageType', 'all')
try {
const response = await apiClient.post(`/merch/images/${uuid}`, formData)
if (response.status !== 200) {
throw new Error(`Upload failed: ${response.status}`)
}
const { fullImage, thumbnail } = response.data
return {
fullImage,
thumbnail,
}
} catch (error) {
console.error('Upload failed:', error)
throw error
}
}
const getImageUrl = (merchUuid, type = 'full') => {
const userUuid = authStore.userUuid;
if (!userUuid) throw new Error('userUuid not found in store');
const allowedTypes = ['full', 'thumbnail'];
if (!allowedTypes.includes(type)) {
throw new Error(`Invalid type: ${type}. Allowed values: ${allowedTypes.join(', ')}`);
}
return `${IMAGE_STORAGE_URL}/merchImages/${userUuid}/${merchUuid}?type=${type}`;
};
const deleteImage = async (uuid) => {
try {
const response = await apiClient.delete(`/merch/images/${uuid}`)
if (response.status !== 200) {
throw new Error(`Delete failed: ${response.status}`)
}
return true
} catch (error) {
console.error('Delete image failed:', error)
throw error
}
}
return {
uploadImage,
getImageUrl,
deleteImage,
}
}