-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from hhhminme/feat/register
회원가입 로직 기능을 추가합니다.
- Loading branch information
Showing
9 changed files
with
190 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { http } from "../../../lib/http"; | ||
import { UserSchema } from "../_types"; | ||
import type { UserType, RegisterReq, UserErrorType } from "../_types"; | ||
|
||
export function register(req: RegisterReq): Promise<UserType | UserErrorType | undefined> { | ||
return http.post({ | ||
url: "/users", | ||
body: { | ||
user: req, | ||
}, | ||
schema: UserSchema, | ||
}); | ||
} |
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,11 @@ | ||
interface ErrorMessageProps { | ||
message?: string; | ||
} | ||
|
||
export function ErrorMessage({ message }: ErrorMessageProps): JSX.Element { | ||
return ( | ||
<ul className="error-messages"> | ||
<li>{message}</li> | ||
</ul> | ||
); | ||
} |
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,88 @@ | ||
"use client"; | ||
|
||
import type { SubmitHandler } from "react-hook-form"; | ||
import { useForm } from "react-hook-form"; | ||
import { useRouter } from "next/navigation"; | ||
import type { RegisterReq } from "../_types"; | ||
import { useRegister } from "../_hooks/use-register"; | ||
import { UserSchema, UserType } from "../_types"; | ||
import { ErrorMessage } from "./error-message"; | ||
|
||
export function RegisterForm(): JSX.Element { | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
setError, | ||
resetField, | ||
} = useForm<RegisterReq>(); | ||
|
||
const { mutate } = useRegister(); | ||
|
||
const router = useRouter(); | ||
|
||
const onSubmit: SubmitHandler<RegisterReq> = data => { | ||
mutate(data, { | ||
onSuccess: () => { | ||
router.push("/login"); | ||
}, | ||
onError: () => { | ||
setError("email", { | ||
message: "email has already been taken", | ||
}); | ||
setError("username", { | ||
message: "username has already been taken", | ||
}); | ||
|
||
resetField("password"); | ||
}, | ||
}); | ||
}; | ||
|
||
const isEmailValid = (email: string) => { | ||
const emailPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i; | ||
return emailPattern.test(email); | ||
}; | ||
|
||
return ( | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<fieldset className="form-group"> | ||
<input | ||
{...register("username", { required: "Username can't be blank" })} | ||
className="form-control form-control-lg" | ||
placeholder="Username" | ||
type="text" | ||
/> | ||
</fieldset> | ||
{errors.username ? <ErrorMessage message={errors.username.message} /> : null} | ||
|
||
<fieldset className="form-group"> | ||
<input | ||
{...register("email", { | ||
required: "Email can't be blank", | ||
validate: email => isEmailValid(email) || "Invalid email format", | ||
})} | ||
className="form-control form-control-lg" | ||
placeholder="Email" | ||
type="text" | ||
/> | ||
</fieldset> | ||
{errors.email ? <ErrorMessage message={errors.email.message} /> : null} | ||
|
||
<fieldset className="form-group"> | ||
<input | ||
{...register("password", { required: "Password can't be blank" })} | ||
className="form-control form-control-lg" | ||
placeholder="Password" | ||
type="password" | ||
/> | ||
</fieldset> | ||
{errors.password ? <ErrorMessage message={errors.password.message} /> : null} | ||
|
||
<button className="btn btn-lg btn-primary pull-xs-right" type="submit"> | ||
Sign up | ||
</button> | ||
</form> | ||
); | ||
} |
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,9 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import type { RegisterReq } from "../_types"; | ||
import { register } from "../_api"; | ||
|
||
export function useRegister() { | ||
return useMutation({ | ||
mutationFn: (req: RegisterReq) => register(req), | ||
}); | ||
} |
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,28 @@ | ||
import { z } from "zod"; | ||
|
||
export interface RegisterReq { | ||
username: string; | ||
email: string; | ||
password: string; | ||
} | ||
|
||
export const UserErrorsSchema = z.object({ | ||
errors: z.object({ | ||
email: z.array(z.string()), | ||
username: z.array(z.string()), | ||
}), | ||
}); | ||
|
||
export const UserSchema = z.object({ | ||
user: z.object({ | ||
email: z.string(), | ||
token: z.string(), | ||
username: z.string(), | ||
bio: z.string(), | ||
image: z.string(), | ||
}), | ||
}); | ||
|
||
export type UserErrorType = z.infer<typeof UserErrorsSchema>; | ||
|
||
export type UserType = z.infer<typeof UserSchema>; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.