import React, { useState, useEffect } from 'react' import Head from 'next/head' import Layout from '../components/layout' import '../defaultStyles.css' import constants from '../constants.json' function MyApp({ Component, pageProps, router }) { const [userId, setUserId] = useState() const [motd, setMotd] = useState() const [unreads, setUnreads] = useState() const [globalState, setGlobalState] = useState({}) const getGlobalProps = () => { fetch(`${constants.apiUrl}infos?motd=true`, { credentials: 'include', Accept: 'application/json', 'Content-Type': 'application/json', }) .then((resp) => { return resp.json() }) .then((data) => { fetch(`${constants.apiUrl}hasNewMsg?userid=${data.uid}`, { credentials: 'include', Accept: 'application/json', 'Content-Type': 'application/json', }) .then((resp) => { return resp.json() }) .then((hasNewMsg) => { const res = { ...data, ...hasNewMsg } setUserId(res.uid) setMotd(res.motd) setUnreads(res.unreads) }) .catch((err) => { const res = { ...data } setUserId(res.uid) setMotd(res.motd) console.error('Error getting unreads') console.error(err) }) }) } useEffect(() => { getGlobalProps() }, []) const updateGlobalState = (newState) => { setGlobalState({ ...globalState, ...newState, }) } const globalData = { userId: userId, motd: motd, unreads: unreads, } return ( <> ) } export default MyApp