From a12a60b3b21319b9a9fb2cf7c1a002472604ebcb Mon Sep 17 00:00:00 2001 From: "haitao(lj)" Date: Thu, 1 Dec 2022 11:57:57 +0800 Subject: [PATCH] fix(review): add anwer edit summery and auto rollback review item --- ui/src/common/interface.ts | 1 + ui/src/pages/Questions/EditAnswer/index.tsx | 9 ++++ ui/src/pages/Review/index.tsx | 54 +++++++++++++-------- ui/src/services/client/revision.ts | 15 +----- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts index c080ce76..bdbff1b2 100644 --- a/ui/src/common/interface.ts +++ b/ui/src/common/interface.ts @@ -69,6 +69,7 @@ export interface AnswerParams { html: string; question_id: string; id: string; + edit_summary?: string; } export interface LoginReqParams { diff --git a/ui/src/pages/Questions/EditAnswer/index.tsx b/ui/src/pages/Questions/EditAnswer/index.tsx index 8ef5287b..7591c74d 100644 --- a/ui/src/pages/Questions/EditAnswer/index.tsx +++ b/ui/src/pages/Questions/EditAnswer/index.tsx @@ -62,6 +62,13 @@ const Ask = () => { ...formData, answer: { ...formData.answer, value }, }); + const handleSummaryChange = (evt) => { + const v = evt.currentTarget.value; + setFormData({ + ...formData, + description: { ...formData.description, value: v }, + }); + }; const checkValidated = (): boolean => { let bol = true; @@ -100,6 +107,7 @@ const Ask = () => { html: editorRef.current.getHtml(), question_id: qid, id: aid, + edit_summary: formData.description.value, }; modifyAnswer(params).then(() => { navigate(`/questions/${qid}/${aid}`); @@ -198,6 +206,7 @@ const Ask = () => { {t('form.fields.edit_summary.label')} { const [isLoading, setIsLoading] = useState(false); const [noTasks, setNoTasks] = useState(false); const [page, setPage] = useState(1); - const { data: reviewResp, mutate: mutateList } = useReviewList(page); + const [reviewResp, setReviewResp] = useState(); const ro = reviewResp?.list[0]; const { info, type, unreviewed_info } = ro || { info: null, type: '', unreviewed_info: null, }; - const reviewInfo = unreviewed_info?.content; - const mutateNextPage = () => { - const count = reviewResp?.count; - if (count && page < count) { - setPage(page + 1); - } else { + const resolveNextOne = (resp, pageNumber) => { + const { count, list = [] } = resp; + // auto rollback + if (!list.length && count && page !== 1) { + pageNumber = 1; + setPage(pageNumber); + // eslint-disable-next-line @typescript-eslint/no-use-before-define + queryNextOne(pageNumber); + return; + } + if (pageNumber !== page) { + setPage(pageNumber); + } + setReviewResp(resp); + if (!list.length) { setNoTasks(true); } + setTimeout(() => { + window.scrollTo({ top: 0 }); + }, 150); }; + const queryNextOne = (pageNumber) => { + getReviewList(pageNumber) + .then((resp) => { + resolveNextOne(resp, pageNumber); + }) + .catch((ex) => { + console.log('ex: ', ex); + }); + }; + const reviewInfo = unreviewed_info?.content; const handlingSkip = () => { - mutateNextPage(); + queryNextOne(page + 1); }; const handlingApprove = () => { if (!unreviewed_info) { @@ -39,7 +61,7 @@ const Index: FC = () => { setIsLoading(true); revisionAudit(unreviewed_info.id, 'approve') .then(() => { - mutateList(); + queryNextOne(page); }) .catch((ex) => { console.log('ex: ', ex); @@ -55,7 +77,7 @@ const Index: FC = () => { setIsLoading(true); revisionAudit(unreviewed_info.id, 'reject') .then(() => { - mutateList(); + queryNextOne(page); }) .catch((ex) => { console.log('ex: ', ex); @@ -93,14 +115,8 @@ const Index: FC = () => { editSummary ||= t('edit_tag'); } useEffect(() => { - if (!reviewResp) { - return; - } - window.scrollTo({ top: 0 }); - if (!reviewResp.list || !reviewResp.list.length) { - setNoTasks(true); - } - }, [reviewResp]); + queryNextOne(page); + }, []); return ( diff --git a/ui/src/services/client/revision.ts b/ui/src/services/client/revision.ts index 28110ae9..06b5ce49 100644 --- a/ui/src/services/client/revision.ts +++ b/ui/src/services/client/revision.ts @@ -1,5 +1,3 @@ -import useSWR from 'swr'; - import request from '@/utils/request'; import * as Type from '@/common/interface'; @@ -16,16 +14,7 @@ export const revisionAudit = (id: string, operation: 'approve' | 'reject') => { }); }; -export const useReviewList = (page: number) => { +export const getReviewList = (page: number) => { const apiUrl = `/answer/api/v1/revisions/unreviewed?page=${page}`; - const { data, error, mutate } = useSWR( - apiUrl, - request.instance.get, - ); - return { - data, - isLoading: !data && !error, - error, - mutate, - }; + return request.get(apiUrl); };