wn-discoteam-theme/resources/js/app.js

122 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-10-03 13:48:45 +02:00
import Alpine from 'alpinejs';
import Datepicker from 'flowbite-datepicker/Datepicker';
import de from '../../node_modules/flowbite-datepicker/js/i18n/locales/de.js';
2024-03-05 11:41:34 +01:00
Datepicker.locales.de = de.de;
2024-10-03 13:48:45 +02:00
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
2024-03-05 11:41:34 +01:00
dayjs.extend(customParseFormat);
2024-10-03 16:17:51 +02:00
import axios from 'axios';
import {error} from './toast.js';
2024-10-03 17:06:06 +02:00
import tippy from 'tippy.js';
2024-10-03 16:17:51 +02:00
import '../css/toastify.css';
import '../css/app.css';
2024-10-03 17:06:06 +02:00
import 'tippy.js/dist/tippy.css';
dayjs.extend(customParseFormat);
2024-02-12 21:44:41 +01:00
2024-03-05 11:41:34 +01:00
function initDatepicker(el) {
2024-10-03 13:48:45 +02:00
new Datepicker(el, {
autohide: true,
format: 'dd.mm.yyyy',
language: 'de',
update: true,
});
el.addEventListener('changeDate', function (event) {
event.target.dispatchEvent(new Event('input'));
});
2024-03-05 11:41:34 +01:00
}
2024-10-03 13:48:45 +02:00
document.querySelectorAll('[datepicker]').forEach((el) => initDatepicker(el));
2024-03-05 11:41:34 +01:00
window.Alpine = Alpine;
2024-10-03 13:48:45 +02:00
Alpine.data('form', () => ({
2024-10-03 16:17:51 +02:00
sent: false,
sending: false,
value: {
reason: '',
date: null,
guests: '',
birthday: null,
firstname: '',
lastname: '',
address: '',
zip: '',
location: '',
phone: '',
email: '',
misc: '',
parent_email: '',
parent_phone: '',
parent_name: '',
datenschutz: false,
},
2024-10-03 13:48:45 +02:00
isUnder18: function () {
2024-10-03 16:17:51 +02:00
if (!this.value.birthday || !this.value.date) {
2024-10-03 13:48:45 +02:00
return false;
}
2024-03-05 11:41:34 +01:00
2024-10-03 16:17:51 +02:00
return dayjs(this.value.birthday, 'DD.MM.YYYY').add(18, 'year').isAfter(dayjs(this.value.date, 'DD.MM.YYYY'));
},
2024-10-03 17:06:06 +02:00
onChangeDate: async function (n) {
const date = dayjs(this.value.date, 'DD.MM.YYYY');
if (!date.isValid()) {
return;
}
const response = await axios.post(
window.location.pathname,
{date: date.format('YYYY-MM-DD')},
{
headers: {
'X-Winter-Request-Handler': 'anfrage::onDateValidation',
'X-Requested-With': 'XMLHttpRequest',
},
}
);
tippy('[data-tippy-target]', {
2024-10-03 17:13:48 +02:00
content: response.data.result ? 'Dieser Termin ist voraussichtlich verfügbar' : 'Dieser Termin ist leider nicht buchbar',
2024-10-03 17:06:06 +02:00
showOnCreate: true,
trigger: 'manual',
theme: response.data.result ? 'success' : 'error',
placement: 'top',
onShown: function (t) {
window.setTimeout(() => t.hide(), 3000);
},
});
},
2024-10-03 16:17:51 +02:00
submit: async function () {
if (this.sending === true) {
return;
}
this.sending = true;
try {
await axios.post(window.location.pathname, this.value, {
headers: {
'X-Winter-Request-Handler': 'anfrage::onSubmit',
'X-Requested-With': 'XMLHttpRequest',
},
});
this.sent = true;
window.scrollTo({
top: this.$el.getBoundingClientRect().top + window.scrollY - 50,
behaviour: 'smooth',
});
this.sending = false;
} catch (e) {
this.sending = false;
if (!e instanceof axios.AxiosError || !e.response || !e.response.data || !e.response.data['X_WINTER_ERROR_FIELDS']) {
throw e;
}
const errors = e.response.data['X_WINTER_ERROR_FIELDS'];
Object.keys(errors).forEach((k) => {
errors[k].forEach((msg) => error(msg));
});
}
2024-10-03 13:48:45 +02:00
},
2024-03-05 11:41:34 +01:00
}));
Alpine.start();