add admin element editing
This commit is contained in:
parent
eab26824bb
commit
240ab4e771
4 changed files with 484 additions and 3 deletions
|
|
@ -19,6 +19,23 @@ interface ElementResponse {
|
|||
childElements: ChildElement[]
|
||||
}
|
||||
|
||||
export interface UpdateElementInput {
|
||||
title: string
|
||||
description: string
|
||||
iconImageUrl: string
|
||||
richText: string
|
||||
pdfPath: string
|
||||
youtubeUrl: string
|
||||
}
|
||||
|
||||
interface UpdateElementResponse {
|
||||
element: Element
|
||||
}
|
||||
|
||||
interface ErrorResponse {
|
||||
error?: string
|
||||
}
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL as string
|
||||
|
||||
export const useElementsStore = defineStore('elements', () => {
|
||||
|
|
@ -26,11 +43,14 @@ export const useElementsStore = defineStore('elements', () => {
|
|||
const childElements = ref<ChildElement[]>([])
|
||||
const isLoading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const isSaving = ref(false)
|
||||
const saveError = ref<string | null>(null)
|
||||
|
||||
async function fetchElement(elementId: string): Promise<void> {
|
||||
element.value = null
|
||||
childElements.value = []
|
||||
error.value = null
|
||||
saveError.value = null
|
||||
isLoading.value = true
|
||||
|
||||
try {
|
||||
|
|
@ -59,5 +79,63 @@ export const useElementsStore = defineStore('elements', () => {
|
|||
}
|
||||
}
|
||||
|
||||
return { element, childElements, isLoading, error, fetchElement }
|
||||
async function updateElement(
|
||||
elementId: string,
|
||||
input: UpdateElementInput,
|
||||
): Promise<boolean> {
|
||||
saveError.value = null
|
||||
isSaving.value = true
|
||||
|
||||
try {
|
||||
const encodedElementId = encodeURIComponent(elementId)
|
||||
const elementUrl = `${API_BASE_URL}/api/elements/${encodedElementId}`
|
||||
const response = await fetch(elementUrl, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
|
||||
if (response.status === 401) {
|
||||
saveError.value = 'Please log in again'
|
||||
return false
|
||||
}
|
||||
|
||||
if (response.status === 404) {
|
||||
saveError.value = 'Element not found'
|
||||
return false
|
||||
}
|
||||
|
||||
if (response.status === 400) {
|
||||
const data: ErrorResponse = await response.json()
|
||||
saveError.value = data.error ?? 'Could not save element'
|
||||
return false
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
saveError.value = 'Could not save element'
|
||||
return false
|
||||
}
|
||||
|
||||
const data: UpdateElementResponse = await response.json()
|
||||
element.value = data.element
|
||||
return true
|
||||
} catch {
|
||||
saveError.value = 'Network error - could not save element'
|
||||
return false
|
||||
} finally {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
element,
|
||||
childElements,
|
||||
isLoading,
|
||||
error,
|
||||
isSaving,
|
||||
saveError,
|
||||
fetchElement,
|
||||
updateElement,
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue