v0.10.7 - Fixed washing_reservation creation on app, hopefully forever

This commit is contained in:
frederikpyt 2020-08-18 22:47:39 +02:00
parent 52b42124a1
commit abd9ebbe3e
3 changed files with 300 additions and 152 deletions

View File

@ -150,7 +150,7 @@ class WashingReservationController extends Controller
$machines = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->get();
if($request->machine_id == 0)
$request->machine_id = WashingMachine::query()->orderBy("name", "asc")->first()->id;
$request->machine_id = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->first()->id;
$reservations = WashingReservation::query()->where("machine_id", "=", $request->machine_id)->where("time", "LIKE", $datetext."%")->get();
@ -162,7 +162,41 @@ class WashingReservationController extends Controller
$locations = Location::query()->orderBy("name", "asc")->get();
$output = json_encode(['date' => $date, 'washingmachines' => $machines, 'unavailable_times' => $times, "locations" => $locations ]);
$output = json_encode(['date' => $date, 'washingmachines' => $machines, 'unavailable_times' => $times, "locations" => $locations, "machine_id" => $request->machine_id, "location_id" => $request->location_id ]);
return Response($output);
}
}
public function getMachines(Request $request){
if($request->ajax()){
if($request->location_id == 0)
$request->location_id = Location::all()->first()->id;
$machines = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->get();
$output = json_encode(["washingmachines" => $machines]);
return Response($output);
}
}
public function getTimes(Request $request){
if($request->ajax()){
if($request->location_id == 0)
$request->location_id = Location::all()->first()->id;
if($request->machine_id == 0)
$request->machine_id = WashingMachine::query()->where("location_id", "=", $request->location_id)->orderBy("name", "asc")->first()->id;
$reservations = WashingReservation::query()->where("machine_id", "=", $request->machine_id)->where("time", "LIKE", $request->datetext."%")->get();
$times = [];
foreach ($reservations as $reservation){
array_push($times, $reservation->time);
}
$output = json_encode(["unavailable_times" => $times]);
return Response($output);
}
}

View File

