Просмотр исходного кода

模式改变通知其他组件

visuddhinanda 2 лет назад
Родитель
Сommit
86b2384514
1 измененных файлов с 25 добавлено и 1 удалено
  1. 25 1
      dashboard/src/components/template/SentEdit/SentTabCopy.tsx

+ 25 - 1
dashboard/src/components/template/SentEdit/SentTabCopy.tsx

@@ -4,8 +4,12 @@ import {
   ShoppingCartOutlined,
   CheckOutlined,
 } from "@ant-design/icons";
-import { useState } from "react";
+import { useEffect, useState } from "react";
 import { IWbw } from "../Wbw/WbwWord";
+import store from "../../../store";
+import { modeChange } from "../../../reducers/cart-mode";
+import { useAppSelector } from "../../../hooks";
+import { mode as _mode } from "../../../reducers/cart-mode";
 
 export interface ISentCart {
   id: string;
@@ -18,6 +22,25 @@ interface IWidget {
 const SentTabCopyWidget = ({ text, wbwData }: IWidget) => {
   const [mode, setMode] = useState("copy");
   const [success, setSuccess] = useState(false);
+  const currMode = useAppSelector(_mode);
+
+  useEffect(() => {
+    const modeSetting = localStorage.getItem("cart/mode");
+    if (modeSetting === "cart") {
+      setMode("cart");
+    }
+  }, []);
+
+  useEffect(() => {
+    localStorage.setItem("cart/mode", mode);
+  }, [mode]);
+
+  useEffect(() => {
+    if (currMode) {
+      setMode(currMode);
+    }
+  }, [currMode]);
+
   const copy = (mode: string) => {
     if (text) {
       if (mode === "copy") {
@@ -61,6 +84,7 @@ const SentTabCopyWidget = ({ text, wbwData }: IWidget) => {
         ],
         onClick: (e) => {
           setMode(e.key);
+          store.dispatch(modeChange(e.key));
           copy(e.key);
         },
       }}