Commit 3086d339 authored by droces's avatar droces

Lots of interaction improvements to Booking form

parent b7ae3bf9
......@@ -62,8 +62,10 @@ var calendar,
is_valid_promo = true,
max_guests = null;
// When calendar is clicked, first click (1) used for check in date,
// second click (2) used for check out date.
var date_arrive,
date_depart;
// When calendar is clicked, first click used for arrival, second for departure.
var last_calendar_click = null;
var bookable_field,
......@@ -478,7 +480,7 @@ function calculate_max_guests() {
function set_up_calendar() {
function set_up_calendar(context) {
// console.log('set_up_calendar()');
calendar.fullCalendar({
......@@ -491,66 +493,109 @@ function set_up_calendar() {
// Drupal.abookings.dayHover(event, jsEvent, view);
},
dayClick: function(date_clicked, jsEvent, view, resourceObj) {
var date_arrive,
date_depart;
// console.log('date_clicked: ', date_clicked);
// var date_arrive,
// date_depart;
// // If the calendar hasn't been clicked, or the last click was for check out
// // Use clicked date for arrival
// if (last_calendar_click !== 'arrive') {
// }
// // Use clicked day for departure
// else {
// date_depart = date_clicked;
// date_arrive = moment(arrival_field.val());
// num_nights = date_depart.diff(date_arrive, 'days');
// }
// date_arrive = date_clicked;
// var nights = parseInt(nights_field.val());
//
// date_depart = moment(date_arrive);
// date_depart.add(nights, 'day');
//
// dayClickHandler(date_arrive, date_depart);
}
});
// Check that there's bookable data
if (jQuery.isEmptyObject(Drupal.abookings.get_bookable_data())) {
alert('Please select a bookable unit');
return null;
}
if(! Drupal.abookings.dayHasPrice(date_clicked)) {
alert('Please select a date that has a price');
}
$(document, context).on('mousedown', '.fc-day, .fc-day-top', function(jsEvent) {
var date_clicked = moment($(this).attr('data-date'));
// console.log('date_clicked: ', date_clicked);
// If the calendar hasn't been clicked, or the last click was for check out
if (last_calendar_click !== 1) {
date_arrive = date_clicked;
var nights = parseInt(nights_field.val());
date_arrive = date_clicked;
var nights = parseInt(nights_field.val());
date_depart = moment(date_arrive);
date_depart.add(nights, 'day');
}
else {
date_depart = date_clicked;
date_arrive = moment(arrival_field.val());
num_nights = date_depart.diff(date_arrive, 'days');
}
arrival_field.val(date_arrive.format('Y-MM-DD'));
});
// If this new event overlaps with any others
var overlap = Drupal.abookings.check_event_overlap(date_arrive, date_depart);
if (overlap) {
return null;
}
// Set arrival and departure date fields
arrival_field.val(date_arrive.format('Y-MM-DD'));
departure_field.val(date_depart.format('Y-MM-DD'));
$(document, context).on('mouseup', '.fc-day, .fc-day-top', function(jsEvent) {
var date_clicked = moment($(this).attr('data-date'));
// console.log('date_clicked: ', date_clicked);
// Create or update the current booking
var booking_data = {
field_booking_status: 'provisional',
field_checkin_date: date_arrive.unix(),
field_checkout_date: date_depart.unix()
};
Drupal.abookings.add_booking('current', booking_data);
date_depart = date_clicked;
date_arrive = moment(arrival_field.val());
if (date_arrive.unix() == date_depart.unix()) {
date_depart.add(1, 'day');
}
num_nights = date_depart.diff(date_arrive, 'days');
num_nights_field.val(num_nights);
refresh_main_info();
departure_field.val(date_depart.format('Y-MM-DD'));
if (last_calendar_click == null) {
last_calendar_click = 1;
}
else if (last_calendar_click == 1) {
last_calendar_click = 2;
}
else {
last_calendar_click = 1;
}
}
dayClickHandler(date_arrive, date_depart);
});
};
/**
* @param {Object} date_arrive - Moment object
* @param {Object} date_depart - Moment object
*/
function dayClickHandler(date_arrive, date_depart) {
// console.log('dayClickHandler()');
// console.log('date_arrive: ', date_arrive);
// console.log('date_depart: ', date_depart);
// Check that there's bookable data
if (jQuery.isEmptyObject(Drupal.abookings.get_bookable_data())) {
alert('Please select a bookable unit');
return null;
}
if(! Drupal.abookings.dayHasPrice(date_arrive)) {
alert('Please select a date that has a price');
return null;
}
// If this new event overlaps with any others
var overlap = Drupal.abookings.check_event_overlap(date_arrive, date_depart);
if (overlap) {
return null;
}
// Create or update the current booking
var booking_data = {
field_booking_status: 'provisional',
field_checkin_date: date_arrive.unix(),
field_checkout_date: date_depart.unix()
};
Drupal.abookings.add_booking('current', booking_data);
refresh_main_info();
// if (last_calendar_click == null) {
// last_calendar_click = 'arrive';
// }
// else if (last_calendar_click == 'arrive') {
// last_calendar_click = 'depart';
// }
// else {
// last_calendar_click = 'arrive';
// }
}
/**
* Used by both front-end (Book page) and backend (booking node edit forms).
......@@ -719,6 +764,10 @@ function save_booking(data_object, type, success_callbacks, always_callbacks) {
show_thank_you_dialog(data.nid, booking_info_container);
arrival_field.val(null);
departure_field.val(null);
num_nights_field.val(null);
Drupal.abookings.invoke_callbacks(success_callbacks);
});
......@@ -819,7 +868,7 @@ Drupal.abookings = {
*/
get_bookable_data: function() {
var bookable_nid = Drupal.abookings.get_bookable_nid();
console.log('bookable_nid: ', bookable_nid);
// console.log('bookable_nid: ', bookable_nid);
var active_bookable = null;
$.each(bookable_data['bookables'], function(index, bookable) {
......@@ -1229,12 +1278,17 @@ Drupal.abookings = {
// start_date and end_date must be Moment.js date objects.
start_date = moment(arrival_field.val());
start_date = moment(arrival_field.val() + ' 09');
// console.log('start_date: ', start_date);
end_date = moment(departure_field.val());
end_date = moment(departure_field.val() + ' 09');
// console.log('end_date: ', end_date);
var t = new Date(); // Use current day and time
t.setHours(0,0,0,0); // Set time to 00:00am
var today = moment(t);
// console.log('today: ', today);
var total_cost = 0;
var num_guests = parseInt(num_guests_field.val());
var guest_multiplier = num_guests;
......@@ -1244,8 +1298,13 @@ Drupal.abookings = {
guest_multiplier = 1;
}
if (! (start_date.unix() > today.unix())) {
alert('You cannot book a date in the past.');
return null;
}
if (! (end_date.unix() - start_date.unix() > 0)) {
alert('The dates you selected for your booking are invalid.');
return null;
}
......
......@@ -28,7 +28,7 @@ class BookingController extends ControllerBase {
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function post(Request $request) {
public function post(Request $request) {
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
......@@ -89,7 +89,7 @@ class BookingController extends ControllerBase {
// Validate promotion
if ($promo_code_provided) {
try {
try {
$promo = get_promo($promo_code_provided, $checkin_date, $nights);
}
catch (BadRequestHttpException $e) {
......@@ -111,7 +111,7 @@ class BookingController extends ControllerBase {
$discount = $booking_total * ($discount_percentage / 100) . "";
// kint($discount, '$discount');
save_promo_line_item($promo_code_provided, $discount, $booking->id(), $checkin_date);
\save_promo_line_item($promo_code_provided, $discount, $booking->id(), $checkin_date);
}
else {
$response_data['error_message'] = 'Promo code provided is not valid';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment