generated from rharkor/next-boilerplate
-
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.
- Loading branch information
Showing
12 changed files
with
147 additions
and
18 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
12 changes: 12 additions & 0 deletions
12
packages/app/prisma/migrations/20240126091548_damage/migration.sql
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,12 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `damage` to the `PingResult` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "PingResult" ADD COLUMN "damage" INTEGER NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "Session" ADD COLUMN "basePoints" INTEGER, | ||
ADD COLUMN "damagePerHit" INTEGER; |
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
95 changes: 95 additions & 0 deletions
95
packages/app/src/app/[lang]/(not-protected)/update-session.tsx
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,95 @@ | ||
"use client" | ||
|
||
import { useEffect } from "react" | ||
import { useForm } from "react-hook-form" | ||
import { z } from "zod" | ||
|
||
import FormField from "@/components/ui/form" | ||
import { ModalHeader } from "@/components/ui/modal" | ||
import { useDictionary } from "@/contexts/dictionary/utils" | ||
import { updateSessionSchema } from "@/lib/schemas/nodes" | ||
import { trpc } from "@/lib/trpc/client" | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { Button, Modal, ModalBody, ModalContent, ModalFooter, useDisclosure } from "@nextui-org/react" | ||
|
||
const formSchema = updateSessionSchema | ||
|
||
export default function UpdateSession() { | ||
const dictionary = useDictionary() | ||
const utils = trpc.useUtils() | ||
const session = trpc.node.getSession.useQuery(undefined) | ||
const updateSessionMutation = trpc.node.updateSession.useMutation() | ||
|
||
const { isOpen, onOpen, onOpenChange, onClose } = useDisclosure() | ||
|
||
const form = useForm<z.infer<ReturnType<typeof formSchema>>>({ | ||
resolver: zodResolver(formSchema()), | ||
values: { | ||
id: session.data?.id ?? "", | ||
basePoints: session.data?.basePoints ?? 0, | ||
enabled: session.data?.enabled ?? false, | ||
damagePerHit: session.data?.damagePerHit ?? 0, | ||
}, | ||
}) | ||
|
||
const onSubmit = async (data: z.infer<ReturnType<typeof formSchema>>) => { | ||
await updateSessionMutation.mutateAsync(data) | ||
onClose() | ||
utils.node.invalidate() | ||
} | ||
|
||
useEffect(() => { | ||
if (!updateSessionMutation.isSuccess) return | ||
form.reset() | ||
}, [updateSessionMutation.isSuccess, form]) | ||
|
||
return ( | ||
<> | ||
<Button | ||
isDisabled={session.isLoading} | ||
onPress={onOpen} | ||
isLoading={updateSessionMutation.isLoading} | ||
color={"warning"} | ||
> | ||
{dictionary.updateSession} | ||
</Button> | ||
<Modal isOpen={isOpen} onOpenChange={onOpenChange}> | ||
<ModalContent> | ||
{(onClose) => ( | ||
<> | ||
<ModalHeader className="flex flex-col gap-1">{dictionary.updateSession}</ModalHeader> | ||
<form onSubmit={form.handleSubmit(onSubmit)}> | ||
<ModalBody> | ||
<FormField | ||
name="basePoints" | ||
label={dictionary.basePoints} | ||
placeholder="1000" | ||
form={form} | ||
type="number" | ||
isRequired | ||
/> | ||
<FormField | ||
name="damagePerHit" | ||
form={form} | ||
type="number" | ||
label={dictionary.damagePerHit} | ||
placeholder="10" | ||
isRequired | ||
/> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="default" variant="light" onPress={onClose}> | ||
{dictionary.cancel} | ||
</Button> | ||
<Button color="primary" type="submit" isLoading={updateSessionMutation.isLoading}> | ||
{dictionary.update} | ||
</Button> | ||
</ModalFooter> | ||
</form> | ||
</> | ||
)} | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
) | ||
} |
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