Skip to content

Commit

Permalink
πŸš€ Deploy #100
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaem03 authored Nov 15, 2024
2 parents f4d003e + 726b5a7 commit e69d9a7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
20 changes: 12 additions & 8 deletions src/pages/nuguPatch/NuguPatch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useNuguPatch } from "./_hooks/useNuguPatch";
import { getNugu } from "@apis/nuguPatch";
export const NuguPatch = () => {
const [updateData, setUpdateData] = useRecoilState(signUpState);
const { handleSubmit, isFormValid } = useNuguPatch();
const { errors, handleSubmit, isFormValid } = useNuguPatch();
const { selectedChip, handleClickStatus, selectedCount } = usePatchChip();

useEffect(() => {
Expand Down Expand Up @@ -59,13 +59,17 @@ export const NuguPatch = () => {
<S.TopWrapper>
<ProgressBar title={"λˆ„κ΅¬ μˆ˜μ •ν•˜κΈ°"} $now={4} $total={4} />
{[...SIGN_UP_FIELDS[1], ...SIGN_UP_FIELDS[2]].map((data, index) => (
<Input
title={data.title}
key={index}
value={updateData[data.name] || ""}
onChange={(e) => handleInputChange(data.name, e.target.value)}
type={data.type}
/>
<div key={index}>
<Input
title={data.title}
value={updateData[data.name] || ""}
onChange={(e) => handleInputChange(data.name, e.target.value)}
type={data.type}
/>
{errors[data.name] && (
<S.ErrorMessage>{errors[data.name]}</S.ErrorMessage>
)}
</div>
))}

<S.TitleWrapper>
Expand Down
54 changes: 52 additions & 2 deletions src/pages/nuguPatch/_hooks/useNuguPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@ import { patchNuguInfo } from "@apis/nuguPatch";
import { useRecoilState } from "recoil";
import { signUpState } from "@atoms/signUpState";
import { useNavigate } from "react-router-dom";

import { useState } from "react";
export const useNuguPatch = () => {
const [errors, setErrors] = useState({});

const navigate = useNavigate();
const [updateData, setUpdateData] = useRecoilState(signUpState);
const handleSubmit = async () => {
console.log(updateData);

let valid = true;
const tempErrors = {};

Object.keys(updateData).forEach((field) => {
const value = updateData[field];
const error = checkFieldValidity(value, field);
console.log(`Error for ${field}: ${error}`);

if (error !== "") {
valid = false;
tempErrors[field] = error;
} else {
delete tempErrors[field];
}
});
setErrors(tempErrors);
console.log(valid);
if (!valid) {
return;
}

try {
const response = await patchNuguInfo({
nickname: updateData.nickname,
Expand All @@ -25,6 +49,32 @@ export const useNuguPatch = () => {
throw err;
}
};
const checkFieldValidity = (value, fieldName) => {
let error = "";
// 각 ν•„λ“œμ— λŒ€ν•œ μœ νš¨μ„± 검사
if (fieldName === "username" && (value.length === 0 || value.length > 20)) {
error = "μ•„μ΄λ””λŠ” 20자 μ΄λ‚΄λ‘œ μž‘μ„±ν•΄μ£Όμ„Έμš”.";
} else if (
fieldName === "password" &&
(value.length < 8 || value.length > 16)
) {
error = "λΉ„λ°€λ²ˆν˜ΈλŠ” 8-16자둜 μž‘μ„±ν•΄μ£Όμ„Έμš”.";
} else if (fieldName === "nickname" && value.length > 5) {
error = "λ‹‰λ„€μž„μ€ 5자 μ΄λ‚΄λ‘œ μž‘μ„±ν•΄μ£Όμ„Έμš”.";
} else if (fieldName === "mbti") {
value = value.trim(); // 곡백 제거
// 영문 4자 검사
if (value && !/^[A-Za-z]{4}$/.test(value)) {
error = "MBTIλŠ” 영문 4자둜 μž…λ ₯ν•΄μ£Όμ„Έμš”."; // 였λ₯˜ λ©”μ‹œμ§€
}
} else if (fieldName === "organization" && value.length > 30) {
error = "μ†Œμ†μ€ 30자 μ΄λ‚΄λ‘œ μž‘μ„±ν•΄μ£Όμ„Έμš”.";
} else if (fieldName === "intro" && value.length > 30) {
error = "ν•œ 쀄 μ†Œκ°œλŠ” 30자 μ΄λ‚΄λ‘œ μž‘μ„±ν•΄μ£Όμ„Έμš”.";
}

return error;
};

const isFormValid = () => {
const requiredFields = [
Expand All @@ -39,5 +89,5 @@ export const useNuguPatch = () => {
];
return requiredFields.every((field) => field !== "");
};
return { handleSubmit, isFormValid };
return { handleSubmit, isFormValid, errors };
};
6 changes: 6 additions & 0 deletions src/pages/nuguPatch/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export const ChipWrapper = styled.div`
margin-top: 1rem;
`;
export const ErrorMessage = styled.p`
color: ${({ theme }) => theme.colors.red200};
${({ theme }) => theme.fonts.pretendardB4};
font-size: 0.8rem;
`;
7 changes: 1 addition & 6 deletions src/pages/nuguTest/ChallengerTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ export const ChallengerTest = () => {
const ResulthighlightIndex = result?.findIndex(
(user) => user.id === challengerId
);
console.log("ResulthighlightIndex", ResulthighlightIndex);
console.log(
"selectedAnswer[currentQuestion]",
selectedAnswer[currentQuestion]
);
console.log("rank[currentQuestion]", rank[currentQuestion]);

//μ •λ‹΅ 비ꡐ
const isAnswerCorrect =
Number(rank[currentQuestion]) == selectedAnswer[currentQuestion]
Expand Down
1 change: 0 additions & 1 deletion src/pages/nuguTest/NuguChallenge.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Cookies from "js-cookie";
export const NuguChallenge = () => {
const setTestUser = useSetRecoilState(testUser);
const navigate = useNavigate();
const isTestMake = true;

const moveOnTest = () => {
navigate(`/challenge/test/${Cookies.get("uuid")}`);
Expand Down

0 comments on commit e69d9a7

Please sign in to comment.