add Ko-fi bean
This commit is contained in:
61
frontend/src/hooks/useJournal.ts
Normal file
61
frontend/src/hooks/useJournal.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
getJournalEntries,
|
||||
getJournalEntry,
|
||||
createJournalEntry,
|
||||
updateJournalEntry,
|
||||
deleteJournalEntry,
|
||||
} from '../api/journal'
|
||||
import type { CreateJournalEntryInput, UpdateJournalEntryInput } from '../types/journal'
|
||||
|
||||
export function useJournalEntries(runId: number, bossResultId?: number) {
|
||||
return useQuery({
|
||||
queryKey: ['runs', runId, 'journal', { bossResultId }],
|
||||
queryFn: () => getJournalEntries(runId, bossResultId),
|
||||
})
|
||||
}
|
||||
|
||||
export function useJournalEntry(runId: number, entryId: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ['runs', runId, 'journal', entryId],
|
||||
queryFn: () => getJournalEntry(runId, entryId!),
|
||||
enabled: entryId != null,
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateJournalEntry(runId: number) {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: CreateJournalEntryInput) => createJournalEntry(runId, data),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['runs', runId, 'journal'] })
|
||||
toast.success('Journal entry created')
|
||||
},
|
||||
onError: (err) => toast.error(`Failed to create entry: ${err.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateJournalEntry(runId: number, entryId: string) {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: UpdateJournalEntryInput) => updateJournalEntry(runId, entryId, data),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['runs', runId, 'journal'] })
|
||||
toast.success('Journal entry saved')
|
||||
},
|
||||
onError: (err) => toast.error(`Failed to save entry: ${err.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteJournalEntry(runId: number) {
|
||||
const qc = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (entryId: string) => deleteJournalEntry(runId, entryId),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['runs', runId, 'journal'] })
|
||||
toast.success('Journal entry deleted')
|
||||
},
|
||||
onError: (err) => toast.error(`Failed to delete entry: ${err.message}`),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user