-
Notifications
You must be signed in to change notification settings - Fork 444
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
1 parent
aad6a98
commit efde5c6
Showing
17 changed files
with
368 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,68 @@ | ||
"use client"; | ||
|
||
import { useConfetti } from "@/components/ConfettiProvider"; | ||
import { Button } from "@/components/ui/Button"; | ||
import { GUILD_AUTH_COOKIE_NAME } from "@/config/constants"; | ||
import { env } from "@/lib/env"; | ||
import { fetcher } from "@/lib/fetcher"; | ||
import { getCookie } from "@/lib/getCookie"; | ||
import type { CreateGuildForm, Guild } from "@/lib/schemas/guild"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useRouter } from "next/navigation"; | ||
import { useFormContext } from "react-hook-form"; | ||
import slugify from "slugify"; | ||
|
||
const CreateGuildButton = () => { | ||
const { handleSubmit } = useFormContext<CreateGuildForm>(); | ||
|
||
const confetti = useConfetti(); | ||
|
||
const router = useRouter(); | ||
|
||
const { mutate: onSubmit, isPending } = useMutation({ | ||
mutationFn: async (data: CreateGuildForm) => { | ||
const token = getCookie(GUILD_AUTH_COOKIE_NAME); | ||
|
||
if (!token) throw new Error("Unauthorized"); // TODO: custom errors? | ||
|
||
const guild = { | ||
...data, | ||
contact: undefined, | ||
// TODO: I think we should do it on the backend | ||
urlName: slugify(data.name, { | ||
replacement: "-", | ||
lower: true, | ||
strict: true, | ||
}), | ||
}; | ||
|
||
return fetcher<Guild>(`${env.NEXT_PUBLIC_API}/guild`, { | ||
method: "POST", | ||
headers: { | ||
"X-Auth-Token": token, | ||
}, | ||
body: JSON.stringify(guild), | ||
}); | ||
}, | ||
onError: (error) => console.error(error), | ||
onSuccess: (res) => { | ||
confetti.current(); | ||
router.push(`/${res.urlName}`); | ||
console.log(res); | ||
}, | ||
}); | ||
|
||
return ( | ||
<Button | ||
colorScheme="success" | ||
size="xl" | ||
isLoading={isPending} | ||
loadingText="Creating guild" | ||
onClick={handleSubmit((data) => onSubmit(data))} | ||
> | ||
Create guild | ||
</Button> | ||
); | ||
}; | ||
|
||
export { CreateGuildButton }; |
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,51 @@ | ||
"use client"; | ||
|
||
import { useFormContext } from "react-hook-form"; | ||
|
||
import { | ||
FormControl, | ||
FormErrorMessage, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
} from "@/components/ui/Form"; | ||
import { Input } from "@/components/ui/Input"; | ||
import type { CreateGuildForm as CreateGuildFormType } from "@/lib/schemas/guild"; | ||
|
||
export const CreateGuildForm = () => { | ||
const { control } = useFormContext<CreateGuildFormType>(); | ||
|
||
return ( | ||
<> | ||
<FormField | ||
control={control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Guild name</FormLabel> | ||
<FormControl> | ||
<Input size="lg" {...field} /> | ||
</FormControl> | ||
|
||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<FormField | ||
control={control} | ||
name="contact" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>E-mail address</FormLabel> | ||
<FormControl> | ||
<Input size="lg" {...field} /> | ||
</FormControl> | ||
|
||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</> | ||
); | ||
}; |
25 changes: 25 additions & 0 deletions
25
src/app/create-guild/components/CreateGuildFormProvider.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,25 @@ | ||
"use client"; | ||
|
||
import { type CreateGuildForm, GuildSchema } from "@/lib/schemas/guild"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import type { PropsWithChildren } from "react"; | ||
import { FormProvider, useForm } from "react-hook-form"; | ||
|
||
const defaultValues = { | ||
name: "", | ||
imageUrl: "", | ||
urlName: "test", | ||
contact: "", | ||
} satisfies CreateGuildForm; | ||
|
||
const CreateGuildFormProvider = ({ children }: PropsWithChildren) => { | ||
const methods = useForm<CreateGuildForm>({ | ||
mode: "all", | ||
resolver: zodResolver(GuildSchema), | ||
defaultValues, | ||
}); | ||
|
||
return <FormProvider {...methods}>{children}</FormProvider>; | ||
}; | ||
|
||
export { CreateGuildFormProvider }; |
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,38 @@ | ||
import { AuthBoundary } from "@/components/AuthBoundary"; | ||
import { ConfettiProvider } from "@/components/ConfettiProvider"; | ||
import { SignInButton } from "@/components/SignInButton"; | ||
import { Card } from "@/components/ui/Card"; | ||
import { CreateGuildButton } from "./components/CreateGuildButton"; | ||
import { CreateGuildForm } from "./components/CreateGuildForm"; | ||
import { CreateGuildFormProvider } from "./components/CreateGuildFormProvider"; | ||
|
||
export const metadata = { | ||
title: "Begin your guild", | ||
}; | ||
|
||
const CreateGuild = () => ( | ||
<main className="container mx-auto grid max-w-lg gap-8 px-4 py-8"> | ||
{/* TODO: make a common layout component & use it here too */} | ||
<CreateGuildFormProvider> | ||
<ConfettiProvider> | ||
<Card className="flex flex-col px-5 py-6 shadow-lg md:px-6"> | ||
<h2 className="mb-7 text-center font-display font-extrabold text-2xl"> | ||
Begin your guild | ||
</h2> | ||
|
||
{/* TODO: <CreateGuildImageUploader /> */} | ||
|
||
<div className="mb-8 flex flex-col gap-4"> | ||
<CreateGuildForm /> | ||
</div> | ||
|
||
<AuthBoundary fallback={<SignInButton size="xl" />}> | ||
<CreateGuildButton /> | ||
</AuthBoundary> | ||
</Card> | ||
</ConfettiProvider> | ||
</CreateGuildFormProvider> | ||
</main> | ||
); | ||
|
||
export default CreateGuild; |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
"use client"; | ||
|
||
import { | ||
type MutableRefObject, | ||
type PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useRef, | ||
} from "react"; | ||
import ReactCanvasConfetti from "react-canvas-confetti/dist"; | ||
import type { TCanvasConfettiInstance } from "react-canvas-confetti/dist/types"; | ||
|
||
const doubleConfetti = (confetti: TCanvasConfettiInstance) => { | ||
const count = 200; | ||
const defaultsPerBarrage: confetti.Options[] = [ | ||
{ | ||
origin: { x: -0.05 }, | ||
angle: 50, | ||
}, | ||
{ | ||
origin: { x: 1.05 }, | ||
angle: 130, | ||
}, | ||
] as const; | ||
|
||
const fire = (particleRatio: number, opts: confetti.Options) => { | ||
confetti({ | ||
...opts, | ||
particleCount: Math.floor(count * particleRatio), | ||
}); | ||
}; | ||
|
||
for (const defaults of defaultsPerBarrage) { | ||
fire(0.25, { | ||
spread: 26, | ||
startVelocity: 55, | ||
...defaults, | ||
}); | ||
fire(0.2, { | ||
spread: 60, | ||
...defaults, | ||
}); | ||
fire(0.35, { | ||
spread: 100, | ||
decay: 0.91, | ||
scalar: 0.8, | ||
...defaults, | ||
}); | ||
fire(0.1, { | ||
spread: 120, | ||
startVelocity: 25, | ||
decay: 0.92, | ||
scalar: 1.2, | ||
...defaults, | ||
}); | ||
fire(0.1, { | ||
spread: 120, | ||
startVelocity: 45, | ||
...defaults, | ||
}); | ||
} | ||
}; | ||
|
||
const ConfettiContext = createContext<MutableRefObject<ConfettiPlayer>>( | ||
{} as MutableRefObject<ConfettiPlayer>, | ||
); | ||
|
||
export const useConfetti = () => useContext(ConfettiContext); | ||
|
||
type ConfettiPlayer = () => void; | ||
|
||
export const ConfettiProvider = ({ children }: PropsWithChildren) => { | ||
const confettiRef = useRef<ConfettiPlayer>(() => { | ||
return; | ||
}); | ||
|
||
const onInitHandler = ({ | ||
confetti, | ||
}: { confetti: TCanvasConfettiInstance }) => { | ||
const confettiClosure: ConfettiPlayer = () => { | ||
doubleConfetti(confetti); | ||
}; | ||
confettiRef.current = confettiClosure; | ||
}; | ||
|
||
return ( | ||
<ConfettiContext.Provider value={confettiRef}> | ||
{children} | ||
<ReactCanvasConfetti | ||
onInit={onInitHandler} | ||
globalOptions={{ | ||
disableForReducedMotion: true, | ||
useWorker: true, | ||
resize: true, | ||
}} | ||
/> | ||
</ConfettiContext.Provider> | ||
); | ||
}; |
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
Oops, something went wrong.