import React, { useState, useEffect } from 'react'
import { useQuery, QueryClientProvider, QueryClient } from 'react-query'
import Head from 'next/head'
import Layout from '../components/layout'
import '../defaultStyles.css'
import constants from '../constants.json'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
refetchOnWindowFocus: 'always',
},
},
})
const getGlobalProps = () => {
return new Promise((resolve) => {
fetch(`${constants.apiUrl}infos?motd=true`, {
credentials: 'include',
Accept: 'application/json',
'Content-Type': 'application/json',
})
.then((resp) => resp.json())
.then((data) => {
fetch(`${constants.apiUrl}hasNewMsg?userid=${data.uid}`, {
credentials: 'include',
Accept: 'application/json',
'Content-Type': 'application/json',
})
.then((resp) => resp.json())
.then((hasNewMsg) => {
resolve({ ...data, ...hasNewMsg })
})
.catch((err) => {
resolve({ ...data })
console.error('Error getting unreads')
console.error(err)
})
})
})
}
function App({ Component, pageProps, router }) {
const [userId, setUserId] = useState()
const [motd, setMotd] = useState()
const [unreads, setUnreads] = useState()
const [globalState, setGlobalState] = useState({})
const { data } = useQuery('hasNewMsgs', () => getGlobalProps(), {
refetchOnWindowFocus: true,
})
useEffect(() => {
if (!data) return
setUserId(data.uid)
setMotd(data.motd)
setUnreads(data.unreads)
}, [data])
useEffect(() => {
getGlobalProps()
}, [])
const updateGlobalState = (newState) => {
setGlobalState({
...globalState,
...newState,
})
}
const globalData = {
userId: userId,
motd: motd,
unreads: unreads,
}
return (
)
}
export default function Root(props) {
return (
)
}