Commit c88e4b95 authored by Magnus Holm Brunbjerg's avatar Magnus Holm Brunbjerg
Browse files

Experiment creation and ending handling

parent c983cf9b
......@@ -8,9 +8,8 @@ const fetch = require('node-fetch')
const app = express()
const port = process.env.PORT
const apiIp = process.env.API_IP
const apiPort = process.env.API_PORT
const apiPath = process.env.API_PATH
const api = `http://${process.env.API_IP}:${process.env.API_PORT}/api`
const standardGet = { method: 'GET', mode: 'cors', headers: { 'Accept': 'application/json' } }
app.listen(port, (err) => {
if (err) return console.error(err)
......@@ -47,78 +46,124 @@ app.use((req, res, next) => {
*
*/
// Frontpage
app.get('/', (req, res, next) => {
fetch(`http://${apiIp}:${apiPort}${apiPath}/experiments/running`, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
}
}).then((response) => {
return response.json()
}).then((data) => {
if (data) {
res.render('index', {
meta: {
title: 'Forside'
},
running: data
app.route('/')
/**
* Frontpage
*/
.get((req, res, next) => {
// Check if an experiment is running
fetch(`${api}/experiments/running`, standardGet)
.then((response) => {
if (response.status === 200) { return response.json() }
})
.then((running) => {
// If experiment is running, load experiment info
if (running) {
running.start_time = new Date(running.start_time).toLocaleString('en-GB', { hour12: false })
res.render('index', {
meta: {
title: 'Forside'
},
running: running
})
} else {
// If experiment isn't running, load users list
fetch(`${api}/users`, standardGet)
.then((response) => {
if (response.status === 200) { return response.json() }
})
.then((users) => {
res.render('index', {
meta: {
title: 'Forside'
},
users: users
})
})
}
})
})
app.route('/experiments')
/**
* List all experiments
*/
.get((req, res, next) => {
// Load experiments list
fetch(`${api}/experiments`, standardGet)
.then((response) => {
if (response.status === 200) { return response.json() }
})
} else {
fetch(`http://${apiIp}:${apiPort}${apiPath}/users`, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
.then((experiments) => {
for (let i = 0; i < experiments.length; i++) {
experiments[i].start_time = new Date(experiments[i].start_time).toLocaleString('en-GB', { hour12: false })
experiments[i].end_time = new Date(experiments[i].end_time).toLocaleString('en-GB', { hour12: false })
}
}).then((response) => {
return response.json()
}).then((data) => {
res.render('index', {
res.render('experiments', {
meta: {
title: 'Forside'
title: 'Eksperimenter'
},
users: data
experiments: experiments
})
})
}
})
})
// Issues
app.get('/issues', (req, res, next) => {
res.render('issues', {
meta: {
title: 'Problemer'
/**
* Create or end an experiment the temp way
*/
.post((req, res, next) => {
const experimentContent = {
user_id: req.body.userId,
name: req.body.name,
start_time: new Date(),
end_time: new Date()
}
})
})
// List all experiments
app.get('/experiments', (req, res, next) => {
fetch(`http://${apiIp}:${apiPort}${apiPath}/experiments`, {
method: 'GET',
mode: 'cors',
headers: {
'Accept': 'application/json'
}
}).then((response) => {
return response.json()
}).then((data) => {
res.render('experiments', {
meta: {
title: 'Eksperimenter'
},
experiments: data
// Create new experiment
fetch(`${api}/experiments`, {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(experimentContent)
}).then((response) => {
if (response.status === 200) { return response.json() }
}).then((experiment) => {
if (experiment) {
fetch(`${api}/experiments/${experiment.id}`, {
method: 'POST',
mode: 'cors',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(experimentContent)
}).then(() => {
res.redirect('/')
})
} else {
res.redirect('/')
}
})
})
})
// Specific experiment
app.get('/experiments/:id', (req, res, next) => {
const id = req.params.id
})
app.route('/experiments/:id')
/**
* Show issues for a specific experiment
*/
.get((req, res, next) => {
// Get from db
fetch(`${api}/experiments/${req.params.id}/issues`, standardGet)
.then((response) => {
if (response.status === 200) { return response.json() }
})
.then((issues) => {
for (let i = 0; i < issues.length; i++) {
issues[i].start_time = new Date(issues[i].start_time).toLocaleString('en-GB', { hour12: false })
issues[i].end_time = new Date(issues[i].end_time).toLocaleString('en-GB', { hour12: false })
}
res.render('issues', {
meta: {
title: 'Problemer'
},
issues: issues
})
})
})
// 404 handling, keep at bottom
app.use((req, res, next) => {
......
<h1>Forside</h1>
{{#if running}}
<p>Navngiv og afslut det nuværende eksperiment:</p>
{{else}}
<p>Start et nyt eksperiment</p>
{{/if}}
<form method="POST" action="/">
<form method="POST" action="/experiments">
<div>
<label for="user">Lærer:</label>
<label for="userId">Lærer:</label>
{{#if running}}
<input type="text" name="user" id="user" disabled="true" value="{{running.user_name}}">
<input type="text" name="userId" id="userId" disabled="true" value="{{running.user_name}}">
{{else}}
<input type="text" name="user" id="user" list="users">
<datalist id="users">
{{#each users}}
<option value="{{this.user_name}}" id="{{this.id}}"></option>
{{/each}}
</datalist>
<input type="text" name="userId" id="userId" list="users" placeholder="Vælg en lærer fra listen">
<datalist id="users">
{{#each users}}
<option value="{{this.id}}">{{this.name}}</option>
{{/each}}
</datalist>
{{/if}}
</div>
{{#if running}}
<div>
<label for="name">Eksperiment navn:</label>
<input type="text" name="name" id="name" placeholder="{{running.name}}">
</div>
<div>
<label for="time">Start tidspunkt:</label>
<input type="text" name="time" id="time" disabled="true" value="{{running.start_time}}">
</div>
{{/if}}
<div>
<label for="name">Navn:</label>
<input type="text" name="name" id="name">
</div>
<div>
<button type="submit">Submit</button>
<button type="submit">{{#if running}}Afslut eksperiment{{else}}Start eksperiment{{/if}}</button>
</div>
</form>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment