feat(Question/Ask): Automatically set the tags on the url parameter when creating a question

This commit is contained in:
haitaoo
2023-02-15 16:10:33 +08:00
parent 027ecf294c
commit d262f1fd8a
2 changed files with 19 additions and 1 deletions
+14 -1
View File
@@ -1,6 +1,6 @@
import React, { useState, useEffect, useRef } from 'react';
import { Container, Row, Col, Form, Button, Card } from 'react-bootstrap';
import { useParams, useNavigate } from 'react-router-dom';
import { useParams, useNavigate, useSearchParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
@@ -16,6 +16,7 @@ import {
useQueryRevisions,
postAnswer,
useQueryQuestionByTitle,
getTagsBySlugName,
} from '@/services';
import { handleFormError } from '@/utils';
import { pathFactory } from '@/router/pathFactory';
@@ -77,6 +78,17 @@ const Ask = () => {
const { qid } = useParams();
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const initQueryTags = () => {
const queryTags = searchParams.get('tags');
if (!queryTags) {
return;
}
getTagsBySlugName(queryTags).then((tags) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleTagsChange(tags);
});
};
const isEdit = qid !== undefined;
const { data: similarQuestions = { list: [] } } = useQueryQuestionByTitle(
@@ -85,6 +97,7 @@ const Ask = () => {
useEffect(() => {
if (!isEdit) {
resetForm();
initQueryTags();
}
}, [isEdit]);
const { data: revisions = [] } = useQueryRevisions(qid);
+5
View File
@@ -58,3 +58,8 @@ export const useTagInfo = ({ id = '', name = '' }) => {
export const followTags = (params) => {
return request.put('/answer/api/v1/follow/tags', params);
};
export const getTagsBySlugName = (slugNames: string) => {
const apiUrl = `/answer/api/v1/tags?tags=${encodeURIComponent(slugNames)}`;
return request.get<Type.TagInfo[]>(apiUrl);
};