brotherton-erpnext/erpnext/www/book_appointment/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

251 lines
8.6 KiB
JavaScript
Raw Normal View History

2019-10-03 11:05:08 +00:00
frappe.ready(async () => {
initialise_select_date();
2019-09-03 06:34:52 +00:00
})
2019-10-03 11:05:08 +00:00
2019-09-03 06:34:52 +00:00
async function initialise_select_date() {
navigate_to_page(1);
2019-09-09 10:11:20 +00:00
await get_global_variables();
setup_date_picker();
setup_timezone_selector();
hide_next_button();
}
async function get_global_variables() {
2019-11-14 05:55:49 +00:00
// Using await through this file instead of then.
2019-09-09 10:11:20 +00:00
window.appointment_settings = (await frappe.call({
method: 'erpnext.www.book_appointment.index.get_appointment_settings'
2019-10-03 11:05:08 +00:00
})).message;
2019-09-09 10:11:20 +00:00
window.timezones = (await frappe.call({
method:'erpnext.www.book_appointment.index.get_timezones'
2019-09-03 06:34:52 +00:00
})).message;
2019-09-09 10:11:20 +00:00
}
function setup_timezone_selector() {
2019-09-03 06:34:52 +00:00
let timezones_element = document.getElementById('appointment-timezone');
let local_timezone = moment.tz.guess()
window.timezones.forEach(timezone => {
2019-09-17 11:28:41 +00:00
let opt = document.createElement('option');
2019-10-03 06:26:23 +00:00
opt.value = timezone;
if (timezone == local_timezone) {
2019-10-03 06:26:23 +00:00
opt.selected = true;
}
opt.innerHTML = timezone;
2019-09-03 06:34:52 +00:00
timezones_element.appendChild(opt)
});
}
2019-09-09 10:11:20 +00:00
function setup_date_picker() {
let date_picker = document.getElementById('appointment-date');
let today = new Date();
date_picker.min = today.toISOString().substr(0, 10);
today.setDate(today.getDate() + window.appointment_settings.advance_booking_days);
2019-09-17 11:28:41 +00:00
date_picker.max = today.toISOString().substr(0, 10);
2019-09-09 10:11:20 +00:00
}
2019-09-17 11:28:41 +00:00
function hide_next_button() {
2019-09-09 10:11:20 +00:00
let next_button = document.getElementById('next-button');
next_button.disabled = true;
next_button.onclick = () => frappe.msgprint(__("Please select a date and time"));
2019-09-09 10:11:20 +00:00
}
2019-09-17 11:28:41 +00:00
function show_next_button() {
2019-09-09 10:11:20 +00:00
let next_button = document.getElementById('next-button');
next_button.disabled = false;
next_button.onclick = setup_details_page;
}
function on_date_or_timezone_select() {
2019-09-03 06:34:52 +00:00
let date_picker = document.getElementById('appointment-date');
2019-09-09 10:11:20 +00:00
let timezone = document.getElementById('appointment-timezone');
2019-09-03 06:34:52 +00:00
if (date_picker.value === '') {
2019-09-09 10:11:20 +00:00
clear_time_slots();
hide_next_button();
frappe.throw(__('Please select a date'));
2019-09-03 06:34:52 +00:00
}
2019-09-09 10:11:20 +00:00
window.selected_date = date_picker.value;
window.selected_timezone = timezone.value;
update_time_slots(date_picker.value, timezone.value);
2019-09-11 08:55:26 +00:00
let lead_text = document.getElementById('lead-text');
lead_text.innerHTML = __("Select Time")
2019-09-03 06:34:52 +00:00
}
2019-09-09 10:11:20 +00:00
async function get_time_slots(date, timezone) {
let slots = (await frappe.call({
method: 'erpnext.www.book_appointment.index.get_appointment_slots',
2019-09-03 06:34:52 +00:00
args: {
date: date,
timezone: timezone
}
})).message;
2019-09-09 10:11:20 +00:00
return slots;
}
async function update_time_slots(selected_date, selected_timezone) {
2019-09-03 06:34:52 +00:00
let timeslot_container = document.getElementById('timeslot-container');
2019-09-09 10:11:20 +00:00
window.slots = await get_time_slots(selected_date, selected_timezone);
clear_time_slots();
if (window.slots.length <= 0) {
2019-09-03 06:34:52 +00:00
let message_div = document.createElement('p');
message_div.innerHTML = __("There are no slots available on this date");
2019-09-03 06:34:52 +00:00
timeslot_container.appendChild(message_div);
2019-09-09 10:11:20 +00:00
return
2019-09-03 06:34:52 +00:00
}
2019-09-17 11:28:41 +00:00
window.slots.forEach((slot, index) => {
// Get and append timeslot div
let timeslot_div = get_timeslot_div_layout(slot)
2019-09-03 06:34:52 +00:00
timeslot_container.appendChild(timeslot_div);
2019-09-09 10:11:20 +00:00
});
set_default_timeslot();
}
2019-09-17 11:28:41 +00:00
function get_timeslot_div_layout(timeslot) {
let start_time = new Date(timeslot.time)
let timeslot_div = document.createElement('div');
timeslot_div.classList.add('time-slot');
if (!timeslot.availability) {
timeslot_div.classList.add('unavailable')
}
timeslot_div.innerHTML = get_slot_layout(start_time);
2019-12-11 09:32:23 +00:00
timeslot_div.id = timeslot.time.substring(11, 19);
2019-09-17 11:28:41 +00:00
timeslot_div.addEventListener('click', select_time);
return timeslot_div
}
2019-09-09 10:11:20 +00:00
function clear_time_slots() {
2019-09-17 11:28:41 +00:00
// Clear any existing divs in timeslot container
2019-09-09 10:11:20 +00:00
let timeslot_container = document.getElementById('timeslot-container');
while (timeslot_container.firstChild) {
2019-09-18 09:03:10 +00:00
timeslot_container.removeChild(timeslot_container.firstChild);
2019-09-03 06:34:52 +00:00
}
2019-09-09 10:11:20 +00:00
}
function get_slot_layout(time) {
2019-11-13 10:30:59 +00:00
let timezone = document.getElementById("appointment-timezone").value;
2019-09-18 09:03:10 +00:00
time = new Date(time);
2019-11-13 10:30:59 +00:00
let start_time_string = moment(time).tz(timezone).format("LT");
let end_time = moment(time).tz(timezone).add(window.appointment_settings.appointment_duration, 'minutes');
2019-09-09 10:11:20 +00:00
let end_time_string = end_time.format("LT");
return `<span style="font-size: 1.2em;">${start_time_string}</span><br><span class="text-muted small">${__("to") } ${end_time_string}</span>`;
2019-09-03 06:34:52 +00:00
}
function select_time() {
2019-09-17 11:28:41 +00:00
if (this.classList.contains('unavailable')) {
2019-09-18 09:03:10 +00:00
return;
2019-09-03 06:34:52 +00:00
}
2019-09-17 11:28:41 +00:00
let selected_element = document.getElementsByClassName('selected');
2019-10-04 06:02:39 +00:00
if (!(selected_element.length > 0)) {
2019-09-18 09:03:10 +00:00
this.classList.add('selected');
show_next_button();
return;
2019-09-03 06:34:52 +00:00
}
2019-09-17 11:28:41 +00:00
selected_element = selected_element[0]
2019-09-18 09:03:10 +00:00
window.selected_time = this.id;
2019-09-17 11:28:41 +00:00
selected_element.classList.remove('selected');
this.classList.add('selected');
show_next_button();
2019-09-03 06:34:52 +00:00
}
function set_default_timeslot() {
let timeslots = document.getElementsByClassName('time-slot')
2019-09-18 09:03:10 +00:00
// Can't use a forEach here since, we need to break the loop after a timeslot is selected
2019-09-03 06:34:52 +00:00
for (let i = 0; i < timeslots.length; i++) {
const timeslot = timeslots[i];
if (!timeslot.classList.contains('unavailable')) {
2019-09-17 11:28:41 +00:00
timeslot.classList.add('selected');
2019-09-03 06:34:52 +00:00
break;
}
}
}
2019-09-17 11:28:41 +00:00
function navigate_to_page(page_number) {
2019-09-09 10:11:20 +00:00
let page1 = document.getElementById('select-date-time');
let page2 = document.getElementById('enter-details');
2019-09-17 11:28:41 +00:00
switch (page_number) {
case 1:
page1.style.display = 'block';
page2.style.display = 'none';
break;
case 2:
page1.style.display = 'none';
page2.style.display = 'block';
break;
default:
2019-09-17 11:28:41 +00:00
break;
}
}
2019-09-17 11:28:41 +00:00
function setup_details_page() {
navigate_to_page(2)
2019-09-09 10:11:20 +00:00
let date_container = document.getElementsByClassName('date-span')[0];
let time_container = document.getElementsByClassName('time-span')[0];
setup_search_params();
date_container.innerHTML = moment(window.selected_date).format("MMM Do YYYY");
2019-09-17 11:28:41 +00:00
time_container.innerHTML = moment(window.selected_time, "HH:mm:ss").format("LT");
2019-09-03 06:34:52 +00:00
}
function setup_search_params() {
let search_params = new URLSearchParams(window.location.search);
let customer_name = search_params.get("name")
let customer_email = search_params.get("email")
let detail = search_params.get("details")
if (customer_name) {
let name_input = document.getElementById("customer_name");
name_input.value = customer_name;
name_input.disabled = true;
}
if(customer_email) {
let email_input = document.getElementById("customer_email");
email_input.value = customer_email;
email_input.disabled = true;
}
if(detail) {
let detail_input = document.getElementById("customer_notes");
detail_input.value = detail;
detail_input.disabled = true;
}
}
2019-09-03 08:46:47 +00:00
async function submit() {
2019-11-13 06:43:42 +00:00
let button = document.getElementById('submit-button');
button.disabled = true;
let form = document.querySelector('#customer-form');
2019-10-04 06:02:39 +00:00
if (!form.checkValidity()) {
form.reportValidity();
2019-11-13 06:43:42 +00:00
button.disabled = false;
return;
}
2019-11-13 06:43:42 +00:00
let contact = get_form_data();
2019-11-14 07:17:08 +00:00
let appointment = frappe.call({
method: 'erpnext.www.book_appointment.index.create_appointment',
2019-09-03 08:46:47 +00:00
args: {
2019-09-09 11:31:40 +00:00
'date': window.selected_date,
'time': window.selected_time,
2019-11-13 06:43:42 +00:00
'contact': contact,
2019-10-31 10:06:33 +00:00
'tz':window.selected_timezone
2019-11-14 07:17:08 +00:00
},
callback: (response)=>{
if (response.message.status == "Unverified") {
frappe.show_alert(__("Please check your email to confirm the appointment"))
2019-11-14 07:17:08 +00:00
} else {
frappe.show_alert(__("Appointment Created Successfully"));
2019-11-14 07:17:08 +00:00
}
setTimeout(()=>{
let redirect_url = "/";
if (window.appointment_settings.success_redirect_url){
redirect_url += window.appointment_settings.success_redirect_url;
}
window.location.href = redirect_url;},5000)
2019-11-14 07:17:08 +00:00
},
error: (err)=>{
frappe.show_alert(__("Something went wrong please try again"));
2019-11-14 07:17:08 +00:00
button.disabled = false;
2019-09-03 08:46:47 +00:00
}
2019-11-14 07:17:08 +00:00
});
2019-09-17 11:28:41 +00:00
}
2019-09-09 10:11:20 +00:00
2019-09-17 11:28:41 +00:00
function get_form_data() {
2019-09-09 10:11:20 +00:00
contact = {};
2019-11-01 04:21:32 +00:00
let inputs = ['name', 'skype', 'number', 'notes', 'email'];
inputs.forEach((id) => contact[id] = document.getElementById(`customer_${id}`).value)
2019-11-13 06:43:42 +00:00
return contact
2019-09-09 10:11:20 +00:00
}