generated from Flycro/laravel-nuxt
86 lines
2.0 KiB
TypeScript
86 lines
2.0 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
|
|
enum BookRecommendationStatusEnum {
|
|
PENDING = 'PENDING',
|
|
REJECTED = 'REJECTED',
|
|
ACTIVE = 'ACTIVE',
|
|
COMPLETED = 'COMPLETED',
|
|
}
|
|
|
|
export interface BookRecommendation {
|
|
id: number
|
|
book_name: string
|
|
author: string
|
|
description: string
|
|
isbn: string
|
|
pages: number
|
|
recommended_by?: number
|
|
recommender?: {
|
|
ulid: number
|
|
name: string
|
|
email: string
|
|
avatar: string
|
|
}
|
|
status: BookRecommendationStatusEnum
|
|
cover_image?: string
|
|
}
|
|
|
|
export const useBookRecommendationStore = defineStore('bookRecommendations', () => {
|
|
const recommendations = ref<BookRecommendation[]>([])
|
|
|
|
const statusOptions = [
|
|
{
|
|
name: 'Pending',
|
|
value: BookRecommendationStatusEnum.PENDING,
|
|
},
|
|
{
|
|
name: 'Rejected',
|
|
value: BookRecommendationStatusEnum.REJECTED,
|
|
},
|
|
{
|
|
name: 'Active',
|
|
value: BookRecommendationStatusEnum.ACTIVE,
|
|
},
|
|
{
|
|
name: 'Completed',
|
|
value: BookRecommendationStatusEnum.COMPLETED,
|
|
},
|
|
]
|
|
|
|
// Fetch all book recommendations
|
|
const { refresh: fetchRecommendations, status: fetchRecommendationsStatus } = useFetch<BookRecommendation[]>('book-recommendations?with=recommender,votes', {
|
|
immediate: false,
|
|
onResponse({ response }) {
|
|
if (response.status === 200) {
|
|
recommendations.value = response._data
|
|
}
|
|
},
|
|
})
|
|
|
|
const deleteRecommendation = async (id: number) => {
|
|
try {
|
|
const { error } = await useFetch(`book-recommendations/${id}`, {
|
|
method: 'DELETE',
|
|
})
|
|
|
|
if (error.value) {
|
|
console.error('Failed to delete book recommendation:', error.value)
|
|
}
|
|
else {
|
|
recommendations.value = recommendations.value.filter(rec => rec.id !== id)
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('An error occurred while deleting a book recommendation:', e)
|
|
}
|
|
}
|
|
|
|
return {
|
|
recommendations,
|
|
statusOptions,
|
|
fetchRecommendations,
|
|
fetchRecommendationsStatus,
|
|
deleteRecommendation,
|
|
}
|
|
})
|