326 lines
5.4 KiB
JavaScript
326 lines
5.4 KiB
JavaScript
export default {
|
|
async toggleDay(day) {
|
|
const validDays = [
|
|
"monday",
|
|
"tuesday",
|
|
"wednesday",
|
|
"thursday",
|
|
"friday"
|
|
];
|
|
|
|
if (!validDays.includes(day)) {
|
|
showAlert(
|
|
"Ungültiger Wochentag.",
|
|
"error"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const currentDays =
|
|
appsmith.store.recurringDays || {};
|
|
|
|
const nextDays = {
|
|
monday: Boolean(
|
|
currentDays.monday
|
|
),
|
|
tuesday: Boolean(
|
|
currentDays.tuesday
|
|
),
|
|
wednesday: Boolean(
|
|
currentDays.wednesday
|
|
),
|
|
thursday: Boolean(
|
|
currentDays.thursday
|
|
),
|
|
friday: Boolean(
|
|
currentDays.friday
|
|
)
|
|
};
|
|
|
|
nextDays[day] =
|
|
!nextDays[day];
|
|
|
|
await storeValue(
|
|
"recurringDays",
|
|
nextDays,
|
|
false
|
|
);
|
|
|
|
return nextDays;
|
|
},
|
|
|
|
getSelectedWeekdays() {
|
|
const days =
|
|
appsmith.store.recurringDays || {};
|
|
|
|
const weekdayMap = {
|
|
monday: 1,
|
|
tuesday: 2,
|
|
wednesday: 3,
|
|
thursday: 4,
|
|
friday: 5
|
|
};
|
|
|
|
return Object.entries(
|
|
weekdayMap
|
|
)
|
|
.filter(
|
|
([day]) =>
|
|
days[day] === true
|
|
)
|
|
.map(
|
|
([, weekday]) =>
|
|
weekday
|
|
);
|
|
},
|
|
|
|
parseDate(value) {
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
|
|
const text =
|
|
String(value).slice(0, 10);
|
|
|
|
const parts =
|
|
text.split("-").map(Number);
|
|
|
|
if (parts.length !== 3) {
|
|
return null;
|
|
}
|
|
|
|
const date =
|
|
new Date(
|
|
parts[0],
|
|
parts[1] - 1,
|
|
parts[2]
|
|
);
|
|
|
|
return Number.isNaN(
|
|
date.getTime()
|
|
)
|
|
? null
|
|
: date;
|
|
},
|
|
|
|
formatDate(date) {
|
|
const pad =
|
|
value =>
|
|
String(value).padStart(
|
|
2,
|
|
"0"
|
|
);
|
|
|
|
return [
|
|
date.getFullYear(),
|
|
pad(date.getMonth() + 1),
|
|
pad(date.getDate())
|
|
].join("-");
|
|
},
|
|
|
|
formatDateTime(
|
|
date,
|
|
time,
|
|
allDay
|
|
) {
|
|
const dateText =
|
|
this.formatDate(date);
|
|
|
|
if (allDay) {
|
|
return `${dateText} 00:00:00`;
|
|
}
|
|
|
|
return `${dateText} ${time}:00`;
|
|
},
|
|
|
|
createRecurringDates(
|
|
startDate,
|
|
endDate,
|
|
selectedWeekdays
|
|
) {
|
|
const dates = [];
|
|
const cursor =
|
|
new Date(startDate);
|
|
|
|
while (
|
|
cursor <= endDate
|
|
) {
|
|
if (
|
|
selectedWeekdays.includes(
|
|
cursor.getDay()
|
|
)
|
|
) {
|
|
dates.push(
|
|
new Date(cursor)
|
|
);
|
|
}
|
|
|
|
cursor.setDate(
|
|
cursor.getDate() + 1
|
|
);
|
|
}
|
|
|
|
return dates;
|
|
},
|
|
|
|
async prepareRecurringBooking() {
|
|
const form =
|
|
appsmith.store.bookingFormData ||
|
|
{};
|
|
|
|
const recurring =
|
|
RecurringCheckbox.isChecked === true;
|
|
|
|
if (!recurring) {
|
|
return CreateBooking.run()
|
|
.then(() => {
|
|
return GetSeatBookings.run();
|
|
})
|
|
.then(() => {
|
|
return GetSeats.run();
|
|
})
|
|
.then(() => {
|
|
closeModal(
|
|
"Platzbuchung"
|
|
);
|
|
|
|
showAlert(
|
|
"Der Platz wurde erfolgreich gebucht.",
|
|
"success"
|
|
);
|
|
})
|
|
.catch((error) => {
|
|
console.error(
|
|
"Einzelbuchung fehlgeschlagen:",
|
|
error
|
|
);
|
|
|
|
showAlert(
|
|
"Die Buchung konnte nicht gespeichert werden.",
|
|
"error"
|
|
);
|
|
});
|
|
}
|
|
|
|
const startDate =
|
|
this.parseDate(
|
|
form.startDate
|
|
);
|
|
|
|
const endDate =
|
|
this.parseDate(
|
|
form.endDate
|
|
);
|
|
|
|
if (!startDate || !endDate) {
|
|
showAlert(
|
|
"Start- oder Enddatum ist ungültig.",
|
|
"error"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const selectedWeekdays =
|
|
this.getSelectedWeekdays();
|
|
|
|
if (
|
|
selectedWeekdays.length === 0
|
|
) {
|
|
showAlert(
|
|
"Bitte mindestens einen Wochentag auswählen.",
|
|
"warning"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const weeks =
|
|
Number(
|
|
RecurringWeeksSelect
|
|
.selectedOptionValue || 1
|
|
);
|
|
|
|
if (
|
|
!Number.isInteger(weeks) ||
|
|
weeks < 1 ||
|
|
weeks > 52
|
|
) {
|
|
showAlert(
|
|
"Bitte eine gültige Wochenanzahl auswählen.",
|
|
"warning"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const dates =
|
|
this.createRecurringDates(
|
|
startDate,
|
|
endDate,
|
|
selectedWeekdays
|
|
);
|
|
|
|
if (dates.length === 0) {
|
|
showAlert(
|
|
"Im gewählten Zeitraum wurden keine passenden Wochentage gefunden.",
|
|
"warning"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const startTime =
|
|
form.startTime || "00:00";
|
|
|
|
const endTime =
|
|
form.endTime || "23:59";
|
|
|
|
const allDay =
|
|
AllDayCheckbox.isChecked === true ||
|
|
form.allDay === true;
|
|
|
|
const candidates =
|
|
dates.map(
|
|
date => ({
|
|
date:
|
|
this.formatDate(date),
|
|
|
|
weekday:
|
|
date.toLocaleDateString(
|
|
"de-DE",
|
|
{
|
|
weekday: "long"
|
|
}
|
|
),
|
|
|
|
startDateTime:
|
|
this.formatDateTime(
|
|
date,
|
|
startTime,
|
|
allDay
|
|
),
|
|
|
|
endDateTime:
|
|
allDay
|
|
? `${this.formatDate(date)} 23:59:59`
|
|
: this.formatDateTime(
|
|
date,
|
|
endTime,
|
|
false
|
|
)
|
|
})
|
|
);
|
|
|
|
await storeValue(
|
|
"recurringCheck",
|
|
{
|
|
candidates,
|
|
freeBookings: [],
|
|
conflicts: [],
|
|
bookingFormData: form,
|
|
weeks,
|
|
selectedWeekdays
|
|
},
|
|
false
|
|
);
|
|
|
|
return candidates;
|
|
}
|
|
}; |