Skip to content

Commit

Permalink
fix: small refactoring for structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasdinh committed Oct 29, 2019
1 parent 9b5a6ad commit 4e49820
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ app.get('/get', (req, res) => {

app.post('/post', bodyParser.json(), (req, res) => {
const { temperature, date, userResponse } = req.body;
console.log('Data payload received:', req.body);

if (temperature && date && userResponse) {
projectData = {
Expand Down
33 changes: 20 additions & 13 deletions website/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,43 @@ const getWeather = async (zip, country) => {
}

// POST call to send user data from forms to own API server
const postUserData = async (temperature, date, userResponse) => {
const postUserData = async (localURL, port, postPayLoad) => {
fetch(localURL + ':' + port + '/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({temperature, date, userResponse})
body: JSON.stringify(postPayLoad)
})
}

// GET call to obtain current project data object
const getCurrentData = async () => {
return await fetch(localURL + ':' + port + '/get').then(response => response.json())
}


/* Execution */

// Adding click EventListener to "Generate" button in HTML

document.getElementById('generate').addEventListener('click', async () => {
async function clickUpdate () {
const country = document.getElementById('country').value;
const zip = document.getElementById('zip').value;

const userResponse = document.getElementById('feelings').value;
const temperature = await getWeather(zip, country);
await postUserData(temperature, newDate, userResponse);

const postPayLoad = {temperature, date: newDate, userResponse};
await postUserData(localURL, port, postPayLoad);

await getCurrentData()
.then((response) => {
document.getElementById('date').innerText = response.date,
document.getElementById('temp').innerText = response.temperature,
document.getElementById('content').innerText = response.userResponse
document.getElementById('date').innerHTML = response.date,
document.getElementById('temp').innerHTML = response.temperature,
document.getElementById('content').innerHTML = response.userResponse
});
})

}


/* Execution */

// Adding click EventListener to "Generate" button in HTML

document.getElementById('generate').addEventListener('click', clickUpdate)

0 comments on commit 4e49820

Please sign in to comment.