-
Notifications
You must be signed in to change notification settings - Fork 445
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
a993155
commit d475d01
Showing
4 changed files
with
147 additions
and
45 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
41 changes: 41 additions & 0 deletions
41
src/app/(marketing)/create-profile/_hooks/useCreateProfile.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,41 @@ | ||
import { useToast } from "@/components/ui/hooks/useToast" | ||
import useJsConfetti from "components/create-guild/hooks/useJsConfetti" | ||
import { SignedValidation, useSubmitWithSign } from "hooks/useSubmit" | ||
import { useRouter } from "next/navigation" | ||
import fetcher from "utils/fetcher" | ||
import { z } from "zod" | ||
import { profileSchema } from "../schemas" | ||
|
||
export const useCreateProfile = () => { | ||
const router = useRouter() | ||
const { toast } = useToast() | ||
const triggerConfetti = useJsConfetti() | ||
|
||
const createProfile = async (signedValidation: SignedValidation) => | ||
fetcher(`/v2/profiles`, { | ||
method: "POST", | ||
...signedValidation, | ||
}) | ||
|
||
const submitWithSign = useSubmitWithSign<unknown>(createProfile, { | ||
onSuccess: (response) => { | ||
triggerConfetti() | ||
toast({ | ||
variant: "success", | ||
title: "Successfully created profile", | ||
}) | ||
// router.push(`/profile/${response.username}`) | ||
}, | ||
onError: (error) => | ||
toast({ | ||
variant: "error", | ||
title: "Failed to create profile", | ||
description: error?.message, | ||
}), | ||
}) | ||
return { | ||
...submitWithSign, | ||
onSubmit: (payload: z.infer<typeof profileSchema>) => | ||
submitWithSign.onSubmit(payload), | ||
} | ||
} |
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,36 @@ | ||
import { z } from "zod" | ||
export const profileSchema = z.object({ | ||
name: z | ||
.string() | ||
.max(100, { message: "Name cannot exceed 100 characters" }) | ||
.optional(), | ||
username: z | ||
.string() | ||
.min(1, { message: "Handle is required" }) | ||
.max(100, { message: "Handle cannot exceed 100 characters" }) | ||
.superRefine((value, ctx) => { | ||
const pattern = /^[\w\-.]+$/ | ||
const isValid = pattern.test(value) | ||
if (!isValid) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: | ||
"Handle must only contain either alphanumeric, hyphen, underscore or dot characters", | ||
}) | ||
} | ||
}), | ||
bio: z | ||
.string() | ||
.max(1000, { message: "Bio cannot exceed 500 characters" }) | ||
.optional(), | ||
profileImageUrl: z | ||
.string() | ||
.url({ message: "Profile image must be a valid URL" }) | ||
.max(500, { message: "Profile image URL cannot exceed 500 characters" }) | ||
.optional(), | ||
backgroundImageUrl: z | ||
.string() | ||
.url({ message: "Background image must be a valid URL" }) | ||
.max(500, { message: "Profile image URL cannot exceed 500 characters" }) | ||
.optional(), | ||
}) |