Added password authorization, minor other changes

This commit is contained in:
MrFry 2020-03-25 16:35:22 +01:00
parent e220da2525
commit a2c06b3343
5 changed files with 93 additions and 56 deletions

View file

@ -23,7 +23,6 @@ class Question extends PureComponent {
}) })
} }
// TODO
const qDataChange = (e) => { const qDataChange = (e) => {
try { try {
let newData = JSON.parse(e.target.value) let newData = JSON.parse(e.target.value)

View file

@ -1,4 +1,4 @@
{ {
"apiUrl": "localhost:8080", "apiUrl": "https://api.frylabs.net/",
"maxQuestionsToRender": 250 "maxQuestionsToRender": 250
} }

View file

@ -1,14 +0,0 @@
import Link from 'next/link'
export default function Layout (props) {
return (
<div>
<Link href='/questionView'>
<a>Question view</a>
</Link>
<Link href='/subjectVIew'>
<a>subjectView view</a>
</Link>
</div>
)
}

View file

@ -13,20 +13,29 @@ const views = {
question: 'QUESTION' question: 'QUESTION'
} }
// TODO: // TODO: Add question on subjects view
// Add question on subjects view // TODO: Add subject on subjects view
// question.data editor // TODO: question.data editor
// save data to server / load it from there // TODO: edit \n-s in questions / answers
// save: save question count and subj count
// save deleted/removed questions ?
// edit \n-s in questions / answers
// Load data: generate key / save keys as cookie and list?
// Upload data: save to new / save to same as loaded
export default function Index (props) { export default function Index (props) {
const [data, setData] = useState(null) const [data, setData] = useState(null)
const [view, setView] = useState(views.subject) const [view, setView] = useState(views.subject)
const [error, setError] = useState(null) const [error, setError] = useState(null)
const [password, setPassword] = useState('')
const [editedQuestions, setEditedQuestions] = useState({})
const [initialCount, setInitialCount] = useState({})
const getCount = (d) => {
return {
subjectCount: d.Subjects.length,
questionCount: d.Subjects.reduce((acc, subj) => {
acc += subj.Questions.length
return acc
}, 0)
}
}
const setIndexes = (d) => { const setIndexes = (d) => {
d.Subjects.forEach((subj, i) => { d.Subjects.forEach((subj, i) => {
@ -38,9 +47,11 @@ export default function Index (props) {
return d return d
} }
useEffect(() => { const LoadData = () => {
console.info('Fetching data') setData(null)
fetch(`${constants.apiUrl}data.json`) const toFetch = `${constants.apiUrl}data.json`
console.info('Fetching', toFetch)
fetch(toFetch)
.then((resp) => { .then((resp) => {
if (resp && resp.ok) { if (resp && resp.ok) {
return resp.json() return resp.json()
@ -50,17 +61,20 @@ export default function Index (props) {
} }
}) })
.then((resp) => { .then((resp) => {
if (data) { setData(setIndexes(resp))
console.warn('Tried to refetch data when it was still set!') const count = getCount(resp)
} else { console.info(`Data count`, count)
setData(setIndexes(resp)) setInitialCount(count)
}
}) })
.catch((e) => { .catch((e) => {
console.log(e) console.error(e)
console.error('Error while fetching data') console.error('Error while fetching data')
setError('Error while fetching data') setError('Error while fetching data')
}) })
}
useEffect(() => {
LoadData()
}, []) }, [])
if (error) { if (error) {
@ -80,12 +94,53 @@ export default function Index (props) {
} }
const onChange = (subjInd, questionInd, newVal) => { const onChange = (subjInd, questionInd, newVal) => {
const key = subjInd + '/' + questionInd
setEditedQuestions({
...editedQuestions,
[key]: editedQuestions[key] ? editedQuestions[key] + 1 : 1
})
data.Subjects[subjInd].Questions[questionInd] = newVal data.Subjects[subjInd].Questions[questionInd] = newVal
setData({ setData({
...data ...data
}) })
} }
const SendDataToServer = async () => {
const rawResponse = await fetch(constants.apiUrl + 'uploaddata', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
newData: data,
count: getCount(data),
initialCount: initialCount,
password: password,
editedQuestions: editedQuestions
})
})
rawResponse.json()
.then((resp) => {
if (resp.status === 'ok') {
alert(`Successfull upload! thanks ${resp.user}!`) // eslint-disable-line
console.log('OK')
} else if (resp.status === 'invalidPass') {
alert('Invalid password!') // eslint-disable-line
console.log('invalidPass')
} else {
alert('Error while uploading (server side)! More in console') // eslint-disable-line
console.error('RESPONSE', resp)
console.error(resp.message)
}
})
.catch((e) => {
alert('Error while uploading (client side)! More in console') // eslint-disable-line
console.error('Error posting data', e)
})
}
const renderView = () => { const renderView = () => {
if (view === views.subject) { if (view === views.subject) {
return ( return (
@ -112,18 +167,6 @@ export default function Index (props) {
} }
} }
const downloadFile = async (data) => {
const json = JSON.stringify(data)
const blob = new Blob([json], { type: 'application/json' }) // eslint-disable-line
const href = await URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = href
link.download = 'data.json'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
if (!data) { if (!data) {
return ( return (
<LoadingIndicator /> <LoadingIndicator />
@ -135,25 +178,26 @@ export default function Index (props) {
<div className={styles.optionsButtonContainer}> <div className={styles.optionsButtonContainer}>
<span <span
onClick={() => { onClick={() => {
LoadData()
}} }}
> >
Load data Reload data
</span>
<span>
<input
placeholder='Password for uploading'
type='text'
value={password}
onChange={(e) => { setPassword(e.target.value) }}
/>
</span> </span>
<span <span
onClick={() => { onClick={() => {
SendDataToServer()
}} }}
> >
Upload data Upload data
</span> </span>
<span
onClick={() => {
downloadFile(data)
}}
>
Download data
</span>
</div> </div>
<div className={styles.viewButtonContainer}> <div className={styles.viewButtonContainer}>
<span <span

View file

@ -23,6 +23,14 @@
word-wrap: none; word-wrap: none;
} }
.optionsButtonContainer span input {
width: 95%;
font-size: 16px;
background-color: var(--background-color);
color: white;
border: 0px solid;
}
.optionsButtonContainer span:hover { .optionsButtonContainer span:hover {
border-color: white; border-color: white;
} }