Skip to content

Commit

Permalink
Created Calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
CatOrmerod committed Jun 4, 2021
1 parent 9fa0f59 commit 501729b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
19 changes: 19 additions & 0 deletions controllers/api/resultsRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const overtimeCalc = require('./utils/calc')

router.get('/results/:email', async (req, res) => {
try {
// Find the logged in user based on the session ID
const entryData = await Entry.findByPk(req.params.email, {
});

const entry = entryData.get({ plain: true });
const X, Y, Z
overtimeCalc(entry.start, entry.end, entry.lunch, entry.salary);
res.render('results', {
X, Y, Z,
});

} catch (err) {
res.status(500).json(err);
}
});
28 changes: 28 additions & 0 deletions public/js/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const entryFormHandler = async (event) => {
event.preventDefault();

const email = document.querySelector('#email-entry').value.trim();
const industry = document.querySelector('#industry-entry').value.trim();
const start = document.querySelector('#start-entry').value.trim();
const end = document.querySelector('#end-entry').value.trim();
const lunch = document.querySelector('#lunch-entry').value.trim();
const salary = document.querySelector('#salary-gross').value.trim();

if (email && industry && start && end && lunch && salary) {
const response = await fetch('/api/entry', {
method: 'POST',
body: JSON.stringify({ mail, industry, start, end, lunch, salary }),
headers: { 'Content-Type': 'application/json' },
});

if (response.ok) {
document.location.replace('/results');
} else {
alert(response.statusText);
}
}
};

document
.querySelector('.entry-form')
.addEventListener('submit', entryFormHandler);
27 changes: 27 additions & 0 deletions utils/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

const moment = require('moment');

module.exports = {
overtimeCalc: (start, end, lunch, salary) => {
// const start = moment(document.querySelector('#start-entry').value.format("HH:MM"));
// const end = moment(document.querySelector('#end-entry').value.format("HH:MM"));
// const lunch = parseFloat(document.querySelector('#lunch-entry').value.trim());
// const salary = parseFloat(document.querySelector('#salary-gross').value.trim());
const duration = moment.duration(moment(end).diff(start));

if (lunch == "nolunch") {
var newDuration = moment(duration).add(30, 'm').toDate();
}

const X = newDuration.asHours();
const Y = hoursWorkedDaily * 230
const hourlyRate = (salary/260)/7.5
const Z = hourlyRate * hoursWorkedAnnual

if (hoursWorkedDaily >= 7.5) {
return { X, Y, Z }
} else {
return
}
}
}
10 changes: 5 additions & 5 deletions views/homepage.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@

<h1 class="text-white mb-5">You have the right to switch off.</h1>

<form>
<form class= "entry-form" id="entry-form">
<div class="row">
<div class="col">
<label class="text-white mb-2">Email address <span>*</span>&nbsp;</label>
<input type="text" class="form-control mb-3" placeholder="[email protected]" name="email_address" id="email" required />
<input type="text" class="form-control mb-3 email-entry" placeholder="[email protected]" name="email_address" id="email-entry" required />
</div>
</div>

<div class="row mb-4">
<div class="row mb-4 industry-entry" id="industry-entry">
<div class="col">
<label class="text-white mb-2">Industry <span>*</span>&nbsp;</label>
<select class="form-select">
Expand Down Expand Up @@ -69,7 +69,7 @@
</div>
</div>

<div class="row mb-4">
<div class="row mb-4 start-entry" id="start-entry">
<div class="col">
<label class="text-white mb-2">Start time <span>*</span>&nbsp;</label>
<select class="form-select">
Expand Down Expand Up @@ -125,7 +125,7 @@
</select>
</div>

<div class="col">
<div class="col end-entry" id="end-entry">
<label class="text-white mb-2">Finish time <span>*</span>&nbsp;</label>
<select class="form-select">
<option selected>9:00am</option>
Expand Down

0 comments on commit 501729b

Please sign in to comment.