refactor(admin): use shared api client across projects list, posts list, new post, project edit load, and media modal
This commit is contained in:
parent
f5a440a2ca
commit
9bc942211a
5 changed files with 26 additions and 152 deletions
|
|
@ -225,8 +225,6 @@
|
||||||
// Short delay to prevent flicker
|
// Short delay to prevent flicker
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500))
|
await new Promise((resolve) => setTimeout(resolve, 500))
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) return
|
|
||||||
|
|
||||||
let url = `/api/media?page=${page}&limit=24`
|
let url = `/api/media?page=${page}&limit=24`
|
||||||
|
|
||||||
|
|
@ -248,15 +246,7 @@
|
||||||
url += `&albumId=${albumId}`
|
url += `&albumId=${albumId}`
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const data = await (await import('$lib/admin/api')).api.get(url)
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to load media')
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json()
|
|
||||||
|
|
||||||
if (page === 1) {
|
if (page === 1) {
|
||||||
// Only clear media after we have new data to prevent flash
|
// Only clear media after we have new data to prevent flash
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
|
import { api } from '$lib/admin/api'
|
||||||
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
||||||
import AdminHeader from '$lib/components/admin/AdminHeader.svelte'
|
import AdminHeader from '$lib/components/admin/AdminHeader.svelte'
|
||||||
import AdminFilters from '$lib/components/admin/AdminFilters.svelte'
|
import AdminFilters from '$lib/components/admin/AdminFilters.svelte'
|
||||||
|
|
@ -85,25 +86,7 @@
|
||||||
|
|
||||||
async function loadPosts() {
|
async function loadPosts() {
|
||||||
try {
|
try {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
const data = await api.get('/api/posts')
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch('/api/posts', {
|
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
if (response.status === 401) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
throw new Error('Failed to load posts')
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json()
|
|
||||||
posts = data.posts || []
|
posts = data.posts || []
|
||||||
total = data.pagination?.total || posts.length
|
total = data.pagination?.total || posts.length
|
||||||
|
|
||||||
|
|
@ -209,28 +192,11 @@
|
||||||
|
|
||||||
async function handleTogglePublish(event: CustomEvent<{ post: Post }>) {
|
async function handleTogglePublish(event: CustomEvent<{ post: Post }>) {
|
||||||
const { post } = event.detail
|
const { post } = event.detail
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const newStatus = post.status === 'published' ? 'draft' : 'published'
|
const newStatus = post.status === 'published' ? 'draft' : 'published'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/posts/${post.id}`, {
|
await api.patch(`/api/posts/${post.id}`, { status: newStatus, updatedAt: post.updatedAt })
|
||||||
method: 'PUT',
|
await loadPosts()
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Basic ${auth}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ status: newStatus })
|
|
||||||
})
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
// Reload posts to refresh the list
|
|
||||||
await loadPosts()
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to toggle publish status:', error)
|
console.error('Failed to toggle publish status:', error)
|
||||||
}
|
}
|
||||||
|
|
@ -244,24 +210,11 @@
|
||||||
async function confirmDelete() {
|
async function confirmDelete() {
|
||||||
if (!postToDelete) return
|
if (!postToDelete) return
|
||||||
|
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/posts/${postToDelete.id}`, {
|
await api.delete(`/api/posts/${postToDelete.id}`)
|
||||||
method: 'DELETE',
|
showDeleteConfirmation = false
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
postToDelete = null
|
||||||
})
|
await loadPosts()
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
showDeleteConfirmation = false
|
|
||||||
postToDelete = null
|
|
||||||
// Reload posts to refresh the list
|
|
||||||
await loadPosts()
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to delete post:', error)
|
console.error('Failed to delete post:', error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
|
import { api } from '$lib/admin/api'
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
||||||
import Composer from '$lib/components/admin/composer'
|
import Composer from '$lib/components/admin/composer'
|
||||||
|
|
@ -73,12 +74,6 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSave(publishStatus?: 'draft' | 'published') {
|
async function handleSave(publishStatus?: 'draft' | 'published') {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
saving = true
|
saving = true
|
||||||
const postData = {
|
const postData = {
|
||||||
title: config?.showTitle ? title : null,
|
title: config?.showTitle ? title : null,
|
||||||
|
|
@ -91,22 +86,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/posts', {
|
const newPost = await api.post('/api/posts', postData)
|
||||||
method: 'POST',
|
goto(`/admin/posts/${newPost.id}/edit`)
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Basic ${auth}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify(postData)
|
|
||||||
})
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const newPost = await response.json()
|
|
||||||
// Redirect to edit page after creation
|
|
||||||
goto(`/admin/posts/${newPost.id}/edit`)
|
|
||||||
} else {
|
|
||||||
console.error('Failed to create post:', response.statusText)
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to create post:', error)
|
console.error('Failed to create post:', error)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
|
import { api } from '$lib/admin/api'
|
||||||
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
import AdminPage from '$lib/components/admin/AdminPage.svelte'
|
||||||
import AdminHeader from '$lib/components/admin/AdminHeader.svelte'
|
import AdminHeader from '$lib/components/admin/AdminHeader.svelte'
|
||||||
import AdminFilters from '$lib/components/admin/AdminFilters.svelte'
|
import AdminFilters from '$lib/components/admin/AdminFilters.svelte'
|
||||||
|
|
@ -79,25 +80,7 @@
|
||||||
|
|
||||||
async function loadProjects() {
|
async function loadProjects() {
|
||||||
try {
|
try {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
const data = await api.get('/api/projects')
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch('/api/projects', {
|
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
if (response.status === 401) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
throw new Error('Failed to load projects')
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json()
|
|
||||||
projects = data.projects
|
projects = data.projects
|
||||||
|
|
||||||
// Calculate status counts
|
// Calculate status counts
|
||||||
|
|
@ -126,21 +109,9 @@
|
||||||
const project = event.detail.project
|
const project = event.detail.project
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
|
||||||
const newStatus = project.status === 'published' ? 'draft' : 'published'
|
const newStatus = project.status === 'published' ? 'draft' : 'published'
|
||||||
|
await api.patch(`/api/projects/${project.id}`, { status: newStatus, updatedAt: project.updatedAt })
|
||||||
const response = await fetch(`/api/projects/${project.id}`, {
|
await loadProjects()
|
||||||
method: 'PATCH',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Basic ${auth}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ status: newStatus })
|
|
||||||
})
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
await loadProjects()
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to update project status:', err)
|
console.error('Failed to update project status:', err)
|
||||||
}
|
}
|
||||||
|
|
@ -155,16 +126,8 @@
|
||||||
if (!projectToDelete) return
|
if (!projectToDelete) return
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
await api.delete(`/api/projects/${projectToDelete.id}`)
|
||||||
|
await loadProjects()
|
||||||
const response = await fetch(`/api/projects/${projectToDelete.id}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
await loadProjects()
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to delete project:', err)
|
console.error('Failed to delete project:', err)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import ProjectForm from '$lib/components/admin/ProjectForm.svelte'
|
import ProjectForm from '$lib/components/admin/ProjectForm.svelte'
|
||||||
import type { Project } from '$lib/types/project'
|
import type { Project } from '$lib/types/project'
|
||||||
|
import { api } from '$lib/admin/api'
|
||||||
|
|
||||||
let project = $state<Project | null>(null)
|
let project = $state<Project | null>(null)
|
||||||
let isLoading = $state(true)
|
let isLoading = $state(true)
|
||||||
|
|
@ -17,21 +18,7 @@
|
||||||
|
|
||||||
async function loadProject() {
|
async function loadProject() {
|
||||||
try {
|
try {
|
||||||
const auth = localStorage.getItem('admin_auth')
|
const data = await api.get(`/api/projects/${projectId}`)
|
||||||
if (!auth) {
|
|
||||||
goto('/admin/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(`/api/projects/${projectId}`, {
|
|
||||||
headers: { Authorization: `Basic ${auth}` }
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to load project')
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json()
|
|
||||||
project = data
|
project = data
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
error = 'Failed to load project'
|
error = 'Failed to load project'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue