User specific motd changes (again)

This commit is contained in:
mrfry 2021-01-13 10:40:20 +01:00
parent 121e43c4e6
commit 23dc6197ad
4 changed files with 62 additions and 24 deletions

View file

@ -15,14 +15,19 @@ const renderSnow = () => {
return date.getMonth() === 11 && date.getDate() > 5 return date.getMonth() === 11 && date.getDate() > 5
} }
export default function Layout(props) { export default function Layout({
let href = props.route children,
route,
globalData,
refetchGlobalData,
}) {
let href = route
const [sidebarOpen, setSidebarOpen] = useState(true) const [sidebarOpen, setSidebarOpen] = useState(true)
const [windowSize, setWindowSize] = useState([100, 200]) const [windowSize, setWindowSize] = useState([100, 200])
const [showMotdModal, setShowMotdModal] = useState(false) const [showMotdModal, setShowMotdModal] = useState(false)
const userId = props.globalData.userId const userId = globalData.userId
const userSpecificMotd = props.globalData.userSpecificMotd const userSpecificMotd = globalData.userSpecificMotd
if (href === '/' || href === '') { if (href === '/' || href === '') {
href = 'index' href = 'index'
@ -99,19 +104,39 @@ export default function Layout(props) {
</Link> </Link>
</div> </div>
<div className="userStatus"> <div className="userStatus">
{userSpecificMotd ? ( <div
<div onClick={() => {
onClick={() => { setShowMotdModal(true)
setShowMotdModal(true) if (userSpecificMotd && userSpecificMotd.seen) {
}} return
style={{ cursor: 'pointer' }} }
title={"You've got Mail!"} fetch(constants.apiUrl + 'infos', {
> method: 'POST',
📬 credentials: 'include',
</div> headers: {
) : ( Accept: 'application/json',
<div>📭</div> 'Content-Type': 'application/json',
)} },
body: JSON.stringify({
userSpecificMotdSeen: true,
}),
})
.then((resp) => {
return resp.json()
})
.then(() => {
refetchGlobalData()
})
}}
style={{ cursor: 'pointer' }}
title={
userSpecificMotd && !userSpecificMotd.seen
? "You've got Mail!"
: ''
}
>
{userSpecificMotd && !userSpecificMotd.seen ? '📬' : '📭'}
</div>
<div>User ID: {userId || '...'}</div> <div>User ID: {userId || '...'}</div>
</div> </div>
{showMotdModal ? ( {showMotdModal ? (
@ -123,13 +148,13 @@ export default function Layout(props) {
<div style={{ textAlign: 'center' }}> <div style={{ textAlign: 'center' }}>
Üzenet az oldal készítőjétől: Üzenet az oldal készítőjétől:
</div> </div>
<div>{userSpecificMotd}</div> <div>{userSpecificMotd.msg}</div>
</Modal> </Modal>
) : null} ) : null}
</> </>
) : null} ) : null}
</div> </div>
<div className="content">{props.children}</div> <div className="content">{children}</div>
<BB /> <BB />
</div> </div>
) )

View file

@ -63,7 +63,7 @@ a {
} }
.userStatus :first-child { .userStatus :first-child {
font-size: 25px; font-size: 45px;
} }
.userStatus > div { .userStatus > div {

View file

@ -12,7 +12,7 @@ function MyApp({ Component, pageProps, router }) {
const [motd, setMotd] = useState() const [motd, setMotd] = useState()
const [userSpecificMotd, setUserSpecificMotd] = useState() const [userSpecificMotd, setUserSpecificMotd] = useState()
useEffect(() => { const getGlobalProps = () => {
fetch(`${constants.apiUrl}infos?motd=true`, { fetch(`${constants.apiUrl}infos?motd=true`, {
credentials: 'include', credentials: 'include',
Accept: 'application/json', Accept: 'application/json',
@ -26,6 +26,10 @@ function MyApp({ Component, pageProps, router }) {
setMotd(data.motd) setMotd(data.motd)
setUserSpecificMotd(data.userSpecificMotd) setUserSpecificMotd(data.userSpecificMotd)
}) })
}
useEffect(() => {
getGlobalProps()
}, []) }, [])
const globalData = { const globalData = {
@ -35,8 +39,17 @@ function MyApp({ Component, pageProps, router }) {
} }
return ( return (
<Layout route={router.route} globalData={globalData}> <Layout
<Component {...pageProps} router={router} globalData={globalData} /> route={router.route}
globalData={globalData}
refetchGlobalData={getGlobalProps}
>
<Component
{...pageProps}
router={router}
globalData={globalData}
refetchGlobalData={getGlobalProps}
/>
</Layout> </Layout>
) )
} }

View file

@ -140,7 +140,7 @@ export default function Index(props) {
{userSpecificMotd ? ( {userSpecificMotd ? (
<div <div
className={styles.motd} className={styles.motd}
dangerouslySetInnerHTML={{ __html: userSpecificMotd }} dangerouslySetInnerHTML={{ __html: userSpecificMotd.msg }}
/> />
) : ( ) : (
<div>...</div> <div>...</div>