1. import { isEmpty } from "lodash";
  2. import React, { useContext, useEffect, useState } from "react";
  3. import { Link, useLocation } from "react-router-dom";
  4. import AuthProvider from "../context/AuthContext";
  5. import io from "socket.io-client";
  6. import { apiGet, apiPost } from "../Utils/apiFetch";
  7. import apiPath from "../Utils/apiPath";
  8. import obj from "../Utils/helpers";
  9. import MessageBox from "./MessageBox";
  10. import WalletWrap from "./WalletWrap";
  11. import BetSlipContext from "../context/BetSlipContext";
  12.  
  13. const Header = () => {
  14. let {
  15. user,
  16. user_coins,
  17. logoutUser,
  18. setOpenBetsToogle,
  19. setSettingToogle,
  20. setIsTv,
  21. setAnnouncmentToogle,
  22. isTv,
  23. setWalletOpen,
  24. walletOpen,
  25. } = useContext(AuthProvider);
  26.  
  27. const location = useLocation();
  28. let { betLoader, unMatchedBets, setRefreshCurrentBets, balanceRefresh, setBalanceRefresh } =
  29. useContext(BetSlipContext);
  30. const [refresh, setRefresh] = useState(false);
  31. const [loader, setLoader] = useState(false);
  32. const [loader1, setLoader1] = useState(false);
  33.  
  34. // Synchronize local balance state with user_coins from AuthContext
  35. const [balance, setBalance] = useState({
  36. totalCoins: user_coins?.totalCoins || 0,
  37. exposure: user_coins?.exposure || 0
  38. });
  39.  
  40. // Update local balance when user_coins changes
  41. // useEffect(() => {
  42. // setBalance({
  43. // totalCoins: user_coins?.totalCoins || 0,
  44. // exposure: user_coins?.exposure || 0
  45. // });
  46. // }, [user_coins]);
  47.  
  48. const refreshAmount = () => {
  49. setRefresh(true);
  50. const newSocket = io(
  51. `${import.meta.env.VITE_API_BASE_URL_OLD}?token=${localStorage.getItem(
  52. "token"
  53. )}&userType=front`,
  54. {
  55. transports: ["websocket"],
  56. }
  57. );
  58. const coinListener = (message) => {
  59. setBalance({
  60. totalCoins: message.results.totalCoins,
  61. exposure: message.results.exposure
  62. });
  63. setRefresh(false);
  64. };
  65. const forceLogout = (message) => {
  66. const uniqueId = localStorage.getItem("uniqueId");
  67. if (uniqueId !== message.results.uniqueId) {
  68. logoutUser();
  69. }
  70. };
  71. newSocket.emit("getCoins", { user_id: user ? user.user._id : "" });
  72. newSocket.on("listenGetCoin", coinListener);
  73. newSocket.on("listenForceLogout", forceLogout);
  74. return () => newSocket.close();
  75. };
  76.  
  77. const recallCasinoAmount = async (event) => {
  78. if (!isEmpty(user)) {
  79. setLoader(true);
  80. try {
  81. const { status, data: response_users } = await apiPost(
  82. apiPath.withdrawCasinoAmount,
  83. { amount: casionData ? casionData : user_coins?.casinoCoins }
  84. );
  85. if (status === 200) {
  86. if (response_users.status) {
  87. if (response_users.data.status === "0000") {
  88. setLoader(false);
  89. mainBalanceClick();
  90. } else {
  91. setLoader(false);
  92. }
  93. } else {
  94. setLoader(false);
  95. }
  96. }
  97. } catch (err) {
  98. setLoader(false);
  99. }
  100. }
  101. };
  102.  
  103. const recallEgtCasinoAmount = async (event) => {
  104. if (!isEmpty(user)) {
  105. setLoader1(true);
  106. try {
  107. const { status, data: response_users } = await apiPost(
  108. apiPath.withdrawEgtCasinoAmount,
  109. { amount: 0 }
  110. );
  111. if (status === 200) {
  112. if (response_users.status) {
  113. setLoader1(false);
  114. mainEgBalanceClick();
  115. getCoins();
  116. } else {
  117. setLoader1(false);
  118. }
  119. }
  120. } catch (err) {
  121. setLoader1(false);
  122. }
  123. }
  124. };
  125.  
  126. const [casionData, setCasinoData] = useState(0);
  127. const [egtCasionData, setEgtCasionData] = useState(0);
  128.  
  129. const mainBalanceClick = async () => {
  130. let { status, data: response_users } = await apiPost(apiPath.awcBalance);
  131. if (status === 200) {
  132. if (!isEmpty(response_users.data)) {
  133. setCasinoData(response_users.data.balance);
  134. }
  135. }
  136. };
  137.  
  138. const mainEgBalanceClick = async () => {
  139. let { status: status1, data: response_users1 } = await apiPost(
  140. apiPath.egtBalance
  141. );
  142. if (status1 === 200) {
  143. if (!isEmpty(response_users1.data)) {
  144. setEgtCasionData(response_users1.data.balance);
  145. }
  146. }
  147. };
  148.  
  149. // const getCoins = async () => {
  150. // if (!isEmpty(user)) {
  151. // const { status, data } = await apiGet(apiPath.refreshAmount);
  152. // if (status === 200) {
  153. // if (data.success) {
  154. // setBalance({
  155. // totalCoins: data?.results?.totalCoins,
  156. // exposure: data?.results?.exposure
  157. // });
  158. // }
  159. // }
  160. // }
  161. // };
  162.  
  163. const getCoins = async () => {
  164. if (!isEmpty(user)) {
  165. setRefresh(true);
  166. try {
  167. const { status, data } = await apiGet(apiPath.refreshAmount);
  168. if (status === 200 && data?.success) {
  169. setBalance({
  170. totalCoins: data?.results?.totalCoins,
  171. exposure: data?.results?.exposure,
  172. });
  173. }
  174. } finally {
  175. setRefresh(false);
  176. }
  177. }
  178. };
  179.  
  180. useEffect(() => {
  181. if (!isEmpty(user)) {
  182. getCoins();
  183. mainBalanceClick();
  184. mainEgBalanceClick();
  185. }
  186. }, [user]);
  187.  
  188. useEffect(() => {
  189. if (balanceRefresh) {
  190. getCoins();
  191. setBalanceRefresh(false);
  192. }
  193. }, [balanceRefresh]);
  194.  
  195. const [message, setMessage] = useState([]);
  196.  
  197. const messageList = async () => {
  198. let hostname = window.location.hostname;
  199. hostname = hostname.replace(/^www\./, "");
  200. hostname = hostname.replace(/^ag\./, "");
  201. hostname = hostname || "sabaexch.com";
  202.  
  203. const { status, data: response_users } = await apiGet(
  204. apiPath.messageList + "?domain=" + hostname
  205. );
  206. if (status === 200) {
  207. if (response_users.success) {
  208. setMessage(response_users.results);
  209. }
  210. }
  211. };
  212.  
  213. useEffect(() => {
  214. messageList();
  215. }, []);
  216.  
  217. return (
  218. <>
  219. {!isEmpty(user) && (
  220. <header id="headerMain1">
  221. <ul>
  222. <li className="li-tv_bet">
  223. {location?.pathname?.split("/")[1] == "match-details" &&
  224. isTv.id !== "" && (
  225. <a
  226. id="openTV"
  227. className="a-open_tv ui-link"
  228. href="#"
  229. onClick={() => setIsTv({ ...isTv, status: true })}
  230. style={{ display: isTv.id == "" ? "none" : "flex" }}
  231. >
  232. <img
  233. src={
  234. // import.meta.env.VITE_IMAGE_URL +
  235. "/assets/images/home/transparent.gif"
  236. }
  237. alt={""}
  238. />
  239. </a>
  240. )}
  241. <a
  242. id="openBetsBtn"
  243. onClick={() => {
  244. setOpenBetsToogle(true);
  245. setRefreshCurrentBets(true);
  246. }}
  247. className="a-open_bets ui-link"
  248. href="javascript:void(0)"
  249. >
  250. <img
  251. src={
  252. // import.meta.env.VITE_IMAGE_URL +
  253. "/assets/images/home/transparent.gif"
  254. }
  255. alt={""}
  256. />
  257. Bets
  258. {unMatchedBets?.length > 0 && (
  259. <div>
  260. <span class="slip-note"></span>
  261. <span class="slip-note"></span>
  262. <span class="slip-note"></span>
  263. </div>
  264. )}
  265. </a>
  266. </li>
  267.  
  268. <li className="main-wallet">
  269. <a
  270. id="multiWallet"
  271. href="javascript:void(0)"
  272. onClick={() => {
  273. setWalletOpen(true);
  274. mainBalanceClick();
  275. mainEgBalanceClick();
  276. }}
  277. className={
  278. walletOpen ? "a-wallet ui-link open" : "a-wallet ui-link"
  279. }
  280. >
  281. {refresh ? (
  282. <p className="loading-bar" id="menuRefreshLoading">
  283. <span></span>
  284. <span></span>
  285. <span></span>
  286. <span></span>
  287. <span></span>
  288. <span></span>
  289. <span></span>
  290. <span></span>
  291. </p>
  292. ) : (
  293. <ul id="accountCredit">
  294. <li>
  295. <span style={{ marginRight: "3px" }}>Main</span>
  296. <span id="betCredit">
  297. PBU{" "}
  298. {balance.totalCoins?.toFixed(2) || 0.0}
  299. </span>
  300. </li>
  301. <li>
  302. <span style={{ marginRight: "3px" }}>Exposure</span>
  303. <span
  304. id="totalExposure"
  305. style={{
  306. color: balance.exposure > 0 && "white",
  307. background: balance.exposure && "red",
  308. padding: "2px",
  309. }}
  310. >
  311.  
  312. {Number(balance.exposure)?.toFixed(2) }
  313. </span>
  314. </li>
  315. <li className="nums">
  316. +<span id="vendorQuantity">4</span>
  317. </li>
  318. </ul>
  319. )}
  320. </a>
  321. <a
  322. className="a-refresh ui-link"
  323. id="menuRefresh"
  324. href="javascript:void(0)"
  325. onClick={() => getCoins()}
  326. title="Refresh Main Wallet"
  327. >
  328. <img
  329. src={
  330. // import.meta.env.VITE_IMAGE_URL +
  331. "/assets/images/home/transparent.gif"
  332. }
  333. alt={""}
  334. />
  335. </a>
  336. </li>
  337.  
  338. <li>
  339. <a
  340. onClick={() => setSettingToogle(true)}
  341. className="a-setting ui-link"
  342. href="javascript:void(0)"
  343. title="Setting"
  344. >
  345. <img
  346. src={
  347. // import.meta.env.VITE_IMAGE_URL +
  348. "/assets/images/home/transparent.gif"
  349. }
  350. alt={""}
  351. />
  352. </a>
  353. </li>
  354. </ul>
  355. <MessageBox />
  356. <WalletWrap
  357. user_coins={user_coins}
  358. recallCasinoAmount={recallCasinoAmount}
  359. recallEgtCasinoAmount={recallEgtCasinoAmount}
  360. casionData={casionData}
  361. egtCasionData={egtCasionData}
  362. loader={loader}
  363. loader1={loader1}
  364. />
  365. </header>
  366. )}
  367. {isEmpty(user) && (
  368. <header style={{ display: "flex"}}>
  369. <h1 className="top-logo"></h1>
  370.  
  371. <Link to="/login" className="login-index ui-link">
  372. Login
  373. </Link>
  374.  
  375. <Link to="/register" className="btn-signup ui-link">
  376. Register
  377. </Link>
  378. <div
  379. id="msgBox"
  380. className="message-wrap success to-open_bets"
  381. style={{ display: "none" }}
  382. >
  383. <div className="message">
  384. <h4 id="header">Bet Matched</h4>
  385. <p id="info">
  386. <span id="sideType" className="back">
  387. Back
  388. </span>
  389. <strong id="selectionName">England</strong>{" "}
  390. <strong id="stake">$1,000</strong> at odds{" "}
  391. <strong id="odds">1.29</strong>
  392. </p>
  393. </div>
  394. <a id="close" className="close ui-link" href="#">
  395. Close
  396. </a>
  397. </div>
  398. <div id="multiWalletDiv" className="overlay" style={{ display: "none" }}>
  399. <div className="wallet-detail">
  400. <div className="wallet-detail-group">
  401. <dl className="wallet-detail-content">
  402. <dt>Main Balance</dt>
  403. <dd className="wallet-balance-num">
  404. <span className="badge-currency" id="currency">
  405. PBU
  406. </span>
  407. <span id="mainBalance">$ 0.00</span>
  408. </dd>
  409. <dd className="wallet-exposure">
  410. Exposure <span id="mainExposure">$ 0.00</span>
  411. </dd>
  412. </dl>
  413. </div>
  414. <div id="walletContent" className="wallet-detail-group"></div>
  415. <div
  416. id="walletTemp"
  417. className="wallet-detail-group"
  418. style={{ display: "none" }}
  419. >
  420. <dl id="tempDl" className="wallet-detail-content">
  421. <dt id="vendorTitle">Housie Balance</dt>
  422. <dd className="wallet-balance-num">
  423. <span className="badge-currency" id="vendorCurrency">
  424. PBU
  425. </span>
  426. <span id="vendorBalance">$ 0.00</span>
  427. </dd>
  428. <dd className="wallet-recall">
  429. <button
  430. className="btn-recall ui-btn ui-shadow ui-corner-all"
  431. id="recall"
  432. >
  433. Recall
  434. </button>
  435. </dd>
  436. </dl>
  437. <dl id="recallAllDl" className="wallet-detail-content">
  438. <dd class="align-R">
  439. <button
  440. className="btn-recall ui-btn ui-shadow ui-corner-all"
  441. id="recallAll"
  442. >
  443. Recall All
  444. </button>
  445. </dd>
  446. </dl>
  447. </div>
  448. <div class="btn-box">
  449. <button
  450. className="btn ui-btn ui-shadow ui-corner-all"
  451. id="balanceClose"
  452. >
  453. Close
  454. </button>
  455. </div>
  456. </div>
  457. </div>
  458. </header>
  459. )}
  460. {!isEmpty(user) && (
  461. <div
  462. id="headerMain2"
  463. className="marquee-box"
  464. style={{ display: "flex" }}
  465. >
  466. <h4>News</h4>
  467. <div
  468. class="marquee"
  469. onClick={() => {
  470. setAnnouncmentToogle(message.length > 0 ? true : false);
  471. }}
  472. >
  473. <marquee class="js-marquee-wrapper">
  474. <div class="js-marquee">
  475. {message?.length > 0 &&
  476. message?.map((item) => {
  477. return (
  478. <a>
  479. <span> {obj.msgDateFormat(item.msgDate)}</span>
  480. {item.message}
  481. </a>
  482. );
  483. })}
  484. </div>
  485. </marquee>
  486. </div>
  487. </div>
  488. )}
  489. </>
  490. );
  491. };
  492.  
  493. export default Header;