Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(QuestionList): Remember how to sort the list of questions in local storage

haitaood6b758993cd6
diff --git a/ui/src/common/constants.ts b/ui/src/common/constants.ts
index 35643bda..c8bc1345 100644
--- a/ui/src/common/constants.ts
+++ b/ui/src/common/constants.ts
@@ -8,6 +8,8 @@ export const CAPTCHA_CODE_STORAGE_KEY = '_a_captcha_';
export const DRAFT_QUESTION_STORAGE_KEY = '_a_dq_';
export const DRAFT_ANSWER_STORAGE_KEY = '_a_da_';
export const DRAFT_TIMESIGH_STORAGE_KEY = '|_a_t_s_|';
+export const QUESTIONS_ORDER_STORAGE_KEY = '_a_qok_';
+
export const USER_AGENT_NAMES = {
SegmentFault: 'SegmentFault',
WeChat: 'WeChat',
diff --git a/ui/src/components/QuestionList/index.tsx b/ui/src/components/QuestionList/index.tsx
index 26599c79..d17f8bba 100644
--- a/ui/src/components/QuestionList/index.tsx
+++ b/ui/src/components/QuestionList/index.tsx
@@ -3,8 +3,8 @@ import { ListGroup } from 'react-bootstrap';
import { NavLink, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
+import type { QuestionOrderBy } from '@/common/interface';
import { pathFactory } from '@/router/pathFactory';
-import type * as Type from '@/common/interface';
import {
Tag,
Pagination,
@@ -16,25 +16,32 @@ import {
Counts,
Icon,
} from '@/components';
+import * as Type from '@/common/interface';
-const QuestionOrderKeys: Type.QuestionOrderBy[] = [
+export const QUESTION_ORDER_KEYS: Type.QuestionOrderBy[] = [
'active',
'newest',
'frequent',
'score',
'unanswered',
];
-
interface Props {
source: 'questions' | 'tag';
+ order?: QuestionOrderBy;
data;
isLoading: boolean;
}
-const QuestionList: FC<Props> = ({ source, data, isLoading = false }) => {
+const QuestionList: FC<Props> = ({
+ source,
+ order,
+ data,
+ isLoading = false,
+}) => {
const { t } = useTranslation('translation', { keyPrefix: 'question' });
const [urlSearchParams] = useSearchParams();
- const curOrder = urlSearchParams.get('order') || QuestionOrderKeys[0];
+ const curOrder =
+ order || urlSearchParams.get('order') || QUESTION_ORDER_KEYS[0];
const curPage = Number(urlSearchParams.get('page')) || 1;
const pageSize = 20;
const count = data?.count || 0;
@@ -47,7 +54,7 @@ const QuestionList: FC<Props> = ({ source, data, isLoading = false }) => {
: t('x_questions', { count })}
</h5>
<QueryGroup
- data={QuestionOrderKeys}
+ data={QUESTION_ORDER_KEYS}
currentSort={curOrder}
pathname={source === 'questions' ? '/questions' : ''}
i18nKeyPrefix="question"
diff --git a/ui/src/pages/Questions/index.tsx b/ui/src/pages/Questions/index.tsx
index 0d01afd5..7c0cf5f0 100644
--- a/ui/src/pages/Questions/index.tsx
+++ b/ui/src/pages/Questions/index.tsx
@@ -17,7 +17,9 @@ import {
} from '@/stores';
import { useQuestionList } from '@/services';
import * as Type from '@/common/interface';
-import { userCenter, floppyNavigation } from '@/utils';
+import { userCenter, floppyNavigation, Storage } from '@/utils';
+import { QUESTIONS_ORDER_STORAGE_KEY } from '@/common/constants';
+import { QUESTION_ORDER_KEYS } from '@/components/QuestionList';
const Questions: FC = () => {
const { t } = useTranslation('translation', { keyPrefix: 'question' });
@@ -25,7 +27,12 @@ const Questions: FC = () => {
const { user: loggedUser } = loggedUserInfoStore((_) => _);
const [urlSearchParams] = useSearchParams();
const curPage = Number(urlSearchParams.get('page')) || 1;
- const curOrder = urlSearchParams.get('order') || 'active';
+ const storageOrder = Storage.get(QUESTIONS_ORDER_STORAGE_KEY);
+ const curOrder =
+ urlSearchParams.get('order') || storageOrder || QUESTION_ORDER_KEYS[0];
+ if (curOrder !== storageOrder) {
+ Storage.set(QUESTIONS_ORDER_STORAGE_KEY, curOrder);
+ }
const reqParams: Type.QueryQuestionsReq = {
page_size: 20,
page: curPage,
@@ -50,6 +57,7 @@ const Questions: FC = () => {
<QuestionList
source="questions"
data={listData}
+ order={curOrder}
isLoading={listLoading}
/>
</Col>