Bläddra i källkod

Merge pull request #1477 from visuddhinanda/agile

#1398
visuddhinanda 2 år sedan
förälder
incheckning
d663f90a7e

+ 5 - 0
dashboard/src/components/term/TermEdit.tsx

@@ -32,6 +32,8 @@ import { get, post, put } from "../../request";
 import MDEditor from "@uiw/react-md-editor";
 import { useAppSelector } from "../../hooks";
 import { currentUser as _currentUser } from "../../reducers/current-user";
+import store from "../../store";
+import { push } from "../../reducers/term-vocabulary";
 
 interface ValueType {
   key?: string;
@@ -192,6 +194,9 @@ const TermEditWidget = ({
 
           if (res.ok) {
             message.success("提交成功");
+            store.dispatch(
+              push({ word: res.data.word, meaning: res.data.meaning })
+            );
             if (typeof onUpdate !== "undefined") {
               onUpdate(res.data);
             }

+ 3 - 3
dashboard/src/load.ts

@@ -10,7 +10,7 @@ import { get, IErrorResponse } from "./request";
 import { get as getLang } from "./locales";
 
 import store from "./store";
-import { ITerm, push } from "./reducers/term-vocabulary";
+import { ITerm, update } from "./reducers/term-vocabulary";
 import { push as nissayaEndingPush } from "./reducers/nissaya-ending-vocabulary";
 import { IRelation, IRelationListResponse } from "./pages/admin/relation/list";
 import { pushRelation } from "./reducers/relation";
@@ -99,7 +99,7 @@ const init = () => {
   get<ITermResponse>(`/v2/term-vocabulary?view=grammar&lang=` + getLang()).then(
     (json) => {
       if (json.ok) {
-        store.dispatch(push(json.data.rows));
+        store.dispatch(update(json.data.rows));
       }
     }
   );
@@ -108,7 +108,7 @@ const init = () => {
     `/v2/term-vocabulary?view=community&lang=` + getLang()
   ).then((json) => {
     if (json.ok) {
-      store.dispatch(push(json.data.rows));
+      store.dispatch(update(json.data.rows));
     }
   });
 

+ 14 - 2
dashboard/src/reducers/term-vocabulary.ts

@@ -20,13 +20,25 @@ export const slice = createSlice({
   name: "term-vocabulary",
   initialState,
   reducers: {
-    push: (state, action: PayloadAction<ITerm[]>) => {
+    update: (state, action: PayloadAction<ITerm[]>) => {
       state.term = action.payload;
     },
+    push: (state, action: PayloadAction<ITerm>) => {
+      if (state.term) {
+        if (
+          state.term.find((value) => value.word === action.payload.word) ===
+          undefined
+        ) {
+          state.term.push(action.payload);
+        }
+      } else {
+        state.term = [action.payload];
+      }
+    },
   },
 });
 
-export const { push } = slice.actions;
+export const { update, push } = slice.actions;
 
 export const getTerm = (state: RootState): ITerm[] | undefined =>
   state.termVocabulary.term;