Fanletter Develop

๋ฐ•์žฌ๋ฏผยท2024๋…„ 2์›” 22์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
35/49

๐Ÿ’กLoginForm

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์š”๊ตฌ ์‚ฌํ•ญ
1) Login.jsx ์—์„œ ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž… UI๋ฅผ ๋ชจ๋‘ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.
2) ๋กœ๊ทธ์ธ์ฐฝ์—์„œ๋Š” ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ํšŒ์›๊ฐ€์ž…์ฐฝ์œผ๋กœ, ํšŒ์›๊ฐ€์ž…์ฐฝ์—์„œ๋Š” ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋กœ๊ทธ์ธ์ฐฝ์œผ๋กœ ํ† ๊ธ€๋ง ๋˜๋„๋ก ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”.
3) ์•„์ด๋””๋Š” 4~10๊ธ€์ž๋กœ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” 4~15๊ธ€์ž๋กœ, ๋‹‰๋„ค์ž„์€ 1~10๊ธ€์ž๋กœ ์ œํ•œํ•˜์„ธ์š”.
4) (์„ ํƒ) ๋กœ๊ทธ์ธํ™”๋ฉด์—์„œ๋Š” ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์— ์ž…๋ ฅ๊ฐ’์ด ๋ชจ๋‘ ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™”๋˜๋„๋ก ํ•˜๊ณ , ํšŒ์›๊ฐ€์ž…ํ™”๋ฉด์—์„œ๋Š” ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ, ๋‹‰๋„ค์ž„์— ์ž…๋ ฅ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ํšŒ์›๊ฐ€์ž… ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™”๋˜๋„๋ก ํ•ด๋ณด์„ธ์š”.
5) redux ๋ชจ๋“ˆ๋กœ authSlice.js ๋ฅผ ๋งŒ๋“ค๊ณ  ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์ „์—ญ ์ƒํƒœ๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•˜์„ธ์š”.
6) ๋กœ๊ทธ์ธ ์„ฑ๊ณตํ–ˆ์„ ๊ฒฝ์šฐ, ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œ์ผœ ํ™ˆํ™”๋ฉด์„ ์ด๋™๋˜๋„๋ก ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
(Router.jsx ์ˆ˜์ •)
7) ํšŒ์›๊ฐ€์ž…์— ์„ฑ๊ณตํ•˜๋ฉด ๋กœ๊ทธ์ธ ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•ด ์ฃผ์„ธ์š”.
8) (์„ ํƒ) ๋กœ๊ทธ์ธ ์„ฑ๊ณต์ด๋‚˜ ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต ์‹œ ๋ธŒ๋ผ์šฐ์ € alert ๋Œ€์‹  react-toastify ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”.
   <Container>
      <LoginTitle>{isLoginMode ? "๋กœ๊ทธ์ธ" : "ํšŒ์›๊ฐ€์ž…"}</LoginTitle>
      <LoginContainer onSubmit={onSubmitForm}>
        <EmailInputBox>
          <label>ID</label>
          <input
            type="text"
            name="id"
            placeholder="์•„์ด๋”” (4~10๊ธ€์ž)"
            minLength={4}
            maxLength={10}
            required
            onChange={onChangeInput}
            value={id}
          />
        </EmailInputBox>
        <PasswordInputBox>
          <label>PW</label>
          <input
            type="password"
            name="password"
            placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ (4~15๊ธ€์ž)"
            minLength={4}
            maxLength={15}
            required
            onChange={onChangeInput}
            value={password}
          />
        </PasswordInputBox>
        {!isLoginMode && (
          <NickNameInputBox>
            <label>Nickname</label>
            <input
              type="text"
              name="nickname"
              value={nickname}
              onChange={onChangeInput}
              minLength={1}
              maxLength={10}
              placeholder="๋‹‰๋„ค์ž„ (1~10๊ธ€์ž)"
              required
            />
          </NickNameInputBox>
        )}
        <LoginNRegisterBox>
          <Button
            text={isLoginMode ? "๋กœ๊ทธ์ธ" : "ํšŒ์›๊ฐ€์ž…"}
            disabled={
              isLoginMode ? !id || !password : !id || !password || !nickname
            }
            size="large"
          ></Button>
          <CreateAccountButton onClick={() => setIsLoginMode((prev) => !prev)}>
            {isLoginMode ? "ํšŒ์›๊ฐ€์ž…" : "๋กœ๊ทธ์ธ"}
          </CreateAccountButton>
        </LoginNRegisterBox>
      </LoginContainer>
    </Container>
  );
};
๋กœ๊ทธ์ธ UI ๊ตฌ์„ฑ์„ ํ•˜๊ณ  useState ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋กœ๊ทธ์ธ์ฐฝ๊ณผ ํšŒ์›๊ฐ€์ž…์ฐฝ์— ๋”ฐ๋ผ์„œ ๋ฒ„ํŠผ์ด ๋‹ฌ๋ผ์ง€๋„๋ก ๊ตฌ์„ฑํ–ˆ๋‹ค. input ์—๋Š” minLength, maxLength ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ตœ์†Œ ๊ธ€์ž์ˆ˜์™€ ์ตœ๋Œ€ ๊ธ€์ž์ˆ˜๋ฅผ ์„ค์ •ํ–ˆ๋‹ค.

const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    login: (state, action) => {
      state.isLogin = true;
    },
    logout: (state, action) => {
      state.isLogin = false;
    },
  },
  
  
// Router.jsx
    const isLogin = useSelector((state) => state.auth.isLogin)

	<Routes>
        {isLogin ? (
          <Route element={<Layout />}>
            <Route path="/" element={<Home />} />
            <Route path="/detail/:id" element={<Detail />} />
            <Route path="/myprofile" element={<MyProfile />} />
            <Route path="*" element={<Navigate replace to="/" />} />
          </Route>
const onSubmitForm = async (e) => {
    e.preventDefault();
    console.log("์ œ์ถœ");
    if (isLoginMode) {
      dispatch(login());
      alert("๋กœ๊ทธ์ธ์„ฑ๊ณต")
    } else {
      // ํšŒ์›๊ฐ€์ž…
      setIsLoginMode(true);
      alert("ํšŒ์›๊ฐ€์ž… ์„ฑ๊ณต");
    }
  };
authSlice.js ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์ „์—ญ๊ด€๋ฆฌ ํ•˜๊ณ  Router ์—์„œ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ๊ฐ€์ ธ์™€์„œ isLogin ์ด true ์ผ ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ Route ์ธ Home ์œผ๋กœ ์ด๋™๋œ๋‹ค. onSubmitForm ์— ํšŒ์›๊ฐ€์ž… ๋์„ ๋•Œ setIsLoginMode ๋ฅผ true ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด์„œ ๋กœ๊ทธ์ธ๋ชจ๋“œ๊ฐ€ ๋ฐ”๋€๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€