Untitled

 avatar
unknown
plain_text
20 days ago
2.2 kB
0
Indexable
/**
 * @file
 * Defines behaviors for the Stripe Payment Element payment method form.
 */

(function ($, Drupal, drupalSettings) {

  'use strict';

  function createAppointment(uuid, appointments) {
    return $.ajax({
      url: '/appointment/' + uuid,
      data: { 'appointment': JSON.stringify(appointments) },
    });
  }

  function renderAppointment(appointment) {
    let startDate = new Date(appointment.start_date);

    if ($('#first-appointment[data-queue-date="' + appointment.start_date + '"]').length > 0) {
      startDate.setDate(startDate.getDate() + 6);
      const options = { weekday: 'long', month: 'short', day: '2-digit', year: 'numeric' };
      appointment.start_date = startDate.toLocaleDateString('en-US', options);
    }

    const content = `
      <p class="appointment-element">
        <time class="start-time">${appointment.start_date}</time>
        <br>
        <time class="end-time">${appointment.start_time} - ${appointment.finish_time}</time>
        <br>
        ${appointment.item_index === 0 ? `<a class="appointment-url" href="${appointment.appointment_url}" target="_blank" aria-label="Opens on a new window Change Date & Time">Change Date & Time</a>` : ''}
      </p>`
    ;

    $('[data-queue-date="' + appointment.start_date + '"]').replaceWith(content);
  }


  async function createAppointments(appointments) {
    appointments.forEach((appointment, index) => {
      setTimeout(() => {
        createAppointment(drupalSettings.order_uuid, appointment).done((response) => {
          if (response.code === 200 && response.appointment) {
            renderAppointment(response.appointment);
          }

          if (response.code === 202) {
            // If the appointment is still processing, try again after 5 seconds for current appointment.
            setTimeout(() => {
              createAppointments([appointment]);
            }, 5000);
          }
        });
      }, 1000);
    });
  }

  Drupal.behaviors.gjJunkAppointment = {
    attach: function (context, settings) {
      if (document !== context) {
        return;
      }

      createAppointments(drupalSettings.appointments);
    },
  };

})(jQuery, Drupal, drupalSettings);
Leave a Comment