1
0
Fork 0
mirror of https://gitlab.com/MrFry/qmining-page synced 2025-04-01 20:23:44 +02:00

Caching fetched resources

This commit is contained in:
mrfry 2021-06-09 09:48:31 +02:00
parent 7e93e41edc
commit 3daeef184a
12 changed files with 213 additions and 85 deletions

View file

@ -66,7 +66,6 @@
flex-direction: column; flex-direction: column;
width: 150px; width: 150px;
background-color: #222426;
position: fixed; position: fixed;
height: 100%; height: 100%;
overflow: auto; overflow: auto;

View file

@ -13,7 +13,7 @@ const byVotes = (a, b) => {
return b.votes.length - a.votes.length return b.votes.length - a.votes.length
} }
export default function Todos() { export default function Todos({ globalState, setGlobalState }) {
const [loaded, setLoaded] = useState(false) const [loaded, setLoaded] = useState(false)
const [columns, setColumns] = useState(null) const [columns, setColumns] = useState(null)
const [cards, setCards] = useState(null) const [cards, setCards] = useState(null)
@ -26,15 +26,22 @@ export default function Todos() {
const [activeGroups, setActiveGroups] = useState(null) const [activeGroups, setActiveGroups] = useState(null)
useEffect(() => { useEffect(() => {
fetch(`${constants.apiUrl}todos`, { if (globalState.todos) {
credentials: 'include', setTodos(globalState.todos)
}) } else {
.then((resp) => { fetch(`${constants.apiUrl}todos`, {
return resp.json() credentials: 'include',
})
.then((data) => {
setTodos(data)
}) })
.then((resp) => {
return resp.json()
})
.then((data) => {
setTodos(data)
setGlobalState({
todos: data,
})
})
}
}, []) }, [])
const setTodos = (data) => { const setTodos = (data) => {
@ -188,6 +195,9 @@ export default function Todos() {
}) })
.then((data) => { .then((data) => {
setTodos(data) setTodos(data)
setGlobalState({
todos: data,
})
}) })
}} }}
/> />

View file

