Hello All,
In my booking website, I am trying to create a booking form and trying to retrieve all booking slots available for a given day. I looked at the wix community example available at " https://www.wix.com/corvid/example/quick-book-and-pending-appointments "
The only difference is that in the above example, Coder creating a drop down with 7 days of date to pick the date, where I am using the calendar input to pick the date. Please have a look and help me, where I am missing:
// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixWindow from 'wix-window';
import wixBookings from 'wix-bookings';
import wixData from 'wix-data';
import wixWindow1 from 'wix-window';
const manandvan = '21d8b748-b342-43b4-b16e-63049bb8e0fb';
const twomenvan = '51d09f78-39f9-424c-8456-91450c43db78';
let slotMap = {};
let servicedata = wixWindow1.lightbox.getContext();
let date = 0;
// List of the days of the week.
const daysOfTheWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let mainservice = null;
let subservice = null;
$w.onReady(function () {
$w("#repeatersvc").value = servicedata.servicename;
$w('#Amount').value = "Delivery cost " + servicedata.serviceprice;
$w("#slotPicker").options = [];
// $w("#datePicker").onChange(() => selectDate());
// // $w("#dayPicker").options = getWeekDays(today).map(day => {
// // return {
// // // Set the label to be the name of the day of the week and its formatted date (e.g Tue 18/12/2018).
// // label: `${daysOfTheWeek[day.date.getDay()]} ${day.date.toLocaleDateString("en-GB")}`,
// // // Set the value to be the ISO string representation of the date (e.g. 2018-12-17T22:00:00.000Z).
// // value: day.date.toISOString()
// // }
// date = selectDate.selectedDate;
selectDate();
});
export function datePicker_change(event, $w) {
const selectedDate = $w("#datePicker").value.toISOString();
// const pickdate = $w("#datePicker").value.toISOString();
// const pickday = daysOfTheWeek[pickdate]
console.log("Selected Date = " + selectedDate);
findAvailableslots(selectedDate);
$w("#submitButton").onClick(() => validateandSubmitform());
}
export function submitButton_click(event) {
validateandSubmitform()
}
export function selectDate() {
const selectedDate = $w("#datePicker").value.toISOString()
findAvailableslots(selectedDate);
$w("#submitButton").onClick(() => validateandSubmitform());
return
}
async function findAvailableslots(date) {
console.log("Date in Find Available slot function = " + date);
slotMap = {};
const startdatetime = getmidnight(date);
const enddatetime = getnextmidnight(date);
const options = {
startdatetime,
enddatetime
};
console.log(options)
let servicename = ($w("#repeatersvc").value).split("/")
mainservice = servicename[0];
subservice = servicename[1];
console.log("mainservice = " + servicename[0])
console.log("subservice = " + servicename[1])
console.log(mainservice)
console.log(subservice)
if (mainservice === "Two Men and a Van Service") {
let serviceid = twomenvan;
} else if (mainservice === "One Man and a Van Service") {
let serviceid = manandvan;
}
const availableslots = await getNonPendingAvailableSlots(mainservice, options);
// console.log(availableslots1)
// const availableslots2 = await findavailableslots(twomenvan, options);
console.log("Service name to check available slot = " + $w("#repeatersvc").value);
// if ($w("#repeatersvc").value === "Two Men and a Van Service") {
// if (availableslots2.length === 0) {
// $w("#slotPicker").options = [];
// $w("#slotPicker").placeholder = "No slots available for " + date;
// $w("#slotPicker").selectedIndex = undefined;
// $w("#slotPicker").disable();
// } else {
// console.log("Slots available for Two Men Service");
// $w("#slotPicker").options = availableslots2.map(slot => {
// const uniqueId = new Date().getUTCMilliseconds().toString() // Generate a short unique ID to identify options in the slotPicker
// slotMap[uniqueId] = slot;
// let starttime = slot.startdatetime
// console.log("StartTime = " + slot.startdatetime)
// let timestring = getTimeOfDay(starttime);
// return {
// label: timestring,
// value: uniqueId
// }
// });
// $w("#slotPicker").placeholder = "Pick a time for " + date;
// $w("#slotPicker").selectedIndex = undefined;
// $w("#slotPicker").enable();
// }
// }
// if ($w("#repeatersvc").value === "One Man and a Van Service") {
if (availableslots.length === 0) {
$w("#slotPicker").options = [];
$w("#slotPicker").placeholder = "No slots available for " + date;
$w("#slotPicker").selectedIndex = undefined;
$w("#slotPicker").disable();
} else {
console.log("Slots available for Two Men Service");
$w("#slotPicker").options = availableslots.map(slot => {
const uniqueId = new Date().getUTCMilliseconds().toString() // Generate a short unique ID to identify options in the slotPicker
slotMap[uniqueId] = slot;
let starttime = slot.startdatetime
console.log("Start Time is = " + starttime)
let timestring = getTimeOfDay(starttime);
return {
label: timestring,
value: uniqueId
}
});
$w("#slotPicker").placeholder = "Pick a time for " + date;
$w("#slotPicker").selectedIndex = undefined;
$w("#slotPicker").enable();
}
}
// }
function getTimeOfDay(Date) {
console.log("we are in getTimeOfDay " + Date)
return Date.toLocaleTimeString("en-GB", { hour: "2-digit", minute: "2-digit" }).toLowerCase();
}
async function validateandSubmitform() {
$w("#errormsg").hide();
$w("#submitButton").disable();
if ($w("#name").valid && $w("#email").valid && $w("#phoneNumber").valid && $w("#datePicker").valid && $w("#slotPicker").valid && $w("#TnCcheck").valid) {
const slot = slotMap[$w("#slotPicker").value];
const newRequest = {
name: $w("#name").value,
email: $w("#email").value,
phoneNumber: $w("#phoneNumber").value,
requestedSlot: slot,
status: "PENDING",
mainService : mainservice,
subService : subservice
}
await wixData.insert("newAppoinments", newRequest)
$w("#bookinggroup").hide();
$w("#thankyouMsg").show();
} else {
$w("#errormsg").show;
$w("#submitButton").enable();
}
// const formField =[ "name","email","phoneNumber","datePicker","slotPicker","TnCcheck"];
// if (formField.every(input => $w('#${input}').valid))){
// }
}
function getmidnight(dateval) {
let midnight = new Date(dateval);
midnight.setHours(0, 0, 0, 0);
return midnight;
}
function getnextmidnight(dateval1) {
let midnight = new Date(dateval1);
midnight.setHours(24, 0, 0, 0);
return midnight;
}
async function getNonPendingAvailableSlots(serviceid, options = {}) {
console.log("we are in find available slots")
console.log(serviceid)
const pending = (await getPendingAppointments()).map(appointment => appointment.requestedSlot._id);
let availableSlots = (await wixBookings.getServiceAvailability(serviceid, options)).slots;
availableSlots = availableSlots.filter(slot => !pending.includes(slot._id))
console.log(availableSlots)
return availableSlots;
}
async function getPendingAppointments() {
console.log("we are in findpendingappointments")
return (await wixData.query("newAppointements").find()).items.filter(item => item.status === "PENDING");
}
Thanks in Advance.