-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.tsx
More file actions
284 lines (262 loc) · 7.47 KB
/
index.tsx
File metadata and controls
284 lines (262 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { useEffect, useState } from 'react';
import { Row, Col } from 'react-bootstrap';
import {
useParams,
useSearchParams,
useNavigate,
useLocation,
} from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Pagination, CustomSidebar } from '@/components';
import { loggedUserInfoStore, toastStore } from '@/stores';
import { scrollToElementTop, scrollToDocTop } from '@/utils';
import { usePageTags, usePageUsers } from '@/hooks';
import type {
ListResult,
QuestionDetailRes,
AnswerItem,
} from '@/common/interface';
import { questionDetail, getAnswers } from '@/services';
import {
Question,
Answer,
AnswerHead,
RelatedQuestions,
WriteAnswer,
Alert,
ContentLoader,
InviteToAnswer,
} from './components';
import './index.scss';
const Index = () => {
const navigate = useNavigate();
const { t } = useTranslation('translation');
const { qid = '', slugPermalink = '' } = useParams();
/**
* Note: Compatible with Permalink
*/
let { aid = '' } = useParams();
if (!aid && slugPermalink) {
aid = slugPermalink;
}
const [urlSearch] = useSearchParams();
const page = Number(urlSearch.get('page') || 0);
const order = urlSearch.get('order') || '';
const [question, setQuestion] = useState<QuestionDetailRes | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [answers, setAnswers] = useState<ListResult<AnswerItem>>({
count: -1,
list: [],
});
const { setUsers } = usePageUsers();
const userInfo = loggedUserInfoStore((state) => state.user);
const isAuthor = userInfo?.username === question?.user_info?.username;
const isAdmin = userInfo?.role_id === 2;
const isModerator = userInfo?.role_id === 3;
const isLogged = Boolean(userInfo?.access_token);
const loggedUserRank = userInfo?.rank;
const { state: locationState } = useLocation();
useEffect(() => {
if (locationState?.isReview) {
toastStore.getState().show({
msg: t('review', { keyPrefix: 'toast' }),
variant: 'warning',
});
}
}, [locationState]);
const requestAnswers = async () => {
const res = await getAnswers({
order: order === 'updated' ? order : 'default',
question_id: qid,
page: 1,
page_size: 999,
});
if (res) {
res.list = res.list?.filter((v) => {
// delete answers only show to author and admin and has search params aid
if (v.status === 10) {
if (
(v?.user_info?.username === userInfo?.username || isAdmin) &&
aid === v.id
) {
return v;
}
return null;
}
return v;
});
setAnswers({ ...res, count: res.list.length });
if (page > 0 || order) {
// scroll into view;
const element = document.getElementById('answerHeader');
scrollToElementTop(element);
}
res.list.forEach((item) => {
setUsers([
{
displayName: item.user_info?.display_name,
userName: item.user_info?.username,
},
{
displayName: item?.update_user_info?.display_name,
userName: item?.update_user_info?.username,
},
]);
});
}
};
const getDetail = async () => {
setIsLoading(true);
try {
const res = await questionDetail(qid);
if (res) {
setUsers([
{
id: res.user_info?.id,
displayName: res.user_info?.display_name,
userName: res.user_info?.username,
avatar_url: res.user_info?.avatar,
},
{
id: res?.update_user_info?.id,
displayName: res?.update_user_info?.display_name,
userName: res?.update_user_info?.username,
avatar_url: res?.update_user_info?.avatar,
},
{
id: res?.last_answered_user_info?.id,
displayName: res?.last_answered_user_info?.display_name,
userName: res?.last_answered_user_info?.username,
avatar_url: res?.last_answered_user_info?.avatar,
},
]);
setQuestion(res);
}
setIsLoading(false);
} catch (e) {
setIsLoading(false);
}
};
const initPage = (type: string) => {
if (type === 'delete_question') {
setTimeout(() => {
navigate('/', { replace: true });
}, 1000);
return;
}
if (type === 'default') {
scrollToDocTop();
getDetail();
return;
}
requestAnswers();
};
const writeAnswerCallback = (obj: AnswerItem) => {
setAnswers({
count: answers.count + 1,
list: [...answers.list, obj],
});
if (question) {
setQuestion({
...question,
answered: true,
});
}
};
useEffect(() => {
if (!qid) {
return;
}
scrollToDocTop();
getDetail();
requestAnswers();
}, [qid]);
useEffect(() => {
if (page || order) {
requestAnswers();
}
}, [page, order]);
usePageTags({
title: question?.title,
description: question?.description,
keywords: question?.tags.map((_) => _.slug_name).join(','),
});
const showInviteToAnswer = question?.id;
let canInvitePeople = false;
if (showInviteToAnswer && Array.isArray(question.extends_actions)) {
const inviteAct = question.extends_actions.find((op) => {
return op.action === 'invite_other_to_answer';
});
if (inviteAct) {
canInvitePeople = true;
}
}
return (
<Row className="answer_question-detail-page questionDetailPage pt-4 mb-5">
<Col className="page-main flex-auto">
{question?.operation?.level && <Alert data={question.operation} />}
{isLoading ? (
<ContentLoader />
) : (
<Question
data={question}
initPage={initPage}
hasAnswer={answers.count > 0}
isLogged={isLogged}
/>
)}
{!isLoading && answers.count > 0 && (
<>
<AnswerHead count={answers.count} order={order} />
{answers?.list?.map((item) => {
return (
<Answer
aid={aid}
key={item?.id}
data={item}
questionTitle={question?.title || ''}
slugTitle={question?.url_title}
canAccept={isAuthor || isAdmin || isModerator}
callback={initPage}
isLogged={isLogged}
/>
);
})}
</>
)}
{!isLoading && Math.ceil(answers.count / 15) > 1 && (
<div className="d-flex justify-content-center answer-item pt-4">
<Pagination
currentPage={Number(page || 1)}
pageSize={15}
totalSize={answers?.count || 0}
/>
</div>
)}
{!isLoading &&
Number(question?.status) !== 2 &&
!question?.operation?.type && (
<WriteAnswer
data={{
qid,
answered: question?.answered,
loggedUserRank,
}}
callback={writeAnswerCallback}
/>
)}
</Col>
<Col className="page-right-side mt-4 mt-xl-0">
<CustomSidebar />
<RelatedQuestions id={question?.id || ''} />
{showInviteToAnswer ? (
<InviteToAnswer
questionId={question.id}
readOnly={!canInvitePeople}
/>
) : null}
</Col>
</Row>
);
};
export default Index;