+page.svelte 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <script lang="ts">
  2. import { goto } from '$app/navigation';
  3. import { page } from '$app/stores';
  4. import { api_host } from '$lib';
  5. import type {
  6. Appointment,
  7. AppointmentDateTimes,
  8. AppointmentInputs,
  9. DynamicJson
  10. } from '$lib/dbdata';
  11. import AppointmentConfirmDialog from '$lib/modules/AppointmentConfirmDialog.svelte';
  12. import AppointmentInfo from '$lib/modules/AppointmentInfo.svelte';
  13. import DatePicker from '$lib/modules/DatePicker.svelte';
  14. import DateTimes from '$lib/modules/DateTimes.svelte';
  15. import { onMount } from 'svelte';
  16. let appointment: Appointment;
  17. let selectedDate: Date = new Date();
  18. let activateModal: boolean = false;
  19. let dynamicInputs: DynamicJson[] = [];
  20. let appointmentDateTimes: AppointmentDateTimes[] = [];
  21. async function getAppointmentInfo(appointmentId: string) {
  22. const response = await fetch(`${api_host}/api/schedule?appointmentId=${appointmentId}`, {
  23. method: 'GET',
  24. headers: {
  25. 'Content-Type': 'application/json'
  26. }
  27. });
  28. const js: Appointment = await response.json();
  29. appointment = js;
  30. appointmentDateTimes = js.times.filter((element) => element.available === true);
  31. dynamicInputs = js.inputs;
  32. if (response.status === 404) {
  33. goto('/errors/404');
  34. }
  35. if (response.status === 410) {
  36. goto('/errors/410');
  37. }
  38. }
  39. onMount(async () => {
  40. const id = $page.url.searchParams.get('id');
  41. if (!id) {
  42. goto('/errors/404');
  43. } else {
  44. await getAppointmentInfo(id);
  45. }
  46. });
  47. </script>
  48. {#if appointment}
  49. <DatePicker bind:appointmentDateTimes bind:selectedDate />
  50. <div class="top-margin w-80 inline-margin">
  51. <AppointmentInfo
  52. bind:title={appointment.title}
  53. bind:description={appointment.description}
  54. bind:location={appointment.place}
  55. />
  56. {#key selectedDate}
  57. <DateTimes bind:activateModal bind:selectedDate bind:availableTimes={appointmentDateTimes} />
  58. {/key}
  59. </div>
  60. <AppointmentConfirmDialog
  61. bind:appointment
  62. bind:dynamicInputs
  63. bind:active={activateModal}
  64. bind:date={selectedDate}
  65. />
  66. {/if}
  67. <style>
  68. .w-80 {
  69. max-width: 600px;
  70. }
  71. .inline-margin {
  72. margin-inline: auto;
  73. }
  74. </style>