Spaces:
Running
Running
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | |
import { createQePrompt, deleteQePrompt, fetchQePrompts, setDefaultQePrompt, updateQePrompt } from './qePromptApi'; | |
export const useQePrompts = () => { | |
const queryClient = useQueryClient(); | |
const { data: prompts = [], isLoading, error } = useQuery({ | |
queryKey: ['qePrompts'], | |
queryFn: fetchQePrompts, | |
}); | |
const createMutation = useMutation({ | |
mutationFn: createQePrompt, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['qePrompts'] }); | |
}, | |
}); | |
const updateMutation = useMutation({ | |
mutationFn: updateQePrompt, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['qePrompts'] }); | |
}, | |
}); | |
const setDefaultMutation = useMutation({ | |
mutationFn: setDefaultQePrompt, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['qePrompts'] }); | |
}, | |
}); | |
const deleteMutation = useMutation({ | |
mutationFn: deleteQePrompt, | |
onSuccess: () => { | |
queryClient.invalidateQueries({ queryKey: ['qePrompts'] }); | |
}, | |
}); | |
return { | |
prompts, | |
isLoading, | |
error: error ? (error instanceof Error ? error.message : 'Failed to fetch prompturations') : null, | |
createPrompt: createMutation.mutateAsync, | |
updatePrompt: updateMutation.mutateAsync, | |
setAsDefaultPrompt: setDefaultMutation.mutateAsync, | |
deletePrompt: deleteMutation.mutateAsync, | |
}; | |
}; |