fetch element title

This commit is contained in:
Yisroel Baum 2026-05-26 19:51:45 +03:00
parent 8029a9e157
commit ba6bd357e2
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 94 additions and 2 deletions

View file

@ -0,0 +1,50 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
export interface Element {
id: number
title: string
}
interface ElementResponse {
element: Element
}
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL as string
export const useElementsStore = defineStore('elements', () => {
const element = ref<Element | null>(null)
const isLoading = ref(false)
const error = ref<string | null>(null)
async function fetchElement(elementId: string): Promise<void> {
element.value = null
error.value = null
isLoading.value = true
try {
const encodedElementId = encodeURIComponent(elementId)
const elementUrl = `${API_BASE_URL}/api/elements/${encodedElementId}`
const response = await fetch(elementUrl)
if (response.status === 404) {
error.value = 'Element not found'
return
}
if (!response.ok) {
error.value = 'Could not load element'
return
}
const data: ElementResponse = await response.json()
element.value = data.element
} catch {
error.value = 'Network error - could not load element'
} finally {
isLoading.value = false
}
}
return { element, isLoading, error, fetchElement }
})