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

89 lines
2.5 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';
import '../css/toastify.css';
import '../css/app.css';
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'));
},
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();