I recommend you split the functionality. You shouldn't try to handle multiplay states within the same method. Your project needs the following methods:
- Init(h) the function you call once when you startup the project
- CheckIn(input) the function that handles saving the checkIn time
- CheckOut(input) the function that handles saving the checkOut time
Remaining functionality like disabling the IN button when it's not been checked before 12 should be its own method and could be handled with an interval.
Eventually, you would get something like this:
const init = (h) => {
const btnGroup = document.querySelector('.btn-group');
const btnSucess = btnGroup.querySelector('.btn-success input');
const btnDanger = btnGroup.querySelector('.btn-danger input');
const isMorning = h > 6 && h < 12;
const isAfternoon = h > 12 && h < 20;
if(isMorning) {
btnSucess.disabled = false;
btnDanger.disabled = true;
} else if(isAfternoon) {
btnSucess.disabled = true;
btnDanger.disabled = false;
} else {
btnSucess.disabled = true;
btnDanger.disabled = true;
}
}
const checkIn = (input) => {
input.disabled = true;
const today = new Date();
console.log(today.toString());
}
const checkOut = (input) => {
input.disabled = true;
const today = new Date();
console.log(today.toString());
}
const setup = () => {
const hours = new Date().getHours();
init(hours);
}
// Load
window.addEventListener('load', setup);
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-success active">
<input type="radio" class="form-control input-lg" name="status" value="in" id="option1" autocomplete="off" onInput="checkIn(this)"> In
</label>
<label class="btn btn-danger">
<input type="radio" name="status" value="out" id="option2" autocomplete="off" onInput="checkOut(this)" disabled> Out
</label>
</div>
This is of course not perfect code. You should check within your checkOut()
function if there is a checkIn time saved. I recommend saving this time by using new Date().getTime()
for doing this.
This should be enough to give you a headstart. I want to point out that your system currently isn't that flexible. If for example there is something that has to be checked-IN after 12 then you might have a problem. You should work on your business case what should happen if a user doesn't follow the model traject you have build.
Good Luck