2024-09-16 16:43:29 +02:00
|
|
|
import { Buffer } from 'buffer'
|
|
|
|
import type { Project } from '~/types'
|
|
|
|
|
|
|
|
export const useProject = defineStore('project', () => {
|
|
|
|
const project = ref<Partial<Project>>()
|
|
|
|
const projectImage = ref<File>()
|
|
|
|
const isPublishing = ref(false)
|
|
|
|
|
|
|
|
const { getProjectById } = useData()
|
|
|
|
|
|
|
|
function setProject(id: string) {
|
2024-10-01 14:54:35 +02:00
|
|
|
clearProject()
|
2024-09-16 16:43:29 +02:00
|
|
|
project.value = getProjectById(id, { shallow: false }) as Project
|
2024-09-30 14:31:17 +02:00
|
|
|
delete project.value.ratings
|
2024-09-16 16:43:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearProject() {
|
|
|
|
project.value = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveProject(data: Partial<Project>) {
|
|
|
|
project.value = {
|
|
|
|
...project.value,
|
|
|
|
...data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveProjectImage(image: File) {
|
|
|
|
projectImage.value = image
|
|
|
|
}
|
|
|
|
|
|
|
|
async function publishProject() {
|
|
|
|
isPublishing.value = true
|
|
|
|
try {
|
|
|
|
let imageBuffer: Buffer | undefined
|
|
|
|
let base64Image: string | undefined
|
|
|
|
|
2024-09-20 12:06:54 +02:00
|
|
|
if (projectImage.value) {
|
|
|
|
const imageArrayBuffer = await projectImage.value?.arrayBuffer()
|
|
|
|
if (imageArrayBuffer) {
|
|
|
|
imageBuffer = Buffer.from(imageArrayBuffer)
|
|
|
|
const base64String = imageBuffer.toString('base64')
|
|
|
|
base64Image = base64String
|
|
|
|
}
|
2024-09-16 16:43:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await $fetch(`/api/data`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
project: project.value,
|
|
|
|
image: {
|
|
|
|
type: projectImage.value?.type,
|
|
|
|
data: base64Image,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
isPublishing.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
project,
|
|
|
|
isPublishing,
|
|
|
|
setProject,
|
|
|
|
clearProject,
|
|
|
|
saveProject,
|
|
|
|
saveProjectImage,
|
|
|
|
publishProject,
|
|
|
|
}
|
|
|
|
})
|