Hi!
Quick question for the code experts out there…
I made a quick booking page, like this one here: https://www.wix.com/corvid/example/quick-book-and-pending-appointments
So I have a dropdown to display the days that are available and then another dropdown for the time slots available. Currently the dropdown displays the days for 1 week starting today (the day I am filling out the form. But I want the dropdown to show 1 week starting from a week from today. So if am filling the form and today is Oct 29th, it would show me the options: Nov 5th, Nov 6th, Nov 7th and so forth up to Nov 11th.
So what do I need to configure or change in the code to make that happen?
Here’s the code that sets the date:
function getMidnight (date) {
let midnight = new Date(date);
midnight.setHours(0, 0, 0, 0);
return midnight;
}
function getNextMidnight (date) {
let midnight = new Date(date);
midnight.setHours(24, 0, 0, 0);
return midnight;
}
function getWeekDays (startDate) {
let weekDays = [];
let current = getMidnight(startDate);
for (let i = 0; i < 7; i++) {
weekDays.push({
_id: "day" + i,
date: current
});
current = getNextMidnight(current)
}
return weekDays;
}
Here’s the code that show the days available:
const serviceId = '7ac4b7cc-a26a-4a83-b4cb-6710de094dbb';
let slotsMap = {};
$w.onReady(function () {
const today = new Date();
const daysOfTheWeek = ["Dom","Seg","Ter","Qua","Qui","Sex", "Sáb"]
$w('#dayPicker').options = getWeekDays(today).map(day => {
return{
label: `${daysOfTheWeek[day.date.getDay()]} ${day.date.toLocaleDateString("pt-BR")}`,
value: day.date.toISOString()
}
});
$w('#dayPicker').onChange((event, $w) => {
const selectedDate = new Date(event.target.value);
findAvailableSlots(selectedDate);
});
$w('#buttonSubmit').onClick(() => validadeAndSubmitForm());
});
Full booking code, if needed:
import wixBookings from 'wix-bookings';
import wixData from 'wix-data';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
const serviceId = '7ac4b7cc-a26a-4a83-b4cb-6710de094dbb';
let slotsMap = {};
$w.onReady(function () {
const today = new Date();
const daysOfTheWeek = ["Dom","Seg","Ter","Qua","Qui","Sex", "Sáb"]
$w('#dayPicker').options = getWeekDays(today).map(day => {
return{
label: `${daysOfTheWeek[day.date.getDay()]} ${day.date.toLocaleDateString("pt-BR")}`,
value: day.date.toISOString()
}
});
$w('#dayPicker').onChange((event, $w) => {
const selectedDate = new Date(event.target.value);
findAvailableSlots(selectedDate);
});
$w('#buttonSubmit').onClick(() => validadeAndSubmitForm());
});
async function findAvailableSlots(dateString) {
slotsMap = {};
const startDateTime = getMidnight(dateString);
const endDateTime = getNextMidnight (dateString);
const options = {
startDateTime,
endDateTime
};
const availableSlots = await getNonPendingAvailableSlots(serviceId, options);
if (availableSlots.length === 0) {
$w("#slotPicker").options = []
$w("#slotPicker").placeholder = "Indisponível"
$w("#slotPicker").selectedIndex = undefined;
$w("#slotPicker").disable();
}
else {
$w("#slotPicker").options = availableSlots.map(slot => {
slotsMap[slot._id] = slot;
let startTime = slot.startDateTime;
let timeString = getTimeOfDay(startTime);
return {
label: timeString,
value: slot._id
}
});
$w("#slotPicker").placeholder = "Horários disponíveis";
$w("#slotPicker").selectedIndex = undefined;
$w("#slotPicker").enable();
}
}
async function validadeAndSubmitForm() {
$w("#errorMessage").hide();
$w("#buttonSubmit").disable();
const formFields = ["inputNome", "inputSobrenome", "inputDN", "inputEmail", "inputWhatsApp", "dayPicker", "slotPicker"];
if (formFields.every(input => $w(`#${input}`).valid)) {
const slot = slotsMap[$w("#slotPicker").value];
const newRequest = {
nome: $w("#inputNome").value,
sobrenome: $w('#inputSobrenome').value,
dataNascimento: $w('#inputDN').value,
email: $w('#inputEmail').value,
whatsapp: $w('#inputWhatsApp').value,
requestedSlot: slot,
status: "PENDING"
};
await wixData.insert("pendingAppointments", newRequest);
$w('#AnchorTop').scrollTo();
showPagamento()
}
else {
$w('#errorMessage').show();
$w('#buttonSubmit').enable();
}
}
async function getNonPendingAvailableSlots (requestedServiceId, options = {}) {
const pending = (await getPendingAppointments()).map(appointment => appointment.requestedSlot._id);
let availableSlots = (await wixBookings.getServiceAvailability(requestedServiceId, options)).slots;
availableSlots = availableSlots.filter(slot => !pending.includes(slot._id));
return availableSlots;
}
async function getPendingAppointments (){
return (await wixData.query("pendingAppointments").find()).items.filter(item => item.status === "PENDING");
}
function getMidnight (date) {
let midnight = new Date(date);
midnight.setHours(0, 0, 0, 0);
return midnight;
}
function getNextMidnight (date) {
let midnight = new Date(date);
midnight.setHours(24, 0, 0, 0);
return midnight;
}
function getWeekDays (startDate) {
let weekDays = [];
let current = getMidnight(startDate);
for (let i = 0; i < 7; i++) {
weekDays.push({
_id: "day" + i,
date: current
});
current = getNextMidnight(current)
}
return weekDays;
}
function getTimeOfDay(date) {
return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit"}).toLowerCase();
}