-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move queries and mutations to own files
- Loading branch information
Showing
25 changed files
with
411 additions
and
248 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
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 |
---|---|---|
@@ -1,53 +1,30 @@ | ||
"use client"; | ||
|
||
import { navigate } from "@/actions/navigate"; | ||
import { randomString } from "@/lib/utils"; | ||
import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
import axios from "axios"; | ||
import { Button, ButtonProps } from "./ui/button"; | ||
import { ReloadIcon } from "@radix-ui/react-icons"; | ||
import React from "react"; | ||
import { PiTerminalWindowLight } from "react-icons/pi"; | ||
|
||
type Credentials = { | ||
username: string, | ||
password: string, | ||
} | ||
import { useCreateReplMutation } from "@/hooks/use-create-repl-mutation"; | ||
|
||
const CreateReplButton = React.forwardRef<HTMLButtonElement, ButtonProps>( | ||
(props, ref) => { | ||
const client = useQueryClient(); | ||
|
||
const mutation = useMutation({ | ||
mutationFn: (credentials: Credentials) => axios.post<{ id: string }>( | ||
(process.env.NEXT_PUBLIC_API ?? "") + '/api/repl', | ||
credentials, | ||
{ | ||
withCredentials: true | ||
} | ||
), | ||
onSuccess: (response) => { | ||
client.invalidateQueries({ queryKey: ['repl-sessions'] }) | ||
navigate("/repl/" + response.data.id) | ||
}, | ||
}) | ||
|
||
const createReplMutation = useCreateReplMutation() | ||
const handleCreateRepl = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { | ||
const username = randomString(60); | ||
const password = randomString(60); | ||
mutation.mutate({ username, password }); | ||
createReplMutation.mutate({ username, password }); | ||
if (props.onClick) props.onClick(event) | ||
} | ||
|
||
return ( | ||
<Button ref={ref} {...props} disabled={mutation.isPending} onClick={handleCreateRepl}> | ||
{mutation.isPending ? <ReloadIcon className="mr-2 h-4 w-4 animate-spin" /> : | ||
<Button ref={ref} {...props} disabled={createReplMutation.isPending} onClick={handleCreateRepl}> | ||
{createReplMutation.isPending ? <ReloadIcon className="mr-2 h-4 w-4 animate-spin" /> : | ||
<PiTerminalWindowLight className="mr-2 h-4 w-4" />} REPLME! | ||
</Button> | ||
) | ||
} | ||
) | ||
|
||
CreateReplButton.displayName = "CreateReplButton"; | ||
|
||
export default CreateReplButton; | ||
|
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
Oops, something went wrong.