Skip to content

Commit

Permalink
refactor: Refactor form state and schema names
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghunYang committed Feb 29, 2024
1 parent 3549a70 commit cd36ce9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions app/business/auth/user.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { State } from '@/app/ui/view/molecule/form/form-root';
import { z } from 'zod';

const SimpleSignUpFormSchema = z
// message name은 logic 구현할 때 통일할 예정
const SignUpFormSchema = z
.object({
userId: z
.string()
Expand Down Expand Up @@ -33,18 +34,17 @@ const SimpleSignUpFormSchema = z
}
});

type User = z.infer<typeof SimpleSignUpFormSchema>;
type User = z.infer<typeof SignUpFormSchema>;

export async function createUser(prevState: State, formData: FormData): Promise<State> {
const validatedFields = SimpleSignUpFormSchema.safeParse({
const validatedFields = SignUpFormSchema.safeParse({
userId: formData.get('userId'),
password: formData.get('password'),
confirmPassword: formData.get('confirmPassword'),
studentNumber: formData.get('studentNumber'),
english: formData.get('english'),
});

console.log(validatedFields);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
Expand Down
11 changes: 6 additions & 5 deletions app/ui/view/molecule/form/form-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { useFormState } from 'react-dom';
import { FormSubmitButton } from './form-submit-button';
import { FormContext } from './form.context';

export type State = {
export type FormState = {
message: string | null;
errors: Record<string, string[] | undefined>;
};

// Refactor: 애를 따로 빼고싶은데 utils 폴더에 어떤 파일명이 좋을까?
export const filterChildrenByType = (children: React.ReactNode, elementType: React.ElementType) => {
const childArray = React.Children.toArray(children);
return childArray.filter((child) => React.isValidElement(child) && child.type === elementType);
Expand All @@ -19,12 +20,12 @@ const getFormSubmitButton = (children: React.ReactNode) => {

type FormRootProps = {
id: string;
action: (prevState: State, formData: FormData) => Promise<State> | State;
action: (prevState: FormState, formData: FormData) => Promise<FormState> | FormState;
};

export function FormRoot({ id, action, children }: React.PropsWithChildren<FormRootProps>) {
const initialState = { message: null, errors: {} };
const [state, dispatch] = useFormState(action, initialState);
const initialState: FormState = { message: null, errors: {} };
const [formState, dispatch] = useFormState(action, initialState);

const formSubmitButton = getFormSubmitButton(children);

Expand All @@ -41,7 +42,7 @@ export function FormRoot({ id, action, children }: React.PropsWithChildren<FormR
};

return (
<FormContext.Provider value={{ ...state, formId: id }}>
<FormContext.Provider value={{ ...formState, formId: id }}>
<form id={id} action={dispatch}>
{renderWithoutSubmitButton()}
<div className="mt-8">{formSubmitButton}</div>
Expand Down
4 changes: 2 additions & 2 deletions app/ui/view/molecule/form/form.context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext } from 'react';
import type { State } from './form-root';
import type { FormState } from './form-root';

type FormContext = State & { formId: string };
type FormContext = FormState & { formId: string };

export const FormContext = createContext<FormContext>({
formId: '',
Expand Down

0 comments on commit cd36ce9

Please sign in to comment.