Globally storing some data, other minor fixes

This commit is contained in:
mrfry 2021-01-10 11:15:54 +01:00
parent 820adec2d0
commit 0876761a0f
7 changed files with 111 additions and 61 deletions

View file

@ -17,8 +17,11 @@ const renderSnow = () => {
export default function Layout(props) {
let href = props.route
const [sidebarOpen, setSidebarOpen] = useState(true)
const [userId, setUserId] = useState(null)
const [windowSize, setWindowSize] = useState([100, 200])
const userId = props.globalData.userId
const userSpecificMotd = props.globalData.userSpecificMotd
console.log(userSpecificMotd)
if (href === '/' || href === '') {
href = 'index'
@ -32,20 +35,6 @@ export default function Layout(props) {
}
}
useEffect(() => {
fetch(`${constants.apiUrl}infos`, {
credentials: 'include',
Accept: 'application/json',
'Content-Type': 'application/json',
})
.then((resp) => {
return resp.json()
})
.then((data) => {
setUserId(data.uid)
})
}, [])
useEffect(() => {
closeSideBar()
setWindowSize([window.innerWidth, window.innerHeight])
@ -108,7 +97,23 @@ export default function Layout(props) {
</a>
</Link>
</div>
<div className="userInfo">User #{userId}</div>
<div className="userStatus">
{userSpecificMotd ? (
<div
onClick={() => {
// TODO: modal
alert(userSpecificMotd)
}}
style={{ cursor: 'pointer' }}
title={"You've got Mail!"}
>
📬
</div>
) : (
<div>📭</div>
)}
<div>User #{userId || '...'}</div>
</div>
</>
) : null}
</div>

View file

@ -55,11 +55,21 @@ a {
color: white;
}
.userInfo {
.userStatus {
display: flex;
align-items: center;
margin-bottom: 15px;
margin-top: auto;
}
.userStatus :first-child {
font-size: 25px;
}
.userStatus > div {
margin: 3px 5px;
}
.content {
margin-left: 200px;
padding: 1px 16px;

View file

@ -1,13 +1,42 @@
// import App from 'next/app'
import React from 'react'
import '../defaultStyles.css'
import React, { useState, useEffect } from 'react'
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 [userSpecificMotd, setUserSpecificMotd] = useState()
useEffect(() => {
fetch(`${constants.apiUrl}infos?motd=true`, {
credentials: 'include',
Accept: 'application/json',
'Content-Type': 'application/json',
})
.then((resp) => {
return resp.json()
})
.then((data) => {
setUserId(data.uid)
setMotd(data.motd)
setUserSpecificMotd(data.userSpecificMotd)
})
}, [])
const globalData = {
userId,
motd,
userSpecificMotd,
}
return (
<Layout route={router.route}>
<Component {...pageProps} router={router} />
<Layout route={router.route} globalData={globalData}>
<Component {...pageProps} router={router} globalData={globalData} />
</Layout>
)
}

View file

@ -22,7 +22,7 @@ function mergeData(data) {
...db.data.map((subj) => {
return {
...subj,
Name: `${subj.Name} (${db.dbName})`,
Name: `${subj.Name} [ DB: ${db.dbName} ]`,
}
}),
]

View file

@ -94,6 +94,13 @@ export default function contribute() {
)
})}
</ul>
<hr />
<div style={{ textAlign: 'center' }}>
<img
style={{ maxWidth: '100%', width: '400px' }}
src={`${constants.siteUrl}img/bug.png`}
/>
</div>
</div>
</div>
)

View file

@ -10,10 +10,10 @@ import styles from './index.module.css'
import links from '../data/links.json'
import constants from '../constants.json'
export default function Index() {
const [motd, setMotd] = useState('loading...')
const [userSpecificMotd, setUserSpecificMotd] = useState('loading...')
export default function Index(props) {
const [news, setNews] = useState(null)
const motd = props.globalData.motd
const userSpecificMotd = props.globalData.userSpecificMotd
useEffect(() => {
console.info('Fetching news.json')
@ -28,49 +28,33 @@ export default function Index() {
})
}, [])
useEffect(() => {
console.info('Fetching data')
fetch(`${constants.apiUrl}infos?motd=true`, {
credentials: 'include',
Accept: 'application/json',
'Content-Type': 'application/json',
})
.then((resp) => {
return resp.json()
})
.then((data) => {
setMotd(data.motd)
setUserSpecificMotd(data.userSpecificMotd)
})
}, [])
const renderQAItem = (n, key) => {
const renderQAItem = (newsItem, key) => {
return (
<div key={key} className={styles.itemContainer}>
<div className={styles.itemNumber}>{key} :</div>
<div
className={styles.question}
dangerouslySetInnerHTML={{ __html: n.q }}
dangerouslySetInnerHTML={{ __html: newsItem.q }}
/>
<div
className={styles.answer}
dangerouslySetInnerHTML={{ __html: n.a }}
dangerouslySetInnerHTML={{ __html: newsItem.a }}
/>
</div>
)
}
const renderNewsItem = (n, key) => {
const renderNewsItem = (newsItem, key) => {
return (
<div key={key} className={styles.itemContainer}>
<div className={styles.itemNumber}>{key} :</div>
<div
className={styles.newsTitle}
dangerouslySetInnerHTML={{ __html: n.title }}
dangerouslySetInnerHTML={{ __html: newsItem.title }}
/>
<div
className={styles.newsBody}
dangerouslySetInnerHTML={{ __html: n.body }}
dangerouslySetInnerHTML={{ __html: newsItem.body }}
/>
</div>
)
@ -79,19 +63,19 @@ export default function Index() {
const renderNews = () => {
if (news) {
let questions = Object.keys(news)
.map((key, i) => {
let n = news[key]
if (n.q) {
.map((key) => {
let newsItem = news[key]
if (newsItem.q) {
return (
<div key={key}>
{renderQAItem(n, key)}
{renderQAItem(newsItem, key)}
<hr />
</div>
)
} else {
return (
<div key={key}>
{renderNewsItem(n, key)}
{renderNewsItem(newsItem, key)}
<hr />
</div>
)
@ -118,10 +102,14 @@ export default function Index() {
<div className={styles.title}>MOTD</div>
<hr />
<hr />
{motd ? (
<div
className={styles.motd}
dangerouslySetInnerHTML={{ __html: motd }}
/>
) : (
<div>...</div>
)}
</div>
)
}
@ -129,13 +117,18 @@ export default function Index() {
const renderUserSpecificMotd = () => {
return (
<div>
<div className={styles.motdHeader}>
<hr />
<div className={styles.subtitle}>
Felhasználó MOTD (ezt csak te látod):
</div>
{userSpecificMotd ? (
<div
className={styles.motd}
dangerouslySetInnerHTML={{ __html: userSpecificMotd }}
/>
) : (
<div>...</div>
)}
</div>
)
}

View file

@ -49,6 +49,12 @@
text-align: center;
}
.subtitle {
color: #9999ff;
font-size: 20px;
text-align: center;
}
.newsTitle {
font-size: 28px;
color: var(--text-color);