From 7cdc4c8c626893d5c5b4b526e8380486e4ee9c7a Mon Sep 17 00:00:00 2001 From: shuai Date: Tue, 26 Sep 2023 14:06:28 +0800 Subject: [PATCH] feat: delete content can recover --- ui/src/common/interface.ts | 1 + ui/src/components/Operate/index.tsx | 10 ++++---- ui/src/pages/Tags/Detail/index.tsx | 2 +- ui/src/pages/Tags/Info/index.tsx | 37 +++++++++++++++++++++++++---- ui/src/services/client/question.ts | 12 ++++++++++ ui/src/services/client/tag.ts | 18 +++++++++++--- 6 files changed, 66 insertions(+), 14 deletions(-) diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts index 16a5fb48..b9cbec70 100644 --- a/ui/src/common/interface.ts +++ b/ui/src/common/interface.ts @@ -56,6 +56,7 @@ export interface TagInfo extends TagBase { updated_at?; main_tag_slug_name?: string; excerpt?; + status: string; } export interface QuestionParams extends ImgCodeReq { title: string; diff --git a/ui/src/components/Operate/index.tsx b/ui/src/components/Operate/index.tsx index 5b83cb40..f93b6926 100644 --- a/ui/src/components/Operate/index.tsx +++ b/ui/src/components/Operate/index.tsx @@ -13,8 +13,8 @@ import { editCheck, reopenQuestion, questionOpetation, - changeQuestionStatus, - changeAnswerStatus, + unDeleteAnswer, + unDeleteQuestion, } from '@/services'; import { tryNormalLogged } from '@/utils/guard'; import { floppyNavigation } from '@/utils'; @@ -151,13 +151,13 @@ const Index: FC = ({ confirmText: t('undelete', { keyPrefix: 'btns' }), onConfirm: () => { if (type === 'question') { - changeQuestionStatus(qid, 'available').then(() => { - callback?.('all'); + unDeleteQuestion(qid).then(() => { + callback?.('default'); }); } if (type === 'answer') { - changeAnswerStatus(aid, 'available').then(() => { + unDeleteAnswer(aid).then(() => { callback?.('all'); }); } diff --git a/ui/src/pages/Tags/Detail/index.tsx b/ui/src/pages/Tags/Detail/index.tsx index 2cde7d04..6b5bc723 100644 --- a/ui/src/pages/Tags/Detail/index.tsx +++ b/ui/src/pages/Tags/Detail/index.tsx @@ -47,7 +47,7 @@ const Index: FC = () => { const { data: tagResp, isLoading } = useTagInfo({ name: curTagName }); const { data: listData, isLoading: listLoading } = useQuestionList(reqParams); const { data: followResp } = useFollow(tagFollow); - const { data: synonymsRes } = useQuerySynonymsTags(tagInfo?.tag_id); + const { data: synonymsRes } = useQuerySynonymsTags(tagInfo?.tag_id, ''); const toggleFollow = () => { if (!guard.tryNormalLogged(true)) { return; diff --git a/ui/src/pages/Tags/Info/index.tsx b/ui/src/pages/Tags/Info/index.tsx index c56b29e1..13eca786 100644 --- a/ui/src/pages/Tags/Info/index.tsx +++ b/ui/src/pages/Tags/Info/index.tsx @@ -1,5 +1,5 @@ import { useState, useEffect } from 'react'; -import { Row, Col, Button, Card } from 'react-bootstrap'; +import { Alert, Row, Col, Button, Card } from 'react-bootstrap'; import { useParams, useNavigate, Link, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; @@ -13,6 +13,7 @@ import { saveSynonymsTags, deleteTag, editCheck, + unDeleteTag, } from '@/services'; import { pathFactory } from '@/router/pathFactory'; import { loggedUserInfoStore, toastStore } from '@/stores'; @@ -23,10 +24,15 @@ const TagIntroduction = () => { const isLogged = Boolean(userInfo?.access_token); const [isEdit, setEditState] = useState(false); const { tagName } = useParams(); - const { data: tagInfo } = useTagInfo({ name: tagName }); + const { data: tagInfo, mutate: refreshTagInfo } = useTagInfo({ + name: tagName, + }); const { t } = useTranslation('translation', { keyPrefix: 'tag_info' }); const navigate = useNavigate(); - const { data: synonymsData, mutate } = useQuerySynonymsTags(tagInfo?.tag_id); + const { data: synonymsData, mutate } = useQuerySynonymsTags( + tagInfo?.tag_id, + tagInfo?.status, + ); let pageTitle = ''; if (tagInfo) { pageTitle = `'${tagInfo.display_name}' ${t('tag_wiki', { @@ -118,7 +124,7 @@ const TagIntroduction = () => { confirmBtnVariant: 'danger', onConfirm: () => { deleteTag(tagInfo.tag_id).then(() => { - navigate('/tags', { replace: true }); + // navigate('/tags', { replace: true }); }); }, }); @@ -126,14 +132,35 @@ const TagIntroduction = () => { const onAction = (params) => { if (params.action === 'edit') { handleEditTag(); - } else if (params.action === 'delete') { + } + if (params.action === 'delete') { handleDeleteTag(); } + if (params.action === 'undelete') { + Modal.confirm({ + title: t('undelete_title', { keyPrefix: 'delete' }), + content: t('undelete_desc', { keyPrefix: 'delete' }), + cancelBtnVariant: 'link', + confirmBtnVariant: 'danger', + confirmText: t('undelete', { keyPrefix: 'btns' }), + onConfirm: () => { + unDeleteTag(tagInfo.tag_id).then(() => { + // undo + refreshTagInfo(); + }); + }, + }); + } }; return ( + {tagInfo?.status === 'deleted' && ( + + {t('post_deleted', { keyPrefix: 'messages' })} + + )}

{ + return request.post('/answer/api/v1/answer/recover', { + answer_id: id, + }); +}; + +export const unDeleteQuestion = (qid) => { + return request.post('/answer/api/v1/question/recover', { + question_id: qid, + }); +}; diff --git a/ui/src/services/client/tag.ts b/ui/src/services/client/tag.ts index 1296e888..8511e1bc 100644 --- a/ui/src/services/client/tag.ts +++ b/ui/src/services/client/tag.ts @@ -13,8 +13,13 @@ export const modifyTag = (params) => { return request.put('/answer/api/v1/tag', params); }; -export const useQuerySynonymsTags = (tagId) => { - const apiUrl = tagId ? `/answer/api/v1/tag/synonyms?tag_id=${tagId}` : ''; +export const useQuerySynonymsTags = (tagId, status) => { + const apiUrl = + status === 'deleted' + ? '' + : tagId + ? `/answer/api/v1/tag/synonyms?tag_id=${tagId}` + : ''; return useSWR<{ synonyms: Type.SynonymsTag[]; member_actions?: Type.MemberActionItem[]; @@ -47,10 +52,11 @@ export const useTagInfo = ({ id = '', name = '' }) => { name = encodeURIComponent(name); apiUrl = `/answer/api/v1/tag?name=${name}`; } - const { data, error } = useSWR(apiUrl, (url) => + const { data, error, mutate } = useSWR(apiUrl, (url) => request.get(url, { allow404: true }), ); return { + mutate, data, isLoading: !data && !error, error, @@ -70,3 +76,9 @@ export const createTag = (params: Type.TagBase) => { const apiUrl = '/answer/api/v1/tag'; return request.post(apiUrl, params); }; + +export const unDeleteTag = (id) => { + return request.post('/answer/api/v1/tag/recover', { + tag_id: id, + }); +};