WbwRelationAdd.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Button, Space } from "antd";
  2. import { useEffect, useState } from "react";
  3. import { useAppSelector } from "../../../hooks";
  4. import { add, relationAddParam } from "../../../reducers/relation-add";
  5. import store from "../../../store";
  6. import type { IWbw } from "./WbwWord"
  7. export const relationWordId = (word: IWbw) => {
  8. return `${word.book}-${word.para}-` + word.sn.join("-");
  9. };
  10. interface IWidget {
  11. data: IWbw;
  12. }
  13. const WbwRelationAddWidget = ({ data }: IWidget) => {
  14. const [show, setShow] = useState(false);
  15. const addParam = useAppSelector(relationAddParam);
  16. useEffect(() => {
  17. if (addParam?.command === "add") {
  18. setShow(true);
  19. } else {
  20. setShow(false);
  21. }
  22. }, [addParam?.command]);
  23. return (
  24. <div style={{ position: "absolute", marginTop: "-24px" }}>
  25. {show ? (
  26. <Space>
  27. <Button
  28. onClick={() => {
  29. if (typeof addParam === "undefined") {
  30. return;
  31. }
  32. store.dispatch(
  33. add({
  34. book: addParam.book,
  35. para: addParam.para,
  36. src_sn: addParam?.src_sn,
  37. target_id: relationWordId(data),
  38. target_spell: data.word.value,
  39. command: "apply",
  40. })
  41. );
  42. }}
  43. >
  44. add
  45. </Button>
  46. <Button
  47. onClick={() => {
  48. if (typeof addParam === "undefined") {
  49. return;
  50. }
  51. store.dispatch(
  52. add({
  53. book: addParam.book,
  54. para: addParam.para,
  55. src_sn: addParam.src_sn,
  56. command: "cancel",
  57. })
  58. );
  59. }}
  60. >
  61. cancel
  62. </Button>
  63. </Space>
  64. ) : undefined}
  65. </div>
  66. );
  67. };
  68. export default WbwRelationAddWidget;