Browse Source

add push to term-vocabulary

visuddhinanda 2 năm trước cách đây
mục cha
commit
acc8e89819
2 tập tin đã thay đổi với 17 bổ sung5 xóa
  1. 3 3
      dashboard/src/load.ts
  2. 14 2
      dashboard/src/reducers/term-vocabulary.ts

+ 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;