-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove message endpoint, hook, integration and css
- Loading branch information
Showing
3 changed files
with
199 additions
and
75 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useCallback, useMemo, useState } from "react"; | ||
import { Id } from "../../../../convex/_generated/dataModel"; | ||
import { useMutation } from "convex/react"; | ||
import { api } from "../../../../convex/_generated/api"; | ||
|
||
type RequestType = { | ||
id: Id<"messages">; | ||
}; | ||
type ResponseType = Id<"messages"> | null; | ||
type Options = { | ||
onSuccess?: (data: ResponseType) => void; | ||
onError?: (error: Error) => void; | ||
onSettled?: () => void; | ||
throwError?: boolean; | ||
}; | ||
|
||
export const useRemoveMessage = () => { | ||
const [data, setData] = useState<ResponseType>(null); | ||
const [error, setError] = useState<Error | null>(null); | ||
const [status, setStatus] = useState< | ||
"pending" | "success" | "error" | "settled" | null | ||
>(null); | ||
|
||
const isPending = useMemo(() => status === "pending", [status]); | ||
const isSuccess = useMemo(() => status === "success", [status]); | ||
const isError = useMemo(() => status === "error", [status]); | ||
const isSettled = useMemo(() => status === "settled", [status]); | ||
|
||
const mutation = useMutation(api.messages.remove); | ||
|
||
const mutate = useCallback( | ||
async (values: RequestType, options?: Options) => { | ||
try { | ||
setData(null); | ||
setError(null); | ||
setStatus("pending"); | ||
|
||
const response = await mutation(values); | ||
options?.onSuccess?.(response); | ||
setStatus("success"); | ||
return response; | ||
} catch (error) { | ||
setStatus("error"); | ||
options?.onError?.(error as Error); | ||
if (options?.throwError) { | ||
throw error; | ||
} | ||
} finally { | ||
setStatus("settled"); | ||
options?.onSettled?.(); | ||
} | ||
}, | ||
[mutation] | ||
); | ||
return { | ||
mutate, | ||
data, | ||
error, | ||
isPending, | ||
isSuccess, | ||
isError, | ||
isSettled, | ||
}; | ||
}; |