@ -300,7 +300,7 @@ select:hover {
.selectContainer > select, .selectContainer > select,
.selectContainer > input { .selectContainer > input {
width: 20%; width: 30%;
border-radius: 5px; border-radius: 5px;
border: 1.5px solid white; border: 1.5px solid white;
background-color: #9c9c98; background-color: #9c9c98;

View file

@ -10,6 +10,7 @@ function MyApp({ Component, pageProps, router }) {
const [userId, setUserId] = useState() const [userId, setUserId] = useState()
const [motd, setMotd] = useState() const [motd, setMotd] = useState()
const [unreads, setUnreads] = useState() const [unreads, setUnreads] = useState()
const [globalState, setGlobalState] = useState({})
const getGlobalProps = () => { const getGlobalProps = () => {
fetch(`${constants.apiUrl}infos?motd=true`, { fetch(`${constants.apiUrl}infos?motd=true`, {
@ -50,6 +51,13 @@ function MyApp({ Component, pageProps, router }) {
getGlobalProps() getGlobalProps()
}, []) }, [])
const updateGlobalState = (newState) => {
setGlobalState({
...globalState,
...newState,
})
}
const globalData = { const globalData = {
userId: userId, userId: userId,
motd: motd, motd: motd,
@ -65,12 +73,16 @@ function MyApp({ Component, pageProps, router }) {
router={router} router={router}
globalData={globalData} globalData={globalData}
refetchGlobalData={getGlobalProps} refetchGlobalData={getGlobalProps}
setGlobalState={updateGlobalState}
globalState={globalState}
> >
<Component <Component
{...pageProps} {...pageProps}
router={router} router={router}
globalData={globalData} globalData={globalData}
refetchGlobalData={getGlobalProps} refetchGlobalData={getGlobalProps}
setGlobalState={updateGlobalState}
globalState={globalState}
/> />
</Layout> </Layout>
</> </>

View file

@ -70,7 +70,7 @@ function fetchDbs() {
}) })
} }
export default function AllQuestions({ router }) { export default function AllQuestions({ router, globalState, setGlobalState }) {
const [subjectsShowing, setSubjectsShowing] = useState(false) const [subjectsShowing, setSubjectsShowing] = useState(false)
const [searchTerm, setSearchTerm] = useState('') const [searchTerm, setSearchTerm] = useState('')
const [activeSubjName, setActiveSubjName] = useState('') const [activeSubjName, setActiveSubjName] = useState('')
@ -112,15 +112,60 @@ export default function AllQuestions({ router }) {
useEffect(() => { useEffect(() => {
if (dbs && selectedDb && (selectedDb === 'all' || dbs[selectedDb])) { if (dbs && selectedDb && (selectedDb === 'all' || dbs[selectedDb])) {
if (selectedDb === 'all') { if (selectedDb === 'all') {
fetchAllData(dbs).then((res) => { const hasAll =
setData(mergeData(res)) globalState.dbs &&
dbs.every((db) => {
return (
Object.keys(globalState.dbs).findIndex((key) => {
return db.name === key
}) !== -1
)
})
if (hasAll) {
const asd = Object.keys(globalState.dbs).map((key) => {
return {
dbName: key,
data: globalState.dbs[key],
}
})
setData(mergeData(asd))
setFetchingData(false) setFetchingData(false)
}) } else {
fetchAllData(dbs).then((res) => {
setData(mergeData(res))
setFetchingData(false)
let cacheRes = {}
res.forEach((db) => {
cacheRes = {
...cacheRes,
[db.dbName]: db.data,
}
})
setGlobalState({
dbs: cacheRes,
})
})
}
} else { } else {
fetchData(dbs[selectedDb]).then((res) => { const selected = dbs[selectedDb]
setData(res.data) if (globalState.dbs && globalState.dbs[selected.name]) {
setData(globalState.dbs[selected.name])
setFetchingData(false) setFetchingData(false)
}) } else {
fetchData(dbs[selectedDb]).then((res) => {
setData(res.data)
setFetchingData(false)
setGlobalState({
dbs: {
...globalState.dbs,
[res.dbName]: res.data,
},
})
})
}
} }
} }
}, [selectedDb, dbs]) }, [selectedDb, dbs])

View file

@ -8,24 +8,31 @@ import LoadingIndicator from '../components/LoadingIndicator'
import styles from './contact.module.css' import styles from './contact.module.css'
export default function Contact() { export default function Contact({ globalState, setGlobalState }) {
const [contacts, setContacts] = useState() const [contacts, setContacts] = useState()
useEffect(() => { useEffect(() => {
fetch(constants.apiUrl + 'contacts.json', { if (globalState.contacts) {
method: 'GET', setContacts(globalState.contacts)
credentials: 'include', } else {
headers: { fetch(constants.apiUrl + 'contacts.json', {
Accept: 'application/json', method: 'GET',
'Content-Type': 'application/json', credentials: 'include',
}, headers: {
}) Accept: 'application/json',
.then((res) => { 'Content-Type': 'application/json',
return res.json() },
})
.then((res) => {
setContacts(res)
}) })
.then((res) => {
return res.json()
})
.then((res) => {
setContacts(res)
setGlobalState({
contacts: res,
})
})
}
}, []) }, [])
return ( return (

View file

@ -9,7 +9,7 @@ import constants from '../constants.json'
import styles from './contribute.module.css' import styles from './contribute.module.css'
import repos from '../data/repos.json' import repos from '../data/repos.json'
export default function contribute() { export default function Contribute({ globalState, setGlobalState }) {
const [showFeedback, setShowFeedback] = useState(false) const [showFeedback, setShowFeedback] = useState(false)
return ( return (
@ -52,7 +52,7 @@ export default function contribute() {
</div> </div>
<br /> <br />
<hr /> <hr />
<Todos /> <Todos globalState={globalState} setGlobalState={setGlobalState} />
<hr /> <hr />
<div id={'gitrepo'} className={styles.gitRepos}> <div id={'gitrepo'} className={styles.gitRepos}>
<div> <div>

View file

@ -76,7 +76,7 @@ function updateForumPost(forum, postKey, postData) {
}, {}) }, {})
} }
export default function Index({ globalData }) { export default function Index({ globalData, globalState, setGlobalState }) {
const userId = globalData.userId const userId = globalData.userId
const motd = globalData.motd const motd = globalData.motd
const [news, setNews] = useState(null) const [news, setNews] = useState(null)
@ -84,13 +84,20 @@ export default function Index({ globalData }) {
const [fetchingForum, setFetchingForum] = useState(false) const [fetchingForum, setFetchingForum] = useState(false)
useEffect(() => { useEffect(() => {
setFetchingForum(true) if (globalState.news) {
fetchForum().then((res) => { const { entries, nextKey } = globalState.news
setFetchingForum(false)
const { entries, nextKey } = res
setNextEntryKey(nextKey) setNextEntryKey(nextKey)
setNews(entries) setNews(entries)
}) } else {
setFetchingForum(true)
fetchForum().then((res) => {
setFetchingForum(false)
const { entries, nextKey } = res
setNextEntryKey(nextKey)
setNews(entries)
setGlobalState({ news: res })
})
}
}, []) }, [])
const renderNews = () => { const renderNews = () => {
@ -281,6 +288,12 @@ export default function Index({ globalData }) {
const { entries, nextKey } = res const { entries, nextKey } = res
setNextEntryKey(nextKey) setNextEntryKey(nextKey)
setNews({ ...news, ...entries }) setNews({ ...news, ...entries })
setGlobalState({
news: {
entries: { ...news, ...entries },
nextKey: nextKey,
},
})
}) })
}} }}
> >

View file

@ -40,7 +40,7 @@ function requestPw() {
}) })
} }
export default function PwRequest({ globalData }) { export default function PwRequest({ globalData, globalState, setGlobalState }) {
const userId = globalData.userId || '...' const userId = globalData.userId || '...'
const [result, setResult] = useState([]) const [result, setResult] = useState([])
const [daysTillNext, setDaysTillNext] = useState('...') const [daysTillNext, setDaysTillNext] = useState('...')
@ -65,9 +65,16 @@ export default function PwRequest({ globalData }) {
} = data } = data
useEffect(() => { useEffect(() => {
fetchAvailablePWS().then((data) => { if (globalState.availablePWS) {
setData(data) setData(globalState.availablePWS)
}) } else {
fetchAvailablePWS().then((data) => {
setData(data)
setGlobalState({
availablePWS: data,
})
})
}
const nextDate = 1 const nextDate = 1
const now = new Date() const now = new Date()
if (now.getDate() === nextDate) { if (now.getDate() === nextDate) {
@ -154,6 +161,9 @@ export default function PwRequest({ globalData }) {
onClick={() => { onClick={() => {
requestPw().then((res) => { requestPw().then((res) => {
setData(res) setData(res)
setGlobalState({
availablePWS: res,
})
if (res.success) { if (res.success) {
setResult([...result, res.pw]) setResult([...result, res.pw])
} else { } else {

View file

@ -84,7 +84,7 @@ function sortDataBy(data, key) {
}) })
} }
export default function RankList({ globalData }) { export default function RankList({ globalData, globalState, setGlobalState }) {
const userId = globalData.userId const userId = globalData.userId
const [ranklist, setRanklist] = useState(null) const [ranklist, setRanklist] = useState(null)
const [selfUserId, setSelfUserId] = useState('...') const [selfUserId, setSelfUserId] = useState('...')
@ -96,17 +96,30 @@ export default function RankList({ globalData }) {
const getList = () => { const getList = () => {
setSum() setSum()
setRanklist(null) setRanklist(null)
getListFromServer(since) if (globalState.ranklist) {
.then((data) => { const { list, sum, selfuserId } = globalState.ranklist
setRanklist(data.list || []) setRanklist(list || [])
setSum(data.sum) setSum(sum)
if (data.selfuserId) { if (selfuserId) {
setSelfUserId(data.selfuserId) setSelfUserId(selfuserId)
} }
}) } else {
.catch(() => { getListFromServer(since)
setRanklist(null) .then((data) => {
}) const { list, sum, selfuserId } = data
setRanklist(list || [])
setSum(sum)
if (selfuserId) {
setSelfUserId(selfuserId)
}
setGlobalState({
ranklist: data,
})
})
.catch(() => {
setRanklist(null)
})
}
} }
useEffect(() => { useEffect(() => {

View file

@ -22,13 +22,20 @@ function fetchSupportedSites() {
}) })
} }
export default function Script() { export default function Script({ globalState, setGlobalState }) {
const [supportedSites, setSupportedSites] = useState() const [supportedSites, setSupportedSites] = useState()
useEffect(() => { useEffect(() => {
fetchSupportedSites().then((res) => { if (globalState.supportedSites) {
setSupportedSites(res) setSupportedSites(globalState.supportedSites)
}) } else {
fetchSupportedSites().then((res) => {
setSupportedSites(res)
setGlobalState({
supportedSites: res,
})
})
}
}, []) }, [])
return ( return (

View file

@ -184,7 +184,12 @@ function uploadFile(dir, file) {
}) })
} }
export default function UserFiles({ router, globalData }) { export default function UserFiles({
router,
globalData,
globalState,
setGlobalState,
}) {
const userId = globalData.userId const userId = globalData.userId
const [dirs, setDirs] = useState() const [dirs, setDirs] = useState()
const [sortBy, setSortBy] = useState('name') const [sortBy, setSortBy] = useState('name')
@ -210,23 +215,48 @@ export default function UserFiles({ router, globalData }) {
setSearchTerm() setSearchTerm()
if (router.query.dir) { if (router.query.dir) {
getDir(dir)
} else {
getRootDir()
}
}, [router.query.dir])
const getDir = (dir, nocache) => {
if (!nocache && globalState.userFiles && globalState.userFiles[dir]) {
setDirs(globalState.userFiles[dir])
} else {
listUserDir(dir) listUserDir(dir)
.then((res) => { .then((res) => {
setDirs(res.files) setDirs(res.files)
setGlobalState({
userFiles: {
...globalState.userFiles,
[dir]: res.files,
},
})
}) })
.catch((res) => { .catch((res) => {
alert(res.msg) alert(res.msg)
}) })
}
}
const getRootDir = (nocache) => {
if (!nocache && globalState.userFilesRoot) {
setDirs(globalState.userFilesRoot)
} else { } else {
listUserDir() listUserDir()
.then((res) => { .then((res) => {
setGlobalState({
userFilesRoot: res.dirs,
})
setDirs(res.dirs) setDirs(res.dirs)
}) })
.catch((res) => { .catch((res) => {
alert(res.msg) alert(res.msg)
}) })
} }
}, [router.query.dir]) }
const dirSorter = (a, b) => { const dirSorter = (a, b) => {
if (a[sortBy] < b[sortBy]) { if (a[sortBy] < b[sortBy]) {
@ -391,13 +421,7 @@ export default function UserFiles({ router, globalData }) {
e.stopPropagation() e.stopPropagation()
if (confirm(`Biztos törlöd '${name}'-t ?`)) { if (confirm(`Biztos törlöd '${name}'-t ?`)) {
deleteFile(currDir, name).then(() => { deleteFile(currDir, name).then(() => {
listUserDir(currDir) getDir(currDir, true)
.then((res) => {
setDirs(res.files)
})
.catch((res) => {
alert(res.msg)
})
}) })
} }
}} }}
@ -479,14 +503,8 @@ export default function UserFiles({ router, globalData }) {
setUploading(true) setUploading(true)
uploadFile(currDir, file).then(() => { uploadFile(currDir, file).then(() => {
setUploading(false) setUploading(false)
listUserDir(currDir) setAddingNew(null)
.then((res) => { getDir(currDir, true)
setDirs(res.files)
setAddingNew(null)
})
.catch((res) => {
alert(res.msg)
})
}) })
}} }}
> >
@ -507,14 +525,8 @@ export default function UserFiles({ router, globalData }) {
setUploading(true) setUploading(true)
newSubj(newSubjName).then(() => { newSubj(newSubjName).then(() => {
setUploading(false) setUploading(false)
listUserDir() setAddingNew(null)
.then((res) => { getRootDir()
setDirs(res.dirs)
setAddingNew(null)
})
.catch((res) => {
alert(res.msg)
})
}) })
}} }}
> >