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

너소서 폼 작성 부분 #87

Merged
merged 13 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
✨feat: 너소서 폼 작성 - 서버 제대로 목킹하기
  • Loading branch information
SeojinSeojin committed Jan 16, 2022
commit b957cb08ecb7118c12b02f99e3c9f4c9410aa99e
4 changes: 2 additions & 2 deletions src/application/stores/neososeo-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const neososeoFormState = atom<NeososeoFormData | null>({
default: null,
});

export const neoseosoAnswerState = atom<NeososeoAnswerData>({
key: 'neoseosoAnswerState',
export const neososeoAnswerState = atom<NeososeoAnswerData>({
key: 'neososeoAnswerState',
default: {
userID: 0,
formID: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/api/neososeo-form.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NeososeoAnswerData, NeososeoFormData } from './types/neososeo-form';

export interface NeososeoFormService {
getFormInfo(userID: number, formID: number): Promise<NeososeoFormData>;
getFormInfo(userID: string, formID: string): Promise<NeososeoFormData>;
postFormAnswer(body: NeososeoAnswerData): Promise<{ isSuccess: boolean }>;
}
4 changes: 1 addition & 3 deletions src/infrastructure/api/types/neososeo-form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Keyword } from './user';

export type NeososeoFormData = {
title: string;
content: string;
Expand All @@ -16,5 +14,5 @@ export type NeososeoAnswerData = {
name: string;
relation: string;
answer: string;
keyword: Keyword[];
keyword: string[];
};
39 changes: 20 additions & 19 deletions src/presentation/pages/NeososeoForm/Answer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ import { api } from '@api/index';
import { Keyword } from '@api/types/user';
import CommonInput from '@components/common/CommonInput';
import ImmutableKeywordList from '@components/common/Keyword/ImmutableList';
import { neoseosoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { neososeoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { useEffect, useState } from 'react';
import { Link, Outlet, useNavigate } from 'react-router-dom';
import { useRecoilState, useRecoilValue } from 'recoil';
import { StButton, StNeososeoFormLayout, StNeososeoTitle, StSubTitle } from '../style';
import { StTextarea, StKeywordListWrapper } from './style';

function NeososeoFormAnswer() {
const neososeoFormData = useRecoilValue(neososeoFormState);
const [neososeoAnswerState, setNeososeoAnswerState] = useRecoilState(neoseosoAnswerState);
const [keywordList, setKeywordList] = useState<Keyword[]>([]);
const [neososeoAnswer, setNeososeoAnswer] = useRecoilState(neososeoAnswerState);
const navigate = useNavigate();

const postNeososeoForm = async () => {
const response = await api.neososeoFormService.postFormAnswer(neososeoAnswerState);
const response = await api.neososeoFormService.postFormAnswer(neososeoAnswer);
if (response.isSuccess) navigate('../finish');
};

const setAnswer = (answer: string) => setNeososeoAnswerState((prev) => ({ ...prev, answer }));
const setAnswer = (answer: string) => setNeososeoAnswer((prev) => ({ ...prev, answer }));

useEffect(() => {
setNeososeoAnswer((prev) => ({ ...prev, keyword: keywordList.map((k) => k.id) }));
}, [keywordList]);

if (!neososeoFormData) return <></>;
Copy link
Member

@Hyoin-Kim Hyoin-Kim Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요건 무슨 코드이징?.? (진짜몰라서 물어보는거..)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

너소서 폼데이터를 API로 받아올 때 없으면 아무것도 안 렌더링되게 일단 해둔 것!!!


Expand All @@ -41,29 +47,24 @@ function NeososeoFormAnswer() {
/>
</Link>
<StKeywordListWrapper>
<ImmutableKeywordList
keywordList={neososeoAnswerState.keyword}
onItemClick={() => null}
/>
<ImmutableKeywordList keywordList={keywordList} onItemClick={() => null} />
</StKeywordListWrapper>
</div>
<StButton onClick={postNeososeoForm}>답변 작성하기</StButton>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필수값이 빈값이면 버튼 disalbed하는거 넣어주는건 어떨까!!

</StNeososeoFormLayout>
<Outlet
context={{
keywordList: neososeoAnswerState.keyword,
keywordList: keywordList,
addKeyword: (keyword: Keyword) =>
setNeososeoAnswerState((prev) => ({
...prev,
keyword: prev.keyword.map((k) => k.content).includes(keyword.content)
? prev.keyword
: [...prev.keyword, keyword],
})),
setKeywordList((prev) =>
prev.map((prev) => prev.content).includes(keyword.content)
? prev
: [...prev, keyword],
),
removeKeyword: (targetKeyword: Keyword) =>
setNeososeoAnswerState((prev) => ({
...prev,
keyword: prev.keyword.filter((keyword) => keyword.content !== targetKeyword.content),
})),
setKeywordList((prev) =>
prev.filter((keyword) => keyword.content !== targetKeyword.content),
),
targetUser: neososeoFormData.userID,
}}
/>
Expand Down
11 changes: 5 additions & 6 deletions src/presentation/pages/NeososeoForm/Intro/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CommonInput from '@components/common/CommonInput';
import { neoseosoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { neososeoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useRecoilState, useRecoilValue } from 'recoil';
Expand All @@ -14,14 +14,13 @@ import {

function NeososeoFormIntro() {
const neososeoFormData = useRecoilValue(neososeoFormState);
const [neososeoAnswerState, setNeososeoAnswerState] = useRecoilState(neoseosoAnswerState);
const [neososeoAnswer, setNeososeoAnswer] = useRecoilState(neososeoAnswerState);
const navigate = useNavigate();

const setUserName = (userName: string) =>
setNeososeoAnswerState((prev) => ({ ...prev, name: userName }));
setNeososeoAnswer((prev) => ({ ...prev, name: userName }));

const setRelation = (relation: string) =>
setNeososeoAnswerState((prev) => ({ ...prev, relation }));
const setRelation = (relation: string) => setNeososeoAnswer((prev) => ({ ...prev, relation }));

useEffect(() => {
if (!neososeoFormData) return;
Expand All @@ -47,7 +46,7 @@ function NeososeoFormIntro() {
{neososeoFormData.relation.map((relation) => (
<StRelation
key={relation}
selected={neososeoAnswerState.relation === relation}
selected={neososeoAnswer.relation === relation}
onClick={() => setRelation(relation)}
>
{relation}
Expand Down
9 changes: 4 additions & 5 deletions src/presentation/pages/NeososeoForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { api } from '@api/index';
import FormRouter from '@routes/FormRouter';
import { neoseosoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { neososeoAnswerState, neososeoFormState } from '@stores/neososeo-form';
import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { useRecoilState, useSetRecoilState } from 'recoil';
Expand All @@ -9,15 +9,14 @@ import { StNeososeoFormPage, StNeososeoFormHeader } from './style';
function NeososeoFormPage() {
const { userID, formID } = useParams();
const [neososeoForm, setNeososeoForm] = useRecoilState(neososeoFormState);
const setNeoseosoAnswerResponse = useSetRecoilState(neoseosoAnswerState);
const setNeoseosoAnswer = useSetRecoilState(neososeoAnswerState);

useEffect(() => {
if (!userID || !formID) return;
if (isNaN(+userID) || isNaN(+formID)) return;
(async () => {
const data = await api.neososeoFormService.getFormInfo(+userID, +formID);
const data = await api.neososeoFormService.getFormInfo(userID, formID);
setNeososeoForm(data);
setNeoseosoAnswerResponse((prev) => ({ ...prev, userID: data.userID, formID: data.formID }));
setNeoseosoAnswer((prev) => ({ ...prev, userID: data.userID, formID: data.formID }));
})();
}, [userID, formID]);

Expand Down