-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-348]: add edit to the prompt table;
- Loading branch information
Sasha
committed
Nov 6, 2024
1 parent
528b63d
commit 513161e
Showing
4 changed files
with
129 additions
and
31 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
apps/opik-frontend/src/api/prompts/usePromptUpdateMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import { AxiosError } from "axios"; | ||
import get from "lodash/get"; | ||
|
||
import api, { PROMPTS_REST_ENDPOINT } from "@/api/api"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
import { Prompt } from "@/types/prompts"; | ||
|
||
type UsePromptUpdateMutationParams = { | ||
prompt: Partial<Prompt>; | ||
}; | ||
|
||
const usePromptUpdateMutation = () => { | ||
const queryClient = useQueryClient(); | ||
const { toast } = useToast(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ prompt }: UsePromptUpdateMutationParams) => { | ||
const { id: promptId, ...restPrompt } = prompt; | ||
|
||
const { data } = await api.put(`${PROMPTS_REST_ENDPOINT}${promptId}`, { | ||
...restPrompt, | ||
}); | ||
|
||
return data; | ||
}, | ||
|
||
onError: (error: AxiosError) => { | ||
const message = get( | ||
error, | ||
["response", "data", "message"], | ||
error.message, | ||
); | ||
|
||
toast({ | ||
title: "Error", | ||
description: message, | ||
variant: "destructive", | ||
}); | ||
}, | ||
onSettled: () => { | ||
return queryClient.invalidateQueries({ queryKey: ["prompts"] }); | ||
}, | ||
}); | ||
}; | ||
|
||
export default usePromptUpdateMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters