diff --git a/web/e.cash/components/navbar/getNavbarData.js b/web/e.cash/components/navbar/getNavbarData.js new file mode 100644 index 000000000..9b5bff651 --- /dev/null +++ b/web/e.cash/components/navbar/getNavbarData.js @@ -0,0 +1,33 @@ +// Copyright (c) 2024 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. +import React, { createContext, useState, useContext, useEffect } from 'react'; + +const ApiDataContext = createContext(); + +export const ApiDataProvider = ({ children }) => { + const [priceLinkText, setPriceLinkText] = useState('Buy XEC'); + + const getPrice = () => { + const api = + 'https://api.coingecko.com/api/v3/simple/price?ids=ecash&vs_currencies=usd'; + fetch(api) + .then(response => response.json()) + .then(data => + setPriceLinkText(`1 XEC = $${data.ecash.usd.toFixed(6)}`), + ) + .catch(err => console.log(err)); + }; + + useEffect(() => { + getPrice(); + }, []); + + return ( + + {children} + + ); +}; + +export const useApiData = () => useContext(ApiDataContext); diff --git a/web/e.cash/components/navbar/index.js b/web/e.cash/components/navbar/index.js index a2ed73b4d..3e7871d19 100644 --- a/web/e.cash/components/navbar/index.js +++ b/web/e.cash/components/navbar/index.js @@ -1,227 +1,215 @@ // Copyright (c) 2023 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. import Link from 'next/link'; import Image from 'next/image'; import { useEffect, useState } from 'react'; import { navitems } from '../../data/navitems'; import { socials } from '../../data/socials'; import { NavbarOuter, NavbarCtn, EnvVarMessage, SocialCtn } from './styles'; import AnnouncementBar from '/components/announcement-bar'; +import { useApiData } from './getNavbarData'; export default function Navbar({ announcementbar }) { - const [priceLinkText, setPriceLinkText] = useState('Buy XEC'); const [mobileMenu, setMobileMenu] = useState(false); const [selectedDropDownMenu, setSelectedDropDownMenu] = useState(-1); const [windowWidth, setWindowWidth] = useState(''); const [windowHeight, setWindowHeight] = useState(0); const [navBackground, setNavBackground] = useState(false); const mobileBreakPoint = 920; + const { priceLinkText } = useApiData(); const handleResize = () => { setWindowWidth(window.innerWidth); }; const handleScroll = () => { setWindowHeight(window.scrollY); }; - const getPrice = () => { - const api = - 'https://api.coingecko.com/api/v3/simple/price?ids=ecash&vs_currencies=usd'; - fetch(api) - .then(response => response.json()) - .then(data => - setPriceLinkText('1 XEC = $' + data.ecash.usd.toFixed(6)), - ) - .catch(err => console.log(err)); - }; - useEffect(() => { // set the window width so logic for mobile or desktop menus is applied correctly setWindowWidth(window.innerWidth); // add event listener for resize so we can update the screen width window.addEventListener('resize', handleResize); // add event listerner for scroll so we can change the nav background on scroll window.addEventListener('scroll', handleScroll); - // get XEC price - getPrice(); // remove the event listeners after mount to avoid memory leak return () => { window.removeEventListener('resize', handleResize); window.removeEventListener('scroll', handleScroll); }; }, []); useEffect(() => { // change the navBackground state based when windowHeight changes if (windowHeight >= 100) { setNavBackground(true); } else { setNavBackground(false); } }, [windowHeight]); return ( Prepare for the eCash network upgrade! Click here for more details {!process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID && ( Google Analytics is disabled, set the env NEXT_PUBLIC_GOOGLE_ANALYTICS_ID to fix )} {!process.env.NEXT_PUBLIC_WEGLOT_API_KEY && ( Translations are disabled, set the env NEXT_PUBLIC_WEGLOT_API_KEY to fix )}
ecash logo
{priceLinkText}
setMobileMenu(!mobileMenu)} />
); } diff --git a/web/e.cash/pages/_app.js b/web/e.cash/pages/_app.js index 8f8d0189d..a024781c4 100644 --- a/web/e.cash/pages/_app.js +++ b/web/e.cash/pages/_app.js @@ -1,41 +1,44 @@ // Copyright (c) 2023 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. import Script from 'next/script'; import { ThemeProvider } from 'styled-components'; import { ecash } from '/styles/theme'; import GlobalCSS from '/styles/global'; +import { ApiDataProvider } from '/components/navbar/getNavbarData'; export default function App({ Component, pageProps }) { return ( - - - - - + - + + + ); }