index.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {useEffect, useState} from "react";
  2. import Fade from "@mui/material/Fade";
  3. import Modal from "@mui/material/Modal";
  4. import styles from "./styles.module.scss";
  5. import Grow from "@mui/material/Grow";
  6. import SmallLoading from "@components/SmallLoading";
  7. import {useDispatch, useSelector} from "react-redux";
  8. import {getProtectGetStatus, getProtectInfo} from "@features/protectSlice";
  9. import {formatCash, stringFormat} from "@utils/helpers";
  10. import {REQ_STATUS} from "@api/config";
  11. import {reqProtectCheck, reqProtectGet} from "@features/api";
  12. import {APP_WELFARE} from "@constants/app";
  13. import {useThemeProvider} from "@contexts/themeContext";
  14. import {getUserInfo} from "@features/mainSlice";
  15. const i18n=global.i18n;
  16. const WProtect = () => {
  17. const [isShow, setIsShow] = useState(false);
  18. const protectInfo=useSelector(getProtectInfo);
  19. const status=useSelector(getProtectGetStatus);
  20. const dispatch = useDispatch();
  21. const {setWelfTabState}=useThemeProvider();
  22. const user=useSelector(getUserInfo);
  23. const show=()=>{
  24. setIsShow(true);
  25. setWelfTabState(APP_WELFARE.PROTECT);
  26. dispatch(reqProtectCheck());
  27. }
  28. const hide=()=>{
  29. setIsShow(false);
  30. setWelfTabState(-1);
  31. }
  32. const callback=(payload)=>{
  33. if (payload&&payload.code===200){
  34. hide();
  35. }
  36. }
  37. const checkCanProtect=()=>{
  38. if (user!=null&&protectInfo!=null){
  39. let Score=Number(user.Score)*global.CASH_BASE;
  40. let ProtectLimit=protectInfo.ProtectLimit!=null?parseInt(protectInfo.ProtectLimit):0;
  41. let CurProtectTimes=protectInfo.CurProtectTimes!=null?parseInt(protectInfo.CurProtectTimes):0;
  42. let ProtectTimes=protectInfo.ProtectTimes!=null?parseInt(protectInfo.ProtectTimes):0;
  43. return (Score<ProtectLimit)&&(CurProtectTimes<ProtectTimes);
  44. }
  45. return false;
  46. }
  47. const onGetProtect=()=>{
  48. if (status===REQ_STATUS.LOADING)return;
  49. if (!checkCanProtect())return;
  50. let ProtectNum=protectInfo.ProtectNum!=null?parseInt(protectInfo.ProtectNum):0;
  51. dispatch(reqProtectGet({callback:callback,ProtectNum:ProtectNum}));
  52. }
  53. useEffect(()=>{
  54. const protectReward={
  55. show,
  56. hide
  57. };
  58. global.protectReward=protectReward;
  59. return ()=>{
  60. }
  61. },[])
  62. const renderInfo=()=>{
  63. let ProtectLimit=protectInfo.ProtectLimit?formatCash(Number(protectInfo.ProtectLimit),0):0;
  64. let ProtectNum=protectInfo.ProtectNum?formatCash(Number(protectInfo.ProtectNum),0):0;
  65. let ProtectTimes=protectInfo.ProtectTimes?parseInt(protectInfo.ProtectTimes):0;
  66. return (
  67. <div className={styles.p_info}>
  68. <span className={styles.title}>{i18n.t("protect.title")}</span>
  69. <div className={styles.des}>
  70. {i18n.t("protect.des")}
  71. <span className={styles.coin}>{global.DOLLAR + ProtectLimit}</span>
  72. </div>
  73. <div className={styles.rewad}>{global.DOLLAR + ProtectNum}</div>
  74. <span className={styles.tips}>{stringFormat(i18n.t("protect.tips"),[ProtectTimes])}</span>
  75. </div>
  76. )
  77. }
  78. const renderGetBtn=()=>{
  79. let isDisabled=!checkCanProtect();
  80. return (
  81. <button className={`${styles.p_get_btn} ${isDisabled?styles.p_get_btn_disabled:''}`} onClick={onGetProtect} disabled={isDisabled}>
  82. {
  83. status===REQ_STATUS.LOADING?(<SmallLoading fill={"#fff"}/>): (
  84. <span className={styles.text}>
  85. {i18n.t("protect.btn_get")}
  86. </span>
  87. )
  88. }
  89. </button>
  90. )
  91. }
  92. return (
  93. <Modal open={isShow} onClose={hide} closeAfterTransition>
  94. <Fade in={isShow} timeout={500}>
  95. <div className={styles.container}>
  96. <Grow in={isShow} timeout={500}>
  97. <div className={styles.p_con}>
  98. <button className={styles.p_close} onClick={hide}>
  99. <img src={require("@assets/protect/p_close.png")}/>
  100. </button>
  101. <div className={styles.p_content}>
  102. {renderInfo()}
  103. {renderGetBtn()}
  104. </div>
  105. </div>
  106. </Grow>
  107. </div>
  108. </Fade>
  109. </Modal>
  110. )
  111. }
  112. export default WProtect;