diff --git a/src/constants.json b/src/constants.json index 8650745..dec44ec 100644 --- a/src/constants.json +++ b/src/constants.json @@ -1,6 +1,6 @@ { "siteUrl": "https://qmining.frylabs.net/", - "apiUrl": "https://api.frylabs.net/", + "apiUrl": "http://localhost:8080/", "mobileWindowWidth": 700, "maxQuestionsToRender": 250 } diff --git a/src/pages/_app.js b/src/pages/_app.js index 76ddcde..07a80ab 100644 --- a/src/pages/_app.js +++ b/src/pages/_app.js @@ -2,11 +2,64 @@ import '../defaultStyles.css' import Layout from '../components/layout' +import constants from '../constants.json' + +var data = null + +function fetchData() { + return new Promise((resolve) => { + if (data) { + resolve(data) + return + } + + console.info('Fetching data') + fetch(`${constants.apiUrl}getDbs`, { + credentials: 'include', + }) + .then((resp) => { + return resp.json() + }) + .then((data) => { + const promises = data.map((db) => { + return fetch(`${constants.apiUrl}${db.path}`, { + credentials: 'include', + }).then((resp) => { + return new Promise((resolve) => { + resp.json().then((jsonRes) => { + resolve({ + dbName: db.name, + data: jsonRes, + }) + }) + }) + }) + }) + + Promise.all(promises).then((data) => { + const mergedData = data.reduce((acc, db) => { + return [ + ...acc, + ...db.data.map((subj) => { + return { + ...subj, + Name: `${subj.Name} (${db.dbName})`, + } + }), + ] + }, []) + + data = mergedData + resolve(mergedData) + }) + }) + }) +} function MyApp({ Component, pageProps, router }) { return ( - + ) } diff --git a/src/pages/allQuestions.js b/src/pages/allQuestions.js index 328d9fe..d82b30f 100644 --- a/src/pages/allQuestions.js +++ b/src/pages/allQuestions.js @@ -1,35 +1,26 @@ import React, { useState, useEffect } from 'react' -import Link from 'next/link' -import fetch from 'unfetch' import LoadingIndicator from '../components/LoadingIndicator.js' import QuestionSearchResult from '../components/QuestionSearchResult.js' import styles from './allQuestions.module.css' -import constants from '../constants.json' -export default function AllQuestions({ router }) { +export default function AllQuestions({ router, getData }) { const [data, setData] = useState(null) const [searchTerm, setSearchTerm] = useState('') useEffect(() => { - fetch(`${constants.apiUrl}data.json`, { - credentials: 'include', - }) - .then((resp) => { - return resp.json() - }) - .then((data) => { - setData(data) + getData().then((result) => { + setData(result) - router.replace(`${router.asPath.replace('.html', '')}`, undefined, { - shallow: true, - }) - const querySearch = router.query.question - ? decodeURIComponent(router.query.question) - : '' - setSearchTerm(querySearch) + router.replace(`${router.asPath.replace('.html', '')}`, undefined, { + shallow: true, }) + const querySearch = router.query.question + ? decodeURIComponent(router.query.question) + : '' + setSearchTerm(querySearch) + }) }, []) if (data) { diff --git a/src/pages/subjectBrowser.js b/src/pages/subjectBrowser.js index 0ed5428..48d76fb 100644 --- a/src/pages/subjectBrowser.js +++ b/src/pages/subjectBrowser.js @@ -7,9 +7,8 @@ import SubjectSelector from '../components/SubjectSelector.js' import Sleep from '../components/sleep' import styles from './subjectBrowser.module.css' -import constants from '../constants.json' -export default function SubjectBrowser (props) { +export default function SubjectBrowser({ getData }) { const [data, setData] = useState(null) const [activeSubjName, setActiveSubjName] = useState('') const [searchTerm, setSearchTerm] = useState('') @@ -18,20 +17,15 @@ export default function SubjectBrowser (props) { const [qCount, setQCount] = useState(0) useEffect(() => { - console.info('Fetching data') - fetch(`${constants.apiUrl}data.json`, { - credentials: 'include' - }) - .then((resp) => { - return resp.json() - }) - .then((data) => { - setData(data) - setSCount(data.length) - setQCount(data.reduce((acc, subj) => { + getData().then((result) => { + setData(result) + setSCount(result.length) + setQCount( + result.reduce((acc, subj) => { return acc + subj.Questions.length - }, 0)) - }) + }, 0) + ) + }) }, []) if (data) { @@ -43,11 +37,13 @@ export default function SubjectBrowser (props) {
{ setSearchTerm(e.target.value) }} + onChange={(e) => { + setSearchTerm(e.target.value) + }} />
{ setActiveSubjName(subjName) }} + onSubjSelect={(subjName) => { + setActiveSubjName(subjName) + }} />
@@ -70,15 +68,11 @@ export default function SubjectBrowser (props) {
- +
) } else { - return ( - - ) + return } }