@ -45,6 +45,12 @@
<script src="{{ asset("/js/moment-with-locales.min.js") }}"></script>
<script src="{{ asset("/js/da.min.js") }}"></script>
<script>
var momentDate = null;
var dateText = null;
//Custom forEach
NodeList.prototype.forEach = Array.prototype.forEach;
function generateCalendar(weekOffset = 0) {
const week = (moment().week() + weekOffset);
@ -66,6 +72,7 @@
calendarRow.classList.add("calendar-table__row")
calendarRow.id = "calenderRow";
//Generate day buttons
weekDays.forEach(function (weekDay) {
let headerDay = document.createElement("div");
headerDay.classList.add("calendar-table__col");
@ -86,6 +93,7 @@
dayHolder.appendChild(day);
//When clicking on a date run "onDateSelect()"
dayHolder.onclick = function() {
onDateSelect(date, dayHolder, datetext);
}
@ -102,14 +110,11 @@
}
function onDateSelect(date, dayHolder, datetext) {
let events;
let locationz;
let machinez;
let buttonz;
//Set selected date and dateText
momentDate = date;
dateText = datetext;
let container = document.getElementById("event-container");
NodeList.prototype.forEach = Array.prototype.forEach;
//Add selected class on the selected date and remove it on all others
var children = document.getElementById("calenderRow").childNodes;
children.forEach(function(item){
@ -118,177 +123,284 @@
dayHolder.classList.add("selected");
/***************/
/*Generate form*/
/***************/
let machine_id;
let location_id;
if(document.getElementById('washing-machines'))
//If a washing-machine select exists, set machine_id to its currently selected options value
//Else set it to 0
if(document.getElementById('washing-machines')) {
machine_id = document.getElementById('washing-machines').value;
else
}
else {
machine_id = 0;
}
if(document.getElementById('locations'))
//If a location select exists, set location_id to its currently selected options value
//Else set it to 0
if(document.getElementById('locations')) {
location_id = document.getElementById('locations').value;
else
}
else {
location_id = 0;
}
axios({
method: 'get',
url: '{{ route("washing-reservations.api") }}',
params: { 'date': date, 'machine_id': machine_id, 'datetext': datetext, 'location_id': location_id }
}).then(function (response) {
})
.then(function (response) {
var data = response.data;
//Fill locations
fillLocations(data["locations"], location_id);
if(document.getElementById("locations") != undefined)
locationz = document.getElementById("locations");
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.lokation')}}";
//Fill washing-machines
fillMachines(data["washingmachines"], machine_id);
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "locations";
select.name = "location_id";
select.onchange = function() {
onDateSelect(date, dayHolder, datetext);
}
container.appendChild(span);
container.appendChild(select);
locationz = document.getElementById("locations");
}
if(document.getElementById("washing-machines") != undefined)
machinez = document.getElementById("washing-machines");
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.maskiner')}}";
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "washing-machines";
select.name = "machine_id";
select.onchange = function() {
onDateSelect(date, dayHolder, datetext);
}
container.appendChild(span);
container.appendChild(select);
machinez = document.getElementById("washing-machines");
}
if(document.getElementById("events") != undefined)
events = document.getElementById("events");
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.tilgængeligetider')}}";
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "events";
select.name = "time";
container.appendChild(span);
container.appendChild(select);
events = document.getElementById("events");
}
if(document.getElementById("create-reservation") != undefined)
buttonz = document.getElementById("create-reservation");
else {
let button = document.createElement("button");
button.id = "create-reservation";
button.innerText = "{{__('msg.reserver')}}";
button.classList.add("btn", "btn-sde-blue")
button.type = "submit";
container.appendChild(button);
buttonz = document.getElementById("events");
}
let locations = data["locations"];
locationz.innerHTML = "";
locationz.onchange = function () {
onDateSelect(date, dayHolder, datetext);
}
for (let i = 0; i < locations.length; i++) {
let option = document.createElement("option");
option.text = locations[i]["name"];
option.value = locations[i]["id"];
if(location_id == locations[i]["id"])
option.selected = "selected";
locationz.appendChild(option);
}
let machines = data["washingmachines"];
machinez.innerHTML = "";
machinez.onchange = function () {
onDateSelect(date, dayHolder, datetext);
}
for (let i = 0; i < machines.length; i++) {
let option = document.createElement("option");
option.text = machines[i]["name"];
option.value = machines[i]["id"];
if(machine_id == machines[i]["id"])
option.selected = "selected";
machinez.appendChild(option);
}
events.innerHTML = "";
for (let hour = 8; hour <= 20; hour++) {
let value = datetext + " " + prependZero(hour) + ":00:00";
let option = document.createElement("option");
option.text = prependZero(hour) + ":00";
option.value = value;
option.id = value;
events.appendChild(option);
}
let unavailable_times = data["unavailable_times"];
unavailable_times.forEach(function (item, index) {
document.getElementById(item).remove();
});
if(events.childElementCount == 0){
let option = document.createElement("option");
option.disabled = "disabled";
option.text = "Der er ingen tilgængelige tider";
option.selected = "selected";
events.appendChild(option);
document.getElementById("create-reservation").disabled = "disabled";
} else {
document.getElementById("create-reservation").disabled = "";
}
//Fill events (times) and remove unavailable_times
fillTimes(data["unavailable_times"], datetext);
});
}
//Fill locations
function fillLocations(locations, selected_location_id){
var element;
//If a location select exists, set element to the select element
//Else create it
if(document.getElementById("locations") !== null) {
element = document.getElementById("locations");
//Clear locations
element.innerHTML = "";
}
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.lokation')}}";
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "locations";
select.name = "location_id";
select.onchange = function() {
updateForm();
}
document.getElementById("event-container").appendChild(span);
document.getElementById("event-container").appendChild(select);
element = document.getElementById("locations");
}
element.onchange = function () {
updateForm();
}
//Add location options
for (let i = 0; i < locations.length; i++) {
let option = document.createElement("option");
option.text = locations[i]["name"];
option.value = locations[i]["id"];
//If current location is the location we have selected, mark it selected
if(selected_location_id == locations[i]["id"])
option.selected = "selected";
element.appendChild(option);
}
}
//Fill washing-machines
function fillMachines(machines, selected_machine_id) {
var element;
//If a washing-machine select exists, set element to the select element
//Else create it
if(document.getElementById("washing-machines") !== null) {
element = document.getElementById("washing-machines");
//Clear machines
element.innerHTML = "";
}
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.maskiner')}}";
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "washing-machines";
select.name = "machine_id";
select.onchange = function() {
updateForm();
}
document.getElementById("event-container").appendChild(span);
document.getElementById("event-container").appendChild(select);
element = document.getElementById("washing-machines");
}
element.onchange = function () {
updateForm();
}
//Add machine options
for (let i = 0; i < machines.length; i++) {
let option = document.createElement("option");
option.text = machines[i]["name"];
option.value = machines[i]["id"];
//If current machine is the machine we have selected, mark it selected
if(selected_machine_id == machines[i]["id"])
option.selected = "selected";
element.appendChild(option);
}
}
//Fill events (times) and remove unavailable_times
function fillTimes(unavailable_times, datetext) {
var element;
//If a events (times) select exists, set element to the select element
//Else create it
if(document.getElementById("events") !== null) {
element = document.getElementById("events");
//Clear times
element.innerHTML = "";
}
else {
let span = document.createElement("span");
span.classList.add("events__title");
span.innerText = "{{__('msg.tilgængeligetider')}}";
let select = document.createElement("select");
select.classList.add("events__title");
select.id = "events";
select.name = "time";
document.getElementById("event-container").appendChild(span);
document.getElementById("event-container").appendChild(select);
element = document.getElementById("events");
}
//Add times 8:00-20:00
for (let hour = 8; hour <= 20; hour++) {
let value = datetext + " " + prependZero(hour) + ":00:00";
let option = document.createElement("option");
option.text = prependZero(hour) + ":00";
option.value = value;
option.id = value;
element.appendChild(option);
}
//Remove unavailable_times
unavailable_times.forEach(function (item, index) {
document.getElementById(item).remove();
});
//If a submit button doesn't exist, create it
if(document.getElementById("create-reservation") === null) {
let button = document.createElement("button");
button.id = "create-reservation";
button.innerText = "{{__('msg.reserver')}}";
button.classList.add("btn", "btn-sde-blue")
button.type = "submit";
document.getElementById("event-container").appendChild(button);
}
//If there is no available times, disable the make reservation button and create new option
if(element.childElementCount === 0){
let option = document.createElement("option");
option.disabled = "disabled";
option.text = "Der er ingen tilgængelige tider";
option.selected = "selected";
element.appendChild(option);
document.getElementById("create-reservation").disabled = "disabled";
}
else {
document.getElementById("create-reservation").disabled = "";
}
}
//Update selects
function updateForm() {
console.log("Updating form...");
var location_id;
var machine_id;
//If a location select exists, set location_id to its currently selected options value
//Else set it to 0
if(document.getElementById('locations') !== null)
location_id = document.getElementById('locations').value;
else
location_id = 0;
console.log("location_id: " + location_id);
axios({
method: 'get',
url: '{{ route("washing-reservations.getMachines") }}',
params: { 'location_id': location_id }
})
.then(function (response) {
var data = response.data;
//Fill washing-machines
fillMachines(data["washingmachines"], machine_id);
//If a washing-machine select exists, set machine_id to its currently selected options value
//Else set it to 0
if(document.getElementById('washing-machines') !== null)
machine_id = document.getElementById('washing-machines').value;
else
machine_id = 0;
console.log("machine_id: " + machine_id);
axios({
method: 'get',
url: '{{ route("washing-reservations.getTimes") }}',
params: { 'machine_id': machine_id, 'datetext': dateText }
})
.then(function (response) {
var data = response.data;
//Fill events (times) and remove unavailable_times
fillTimes(data["unavailable_times"], dateText);
});
});
}
//Adds a "0" in front of values below 10
function prependZero(value) {
if(value < 10)
return "0" + value;
return value;
}
//The offset from the current week
let weekOffset = 0;
//Generate calender and set onclick function on the week changing arrows
if(document.getElementById("calendar")) {
generateCalendar();

View File

@ -30,6 +30,8 @@ Route::get("/events/signups", "EventController@signups")->name("events.signups")
Route::get("/events/accountsignups", "EventController@accountsignups")->name("events.accountsignups");
Route::get("phones", "PhoneController@index")->name("phones.index");
Route::get("/washing-reservationsapi", "WashingReservationController@api")->name("washing-reservations.api");
Route::get("/washing-reservationsgetMachines", "WashingReservationController@getMachines")->name("washing-reservations.getMachines");
Route::get("/washing-reservationsgetTimes", "WashingReservationController@getTimes")->name("washing-reservations.getTimes");
Route::get("/app/washing-reservations", "WashingReservationController@appindex")->name("washing-reservations.appindex");
Route::get("/settings", "SettingsController@index")->name("settings.index");
Route::post("/events/signup", "UserEventController@createajax")->name("userevents.createajax");