Skip to content

Commit

Permalink
Merge pull request #98 from next-petree/feat_login
Browse files Browse the repository at this point in the history
Feat login
  • Loading branch information
ChanghyeonO authored Jan 21, 2024
2 parents 877aef3 + a06e9da commit fea6e2d
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 105 deletions.
6 changes: 3 additions & 3 deletions src/api/token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export const setToken = (token: string, value: string) => {
localStorage.setItem(token, value);
sessionStorage.setItem(token, value);
};
export const getToken = (token: string) => {
return localStorage.getItem(token);
return sessionStorage.getItem(token);
};
export const removeToken = (token: string) => {
localStorage.removeItem(token);
sessionStorage.removeItem(token);
};
22 changes: 11 additions & 11 deletions src/component/Answer/Answer.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import React, { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import React, { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import {
Wrapper,
AnswerTitle,
SubTitle,
AnswerWrapper,
Btn,
} from './AnswerStyle';
import AnswerComp1 from './AnswerComp1/AnswerComp1';
import { QuestionType, AnswersType, ResultType } from '../../types/index';
} from "./AnswerStyle";
import AnswerComp1 from "./AnswerComp1/AnswerComp1";
import { QuestionType, AnswersType, ResultType } from "../../types/index";

export default function AnswerComp() {
const navigate = useNavigate();
const storedQuestions: QuestionType = JSON.parse(
localStorage.getItem('questions') || '[]'
sessionStorage.getItem("questions") || "[]",
);
const storedUserAnswers: AnswersType = JSON.parse(
localStorage.getItem('userAnswers') || '[]'
sessionStorage.getItem("userAnswers") || "[]",
);
const storedResult: ResultType = JSON.parse(
localStorage.getItem('result') || '{}'
sessionStorage.getItem("result") || "{}",
);

function moveBack() {
Expand All @@ -33,10 +33,10 @@ export default function AnswerComp() {
<AnswerWrapper>
{storedQuestions.map((question, index) => {
const result = storedResult.explanations.find(
(res) => res.questionId === question.id
res => res.questionId === question.id,
);
const userAnswer = storedUserAnswers.find(
(answer) => answer.questionId === question.id
answer => answer.questionId === question.id,
);
return (
<AnswerComp1
Expand All @@ -46,7 +46,7 @@ export default function AnswerComp() {
num={index}
question={question.questionText}
choices={question.choices}
solution={result?.explanationText || ''}
solution={result?.explanationText || ""}
correctChoiceId={result?.correctChoiceId}
selectedChoiceId={userAnswer?.selectedChoiceId} // selectedChoiceId๋ฅผ props๋กœ ์ „๋‹ฌ
/>
Expand Down
92 changes: 46 additions & 46 deletions src/component/BasicTest/BasicTest.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { get, post } from '../../api/api';
import TestComp from './BasicTestComp/TestComp';
import petfoot from '../../assets/icons/test_pets_black_24dp.png';
import petree from '../../assets/images/test_petree.png';
import TestBG from '../../assets/images/TestBG.png';
import LoginModal from '../Modal/LoginModal';
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { get, post } from "../../api/api";
import TestComp from "./BasicTestComp/TestComp";
import petfoot from "../../assets/icons/test_pets_black_24dp.png";
import petree from "../../assets/images/test_petree.png";
import TestBG from "../../assets/images/TestBG.png";
import LoginModal from "../Modal/LoginModal";
import {
Choice,
Question,
TestResponse,
ResultResponse,
} from '../../types/index';
} from "../../types/index";

import {
Wrapper,
Expand All @@ -23,7 +23,7 @@ import {
MoveBtnWrap,
PreBtn,
NextBtn,
} from './BasicTestStyle';
} from "./BasicTestStyle";

const BasicTest: React.FC = () => {
const [testData, setTestData] = useState<Question[] | null>(null);
Expand All @@ -35,29 +35,29 @@ const BasicTest: React.FC = () => {
const [selectedExample, setSelectedExample] = useState<number | null>(null);
const [isLoggedIn, setIsLoggedIn] = useState(false); // ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ ๋ณ€์ˆ˜
const [showLoginModal, setShowLoginModal] = useState(false); // ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ์„ ๋ณด์—ฌ์ค„์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ƒํƒœ ๋ณ€์ˆ˜
const [errorMessage, setErrorMessage] = useState<string>(''); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ ๋ณ€์ˆ˜
const [errorMessage, setErrorMessage] = useState<string>(""); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ƒํƒœ ๋ณ€์ˆ˜
const navigate = useNavigate();

useEffect(() => {
localStorage.setItem('userAnswers', JSON.stringify(userAnswers));
sessionStorage.setItem("userAnswers", JSON.stringify(userAnswers));
}, [userAnswers]);

useEffect(() => {
const accessToken = localStorage.getItem('accessToken');
const accessToken = sessionStorage.getItem("accessToken");
setIsLoggedIn(!!accessToken);
// ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์—์„œ ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const getTest = async () => {
try {
const response = await get<TestResponse>('/basic-test/start');
const response = await get<TestResponse>("/basic-test/start");

if (response.data.status === 'SUCCESS') {
if (response.data.status === "SUCCESS") {
// ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์— ๋ฌธ์ œ ๋ฐ์ดํ„ฐ ์ €์žฅ
localStorage.setItem(
'questions',
JSON.stringify(response.data.data.questions)
sessionStorage.setItem(
"questions",
JSON.stringify(response.data.data.questions),
);
const storedQuestions = JSON.parse(
localStorage.getItem('questions') || '[]'
sessionStorage.getItem("questions") || "[]",
);
setTestData(storedQuestions);
}
Expand All @@ -66,14 +66,14 @@ const BasicTest: React.FC = () => {
{
id: 1,
questionText:
'๊ฐœ๋ฅผ ํฌ๊ฒŒ ์†Œํ˜•๊ฒฌ, ์ค‘ํ˜•๊ฒฌ, ๋Œ€ํ˜•๊ฒฌ์œผ๋กœ ๋‚˜๋ˆŒ๋•Œ ์†Œํ˜•๊ฒฌ์— ํ•ด๋‹นํ•˜๋Š” ์„ฑ๊ฒฌ์˜ ์ฒด์ค‘๊ณผ ์ฒด๊ณ ๋Š” ๋ฌด์—‡์ธ๊ฐ€?',
"๊ฐœ๋ฅผ ํฌ๊ฒŒ ์†Œํ˜•๊ฒฌ, ์ค‘ํ˜•๊ฒฌ, ๋Œ€ํ˜•๊ฒฌ์œผ๋กœ ๋‚˜๋ˆŒ๋•Œ ์†Œํ˜•๊ฒฌ์— ํ•ด๋‹นํ•˜๋Š” ์„ฑ๊ฒฌ์˜ ์ฒด์ค‘๊ณผ ์ฒด๊ณ ๋Š” ๋ฌด์—‡์ธ๊ฐ€?",
TestNum: 1,
choices: [
{ id: 1, choiceText: '์ฒด์ค‘ 10kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ' },
{ id: 2, choiceText: '์ฒด์ค‘ 20kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ' },
{ id: 3, choiceText: '์ฒด์ค‘ 30kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ' },
{ id: 4, choiceText: '์ฒด์ค‘ 40kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ' },
{ id: 5, choiceText: '์ฒด์ค‘ 50kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ' },
{ id: 1, choiceText: "์ฒด์ค‘ 10kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ" },
{ id: 2, choiceText: "์ฒด์ค‘ 20kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ" },
{ id: 3, choiceText: "์ฒด์ค‘ 30kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ" },
{ id: 4, choiceText: "์ฒด์ค‘ 40kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ" },
{ id: 5, choiceText: "์ฒด์ค‘ 50kg์ดํ•˜, ์ฒด๊ณ  40cm ๋ฏธ๋งŒ" },
],
},
]);
Expand All @@ -85,21 +85,21 @@ const BasicTest: React.FC = () => {

useEffect(() => {
if (!isLoggedIn) {
setErrorMessage('์„œ๋น„์Šค ์ด์šฉ์„ ์œ„ํ•ด ๋กœ๊ทธ์ธ์„ ํ•˜์„ธ์š”.');
setErrorMessage("์„œ๋น„์Šค ์ด์šฉ์„ ์œ„ํ•ด ๋กœ๊ทธ์ธ์„ ํ•˜์„ธ์š”.");
} else {
setErrorMessage('');
setErrorMessage("");
}
}, [isLoggedIn]);

const handleAnswer = (answer: string) => {
if (testData) {
const selectedChoice = testData[currentQuestionIndex].choices.find(
(choice) => choice.choiceText === answer
choice => choice.choiceText === answer,
);

if (selectedChoice) {
setSelectedAnswer(selectedChoice.id);
setSelectedExample(selectedChoice.id); // ์„ ํƒํ•œ Example ์—…๋ฐ์ดํŠธ
setSelectedExample(selectedChoice.id);
}
}
};
Expand All @@ -112,7 +112,7 @@ const BasicTest: React.FC = () => {
if (selectedAnswer !== null && testData) {
const currentQuestionId = testData[currentQuestionIndex].id;
const existingAnswerIndex = userAnswers.findIndex(
(answer) => answer.questionId === currentQuestionId
answer => answer.questionId === currentQuestionId,
);
let newUserAnswers;
if (existingAnswerIndex !== -1) {
Expand All @@ -131,32 +131,32 @@ const BasicTest: React.FC = () => {
}

setUserAnswers(newUserAnswers);
localStorage.setItem('userAnswers', JSON.stringify(newUserAnswers));
sessionStorage.setItem("userAnswers", JSON.stringify(newUserAnswers));

if (currentQuestionIndex + 1 >= testData.length) {
try {
const response = await post<ResultResponse>('/basic-test/submit', {
const response = await post<ResultResponse>("/basic-test/submit", {
answers: newUserAnswers,
});
localStorage.setItem('result', JSON.stringify(response.data));
navigate('/result');
sessionStorage.setItem("result", JSON.stringify(response.data));
navigate("/result");
} catch (error: any) {
console.log('error', error);
console.log("error", error);
if (
error.response &&
error.response?.data &&
error.response?.data?.data === '๋ถ„์–‘ํฌ๋ง์ž ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค.'
error.response?.data?.data === "๋ถ„์–‘ํฌ๋ง์ž ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค."
) {
window.scroll({ top: 0, behavior: 'smooth' }); // ์Šคํฌ๋กค์„ ํŽ˜์ด์ง€ ์ƒ๋‹จ์œผ๋กœ ์ด๋™
setErrorMessage('๋ถ„์–‘ ํฌ๋ง์ž ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค.'); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
window.scroll({ top: 0, behavior: "smooth" }); // ์Šคํฌ๋กค์„ ํŽ˜์ด์ง€ ์ƒ๋‹จ์œผ๋กœ ์ด๋™
setErrorMessage("๋ถ„์–‘ ํฌ๋ง์ž ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค."); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
setShowLoginModal(true); // ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ ๋ณด์—ฌ์ฃผ๊ธฐ
} else if (
error.response?.data?.data === 'ํ•ด๋‹น ํšŒ์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.'
error.response?.data?.data === "ํ•ด๋‹น ํšŒ์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
) {
setErrorMessage('ํ•ด๋‹น ํšŒ์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.'); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
setErrorMessage("ํ•ด๋‹น ํšŒ์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
setShowLoginModal(true); // ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ ๋ณด์—ฌ์ฃผ๊ธฐ
} else {
setErrorMessage('๋‹ต๋ณ€ ์ œ์ถœ ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.'); // ๊ธฐ๋ณธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
setErrorMessage("๋‹ต๋ณ€ ์ œ์ถœ ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค."); // ๊ธฐ๋ณธ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์„ค์ •
}
}
} else {
Expand Down Expand Up @@ -185,10 +185,10 @@ const BasicTest: React.FC = () => {
{/* ๋กœ๊ทธ์ธ ๋ชจ๋‹ฌ ์ถ”๊ฐ€ */}
{errorMessage && (
<LoginModal
top={'8'}
top={"8"}
onLogin={() => {
setIsLoggedIn(true);
setErrorMessage(''); // ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ดˆ๊ธฐํ™”
setErrorMessage(""); // ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ดˆ๊ธฐํ™”
}}
onClose={() => setShowLoginModal(false)}
errorMessage={errorMessage} // ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ prop ์ „๋‹ฌ
Expand All @@ -205,10 +205,10 @@ const BasicTest: React.FC = () => {
{currentQuestion && (
<>
<TestComp
TestNum={(currentQuestionIndex + 1).toString().padStart(2, '0')}
TestNum={(currentQuestionIndex + 1).toString().padStart(2, "0")}
Question={currentQuestion.questionText}
Example={currentQuestion.choices.map(
(choice) => choice.choiceText
choice => choice.choiceText,
)}
onAnswer={handleAnswer}
selectedExample={selectedExample}
Expand All @@ -220,7 +220,7 @@ const BasicTest: React.FC = () => {
<NextBtn onClick={handleNext}>๋‹ค์Œ</NextBtn>
</>
) : (
<NextBtn style={{ width: '100%' }} onClick={handleNext}>
<NextBtn style={{ width: "100%" }} onClick={handleNext}>
๋‹ค์Œ
</NextBtn>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/component/Certify/CertifyComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default function CertifyComp() {
}
};
useEffect(() => {
const accessToken = localStorage.getItem("accessToken");
const accessToken = sessionStorage.getItem("accessToken");
setIsLoggedIn(!!accessToken);
}, []);

Expand Down
4 changes: 2 additions & 2 deletions src/component/Dropdown/NavDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const NavDropdown = ({ loggedIn }: IDropdownProps) => {
const handleLogoutClick = async () => {
try {
const res = await get<ILogoutResponse>("/logout");
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
sessionStorage.removeItem("accessToken");
sessionStorage.removeItem("refreshToken");
if (res.data.status === "SUCCESS") {
await Swal.fire({
...alertList.successMessage(res.data.data),
Expand Down
4 changes: 2 additions & 2 deletions src/component/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const Header = () => {

useEffect(() => {
if (
localStorage.getItem("accessToken") &&
localStorage.getItem("refreshToken")
sessionStorage.getItem("accessToken") &&
sessionStorage.getItem("refreshToken")
) {
setIsLoggedIn(true);
dispath(setProfileImg(localStorage.getItem("profileImg")!))
Expand Down
4 changes: 2 additions & 2 deletions src/component/KakaoCallback/KakaoCallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const KakaoCallback = () => {
);

if (response.data.status === "SUCCESS") {
localStorage.setItem("accessToken", response.data.data.accessToken);
localStorage.setItem("refreshToken", response.data.data.refreshToken);
sessionStorage.setItem("accessToken", response.data.data.accessToken);
sessionStorage.setItem("refreshToken", response.data.data.refreshToken);
navigate("/");
} else if (response.data.status === "FAIL") {
Swal.fire(alertList.errorMessage("์นด์นด์˜ค์— ์—ฐ๋™๋œ ๊ณ„์ •์ด ์—†์Šต๋‹ˆ๋‹ค."));
Expand Down
12 changes: 4 additions & 8 deletions src/component/LoginContent/LoginContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,10 @@ const LoginContent = () => {
if (response.data.status === "SUCCESS") {
await Swal.fire(alertList.successMessage("๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค."));

localStorage.setItem("accessToken", response.data.data.accessToken);
localStorage.setItem("refreshToken", response.data.data.refreshToken);
if(response.data.data.profileImgUrl) {
localStorage.setItem("profileImg", response.data.data.profileImgUrl);
}
else {
localStorage.setItem("profileImg", "")
}
sessionStorage.setItem("accessToken", response.data.data.accessToken);
sessionStorage.setItem("refreshToken", response.data.data.refreshToken);

dispath(setProfileImg(response.data.data.profileImgUrl!));
navigate("/");
} else if (response.data.status === "FAIL") {
await Swal.fire(alertList.errorMessage(`${response.data.data}`));
Expand Down
7 changes: 4 additions & 3 deletions src/component/Modal/LoginModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const LoginModal: React.FC<LoginModalProps> = ({

if (response.data.status === "SUCCESS") {
console.log("๋กœ๊ทธ์•„์›ƒ!", response.data);
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
sessionStorage.removeItem("accessToken");
sessionStorage.removeItem("refreshToken");
}
} catch (error: any) {
console.log(error);
Expand All @@ -62,7 +62,8 @@ const LoginModal: React.FC<LoginModalProps> = ({
};

const isLoggedIn =
localStorage.getItem("accessToken") && localStorage.getItem("refreshToken");
sessionStorage.getItem("accessToken") &&
sessionStorage.getItem("refreshToken");

return (
<Modal ref={modalRef} style={{ top: `${top}%` }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,23 @@ const TableComp = ({
>
์ƒ์„ธ๋ณด๊ธฐ
</Button>
) : cell.column.id === "state" &&
cell.value === "๋ถ„์–‘์Šน์ธ" ? (
) : cell.column.id === "state" ? (
<>
{cell.render("Cell")}
<Button
bgcolor="#4EC1BF"
buttonwidth="70px;"
buttonheight="40px;"
onClick={() => {
console.log("ํ›„๊ธฐ ์ž‘์„ฑ!!");
}}
>
ํ›„๊ธฐ์ž‘์„ฑ
</Button>
<span style={{ marginRight: "10px" }}>
{cell.render("Cell")}
</span>
{cell.value === "๋ถ„์–‘์Šน์ธ" && (
<Button
bgcolor="#4EC1BF"
buttonwidth="70px;"
buttonheight="40px;"
onClick={() => {
console.log("ํ›„๊ธฐ ์ž‘์„ฑ!!");
}}
>
ํ›„๊ธฐ์ž‘์„ฑ
</Button>
)}
</>
) : (
cell.render("Cell")
Expand Down
Loading

0 comments on commit fea6e2d

Please sign in to comment.