Skip to content

Commit a801499

Browse files
authored
Merge pull request #132 from codestates-seb/client/fix/jwo0o0/answers
[FE] fix: Redux 적용해서 상태관리 추가
2 parents a95ca9d + aabd2b7 commit a801499

20 files changed

+179
-80
lines changed

client/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2-
2+
../.idea/
33
# dependencies
44
/node_modules
55
/.pnp

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"web-vitals": "^2.1.4"
2323
},
2424
"scripts": {
25-
"start": "react-scripts start",
25+
"start": "GENERATE_SOURCEMAP=false react-scripts start",
2626
"build": "react-scripts build",
2727
"test": "react-scripts test",
2828
"eject": "react-scripts eject",

client/src/App.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const App = () => {
3333
<Route path="/signup" element={<Signup />} />
3434
<Route path="/questions" element={<QuestionsPage />} />
3535
<Route path="/questions/ask" element={<AskQuestionPage />} />
36-
<Route path="/questions/0" element={<QuestionDetailPage />} />
36+
<Route
37+
path="/questions/:question_id"
38+
element={<QuestionDetailPage />}
39+
/>
3740
<Route path="/tags" element={<TagsPage />} />
3841
<Route path="/users" element={<UsersPage />} />
3942
<Route path="*" element={<NoMatch />} />

client/src/_actions/answer_action.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import axios from 'axios';
2+
import { QUESTIONS_URL } from '../api/requests';
3+
4+
export const GET_ANSWERS = 'GET_ANSWERS';
5+
6+
export const getAnswers = async (question_id) => {
7+
const payload = await axios
8+
.get(`${QUESTIONS_URL}/${question_id}/answers?page=1&size=30`)
9+
.catch((error) => console.error(error));
10+
11+
return {
12+
type: GET_ANSWERS,
13+
payload: payload.data,
14+
};
15+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import axios from 'axios';
2+
import { QUESTIONS_URL } from '../api/requests';
3+
4+
export const SET_QUESTION_ID = 'SET_QUESTION_ID';
5+
export const GET_QUESTION = 'GET_QUESTION';
6+
7+
export const setQuestionId = (question_id) => {
8+
return {
9+
type: SET_QUESTION_ID,
10+
payload: question_id,
11+
};
12+
};
13+
14+
export const getQuestion = async (question_id) => {
15+
const payload = await axios
16+
.get(`${QUESTIONS_URL}/${question_id}`)
17+
.catch((error) => console.error(error));
18+
19+
return {
20+
type: GET_QUESTION,
21+
payload: payload.data,
22+
};
23+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { GET_ANSWERS } from '../_actions/answer_action';
2+
3+
const initialState = {
4+
data: {},
5+
};
6+
export const answerReducer = (state = initialState, action) => {
7+
switch (action.type) {
8+
case GET_ANSWERS:
9+
return { ...state, data: action.payload };
10+
default:
11+
return state;
12+
}
13+
};

client/src/_reducers/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { combineReducers } from 'redux';
22

33
import users from './users_reducer';
4-
import answer from './answer_reducer';
54
import questions from './questions_reducer';
65
import search from './search_reducer';
76
import tags from './tags_reducer';
7+
import { questionReducer } from './question_reducer';
8+
import { answerReducer } from './answer_reducer';
89

910
const rootReducer = combineReducers({
1011
users,
11-
answer,
1212
questions,
1313
search,
1414
tags,
15+
questionReducer,
16+
answerReducer,
1517
});
1618

1719
export default rootReducer;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { GET_QUESTION, SET_QUESTION_ID } from '../_actions/question_action';
2+
3+
const initialState = {
4+
question_id: 1,
5+
data: {},
6+
};
7+
8+
export const questionReducer = (state = initialState, action) => {
9+
switch (action.type) {
10+
case SET_QUESTION_ID:
11+
return { ...state, question_id: action.payload };
12+
case GET_QUESTION:
13+
return { ...state, data: action.payload };
14+
default:
15+
return state;
16+
}
17+
};

client/src/pages/HomePage/HomePage/QuestionList/SummaryContent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const SummaryContent = () => {
5757
return (
5858
<SummaryContentWrapper>
5959
<SummaryContentTitle>
60-
<SummaryContentTitleLink to="/questions/0">
60+
<SummaryContentTitleLink to="/questions/1">
6161
Function not working correctly, x != (y or z)
6262
</SummaryContentTitleLink>
6363
</SummaryContentTitle>

client/src/pages/QuestionDetail/Answers/QuestionAnswers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ const AnswerConatiner = styled.div`
99
border-bottom: 1px solid #e3e6e8;
1010
`;
1111

12-
//GET 요청: '/questions/{question-id}/answers?page=1
13-
1412
const answersData = [0, 0, 0, 0, 0, 0, 0];
1513

1614
const QuestionAnswers = () => {

client/src/pages/QuestionDetail/MainQuestion/MainQuestion.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const MainQuestionContainer = styled.div`
77
flex-grow: 1;
88
`;
99

10-
const MainQuestion = () => {
10+
const MainQuestion = ({ data }) => {
1111
return (
1212
<MainQuestionContainer>
13-
<QuestionVote />
14-
<QuestionContentBox />
13+
<QuestionVote data={data} />
14+
<QuestionContentBox data={data} />
1515
</MainQuestionContainer>
1616
);
1717
};

client/src/pages/QuestionDetail/MainQuestion/QuestionBody.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ const QuestionBodyBottom = styled.div`
1616
justify-content: space-between;
1717
`;
1818

19-
const QuestionBody = () => {
19+
const QuestionBody = ({ data }) => {
20+
const problem = data === undefined ? '' : data.problem;
21+
const expect = data === undefined ? '' : data.expect;
22+
const tagList = data === undefined ? [] : data.tagList;
23+
const userName = data === undefined ? '' : data.userResponseDto.nickName;
24+
const createdDate = data === undefined ? '' : data.createdAt;
25+
2026
return (
2127
<QuestionBodyContainer>
22-
<QuestionBodyTxt />
23-
<QuestionTags />
28+
<QuestionBodyTxt text={problem} />
29+
<QuestionBodyTxt text={expect} />
30+
<QuestionTags tagList={tagList} />
2431
<QuestionBodyBottom>
2532
<QuestionBodyBtns />
26-
<QuestionUserinfo />
33+
<QuestionUserinfo userName={userName} createdAt={createdDate} />
2734
</QuestionBodyBottom>
2835
</QuestionBodyContainer>
2936
);

client/src/pages/QuestionDetail/MainQuestion/QuestionBodyTxt.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ const QuestionBodyTxtContainer = styled.div`
1010
color: #232629;
1111
`;
1212

13-
//여기서 axios 요청(아마?)
14-
const body = `I am trying to list out duplicate elements in the integer list say for eg,
15-
16-
List<Integer> numbers = Arrays.asList(new Integer[]{1,2,1,3,4,4});
17-
using Streams of jdk 8. Has anybody tried out. To remove the duplicates we can use the distinct() api. But what about finding the duplicated elements ? Anybody can help me out ?`;
18-
const QuestionBodyTxt = () => {
19-
return <QuestionBodyTxtContainer>{body}</QuestionBodyTxtContainer>;
13+
const QuestionBodyTxt = ({ text }) => {
14+
return <QuestionBodyTxtContainer>{text}</QuestionBodyTxtContainer>;
2015
};
2116

2217
export default QuestionBodyTxt;
Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
import Comments from '../Comments/Comments';
22

3-
// '/comments?page={}'로 axios 요청 -> data에서 commentType = 'QUESTION'인 것만
4-
const CommentData = [
5-
{
6-
commentId: 1,
7-
memberResponseDto: {
8-
memberId: 1,
9-
nickName: 'Tagir Valeev',
10-
11-
},
12-
createdAt: '2022-10-28T18:37:47.0798803',
13-
body: 'possible duplicate of Collect stream with grouping, counting and filtering operations',
14-
commentType: 'QUESTION',
15-
},
16-
{
17-
commentId: 2,
18-
memberResponseDto: {
19-
memberId: 2,
20-
nickName: 'Ravn Andersen',
21-
22-
},
23-
body: `If you don't want to collect the stream, this essentially boils down to "how can I look at more than one item at once in a stream"?`,
24-
createdAt: '2022-10-31T09:47:42.482746',
25-
commentType: 'QUESTION',
26-
},
27-
{
28-
commentId: 3,
29-
memberResponseDto: {
30-
memberId: 3,
31-
nickName: 'Saroj Kumar Sahoo',
32-
33-
},
34-
body: `Set<Integer> items = new HashSet(); numbers.stream().filter(n -> i!tems.add(n)).collect(Collectors.toSet()); `,
35-
createdAt: '2022-10-28T13:12:48.456698',
36-
commentType: 'QUESTION',
37-
},
38-
];
39-
const QuestionComments = () => {
40-
return <Comments data={CommentData}></Comments>;
3+
// const stupData = [
4+
// {
5+
// commentId: 1,
6+
// memberResponseDto: {
7+
// memberId: 1,
8+
// nickName: 'Tagir Valeev',
9+
// email: '[email protected]',
10+
// },
11+
// createdAt: '2022-10-28T18:37:47.0798803',
12+
// body: 'possible duplicate of Collect stream with grouping, counting and filtering operations',
13+
// commentType: 'QUESTION',
14+
// },
15+
// {
16+
// commentId: 2,
17+
// memberResponseDto: {
18+
// memberId: 2,
19+
// nickName: 'Ravn Andersen',
20+
// email: '[email protected]',
21+
// },
22+
// body: `If you don't want to collect the stream, this essentially boils down to "how can I look at more than one item at once in a stream"?`,
23+
// createdAt: '2022-10-31T09:47:42.482746',
24+
// commentType: 'QUESTION',
25+
// },
26+
// {
27+
// commentId: 3,
28+
// memberResponseDto: {
29+
// memberId: 3,
30+
// nickName: 'Saroj Kumar Sahoo',
31+
// email: '[email protected]',
32+
// },
33+
// body: `Set<Integer> items = new HashSet(); numbers.stream().filter(n -> i!tems.add(n)).collect(Collectors.toSet()); `,
34+
// createdAt: '2022-10-28T13:12:48.456698',
35+
// commentType: 'QUESTION',
36+
// },
37+
// ];
38+
const QuestionComments = ({ data }) => {
39+
const commentsData = data === undefined ? [] : data.comments;
40+
return <Comments data={commentsData}></Comments>;
4141
};
4242

4343
export default QuestionComments;

client/src/pages/QuestionDetail/MainQuestion/QuestionTags.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ const Tag = styled.button`
2222
}
2323
`;
2424

25-
const QuestionTags = () => {
25+
const QuestionTags = ({ tagList }) => {
2626
return (
2727
<QuestionTagsContainer>
28-
<Tag>java</Tag>
29-
<Tag>javascript</Tag>
30-
<Tag>MySQL</Tag>
31-
<Tag>React</Tag>
28+
{tagList.map((tag, idx) => {
29+
return <Tag key={idx}>{tag}</Tag>;
30+
})}
3231
</QuestionTagsContainer>
3332
);
3433
};

client/src/pages/QuestionDetail/MainQuestion/QuestionUserinfo.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,20 @@ const UserStats = styled.div`
4242
}
4343
`;
4444

45-
//이것도 axios로 요청??? Question created_time 사용
46-
const AskedDate = ' Dec 28, 2014 at 14:19';
47-
const UserName = 'jwo0o0';
45+
const AskedDate = (str) => {
46+
if (str === '') return '';
47+
const date = new Date(str.slice(0, 19));
48+
const arr = date.toDateString().split(' ');
4849

49-
const QuestionUserinfo = () => {
50+
return `${arr[1]} ${arr[2]}, ${
51+
arr[3]
52+
} at ${date.getHours()}:${date.getMinutes()}`;
53+
};
54+
55+
const QuestionUserinfo = ({ userName, createdAt }) => {
5056
return (
5157
<QuestionUserinfoContainer>
52-
<QuestionDate>asked {AskedDate}</QuestionDate>
58+
<QuestionDate>asked {AskedDate(createdAt)}</QuestionDate>
5359
<UserInfoWrapper>
5460
<UserIcon>
5561
<img
@@ -58,7 +64,7 @@ const QuestionUserinfo = () => {
5864
></img>
5965
</UserIcon>
6066
<UserStats>
61-
<div className="username">{UserName}</div>
67+
<div className="username">{userName}</div>
6268
<div className="stats">1,201 •2 •9 •6</div>
6369
</UserStats>
6470
</UserInfoWrapper>

client/src/pages/QuestionDetail/QuestionContentBox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const ContentBoxContainer = styled.div`
99
padding-right: 10px;
1010
`;
1111

12-
const QuestionContentBox = () => {
12+
const QuestionContentBox = ({ data }) => {
1313
return (
1414
<ContentBoxContainer>
15-
<QuestionBody />
16-
<QuestionComments />
15+
<QuestionBody data={data} />
16+
<QuestionComments data={data} />
1717
</ContentBoxContainer>
1818
);
1919
};

client/src/pages/QuestionDetail/QuestionDetailPage.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
//import { Container } from '../pages/Home/HomePage/HomePage';
1+
import { useParams } from 'react-router-dom';
2+
import { useDispatch, useSelector } from 'react-redux';
3+
import { setQuestionId, getQuestion } from '../../_actions/question_action';
24
import styled from 'styled-components';
35
import Nav from '../../components/Nav';
46
import Footer from '../../components/Footer';
57
import Sidebar from '../../components/Sidebar.js';
68
import QuestionTitle from './QuestionTitle';
79
import MainQuestion from './MainQuestion/MainQuestion';
810
import QuestionAnswers from './Answers/QuestionAnswers';
11+
import { useEffect } from 'react';
912

1013
const MainContent = styled.div`
1114
display: flex;
@@ -31,6 +34,17 @@ const MainWrapper = styled.div`
3134
`;
3235

3336
const QuestionDetailPage = () => {
37+
const dispatch = useDispatch();
38+
const { question_id } = useParams();
39+
40+
const state = useSelector((state) => state.questionReducer);
41+
const { data } = state.data;
42+
43+
useEffect(() => {
44+
dispatch(setQuestionId(question_id));
45+
dispatch(getQuestion(question_id));
46+
}, [dispatch]);
47+
3448
return (
3549
<>
3650
<MainContent>
@@ -39,7 +53,7 @@ const QuestionDetailPage = () => {
3953
<QuestionTitle />
4054
<MainWrapper>
4155
<div>
42-
<MainQuestion />
56+
<MainQuestion data={data} />
4357
<QuestionAnswers />
4458
</div>
4559
<Sidebar />

0 commit comments

Comments
 (0)