Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

유형 검사 결과 출력 api 연결 #84

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/app/api/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fastApiRequest } from './api';

export const getQuestions = () => fastApiRequest('GET', '/user-type/questionnaire');
export const getQuestions = () => fastApiRequest('GET', '/usertype/form');

export const postUserType = (token: string, data: { answers: number[] }) =>
fastApiRequest('POST', '/user-type', data, token);
export const postUserType = (data: { answers: number[] }, token?: string | null) =>
fastApiRequest('POST', '/usertype/calculate', data, token);
22 changes: 15 additions & 7 deletions src/app/test/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import { Box, Button, FormControlLabel, Radio, RadioGroup, Stack } from '@mui/ma
import CommentCard from '@/components/CommentCard';
import GradientBox from '@/components/GradientBox';
import color from '@/constants/color';
import userType from '@/constants/userType';
import { Question } from '@/types';

import { getQuestions } from '../api/test';
import { getQuestions, postUserType } from '../api/test';

const Page = () => {
const [questions, setQuestions] = useState<Question[]>([]);
const [selectedValue, setSelectedValue] = useState<number[]>([]);

const isNotSelected = selectedValue.includes(0);
const isNotSelected = selectedValue.includes(-1);

const handleChange = (index: number, value: number) => {
const newSelectedValue = [...selectedValue];
Expand All @@ -24,14 +25,21 @@ const Page = () => {
};

const handleSubmit = () => {
// TODO: post api 연결
postUserType({ answers: selectedValue }).then((res) => {
if (res.result) {
const resultUserType = userType[res.data.userType];
alert(`당신의 경제 유형은 ${resultUserType} 입니다.\n추후 경제 유형에 따른 기사를 추천해드릴 예정입니다.`);
} else {
throw res.message;
}
});
};

useEffect(() => {
getQuestions().then((res) => {
if (res.result) {
setQuestions(res.data);
setSelectedValue(Array(res.data.length).fill(0));
setSelectedValue(Array(res.data.length).fill(-1));
}
});
}, []);
Expand All @@ -56,9 +64,9 @@ const Page = () => {
value={selectedValue[questions.indexOf(item)]}
onChange={(e) => handleChange(questions.indexOf(item), Number(e.target.value))}
>
<FormControlLabel control={<Radio />} label={item.option1.option} sx={{ width: 220 }} value="1" />
<FormControlLabel control={<Radio />} label={item.option2.option} sx={{ width: 220 }} value="2" />
<FormControlLabel control={<Radio />} label={item.option3.option} sx={{ width: 220 }} value="3" />
<FormControlLabel control={<Radio />} label={item.option1.option} sx={{ width: 220 }} value="0" />
<FormControlLabel control={<Radio />} label={item.option2.option} sx={{ width: 220 }} value="1" />
<FormControlLabel control={<Radio />} label={item.option3.option} sx={{ width: 220 }} value="2" />
</RadioGroup>
</Stack>
))}
Expand Down
9 changes: 9 additions & 0 deletions src/constants/userType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const userType: { [key: string]: string } = {
ISSUE_FINDER: '이슈파인더',
LIFESTYLE_CONSUMER: '라이프스타일 소비자',
ENTERTAINER: '엔터테이너',
TECH_SPECIALIST: '테크 전문가',
PROFESSIONALS: '전문가',
};

export default userType;