EventTables.svelte 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <script lang="ts">
  2. import { goto } from '$app/navigation';
  3. import { api_host, authToken } from '$lib';
  4. import type { Appointment } from '$lib/dbdata';
  5. import { authFetch, isDueDatePassed } from '$lib/funcs';
  6. import { onMount } from 'svelte';
  7. let appointmentData: Appointment[] = [];
  8. let ready: boolean = false;
  9. let errorMessage: string = '';
  10. let actionCounter: number = 0;
  11. let action: {action: string, id: string} = {action: '', id: ''};
  12. async function fetchAppointments() {
  13. try {
  14. const response = await authFetch(`${api_host}/api/users/appointments`, {
  15. method: 'GET',
  16. headers: {
  17. Authorization: 'Bearer ' + $authToken
  18. }
  19. });
  20. const data: Appointment[] = await response.json();
  21. appointmentData = data;
  22. console.log(appointmentData);
  23. } catch (error) {
  24. console.log('Error during fetch:', error);
  25. }
  26. }
  27. function copyShareLink(appointmentId: string) {
  28. window.navigator.clipboard.writeText(
  29. `${window.location.origin}/appointment?id=${appointmentId}`
  30. );
  31. }
  32. async function deleteEntry(appointmentId: string) {
  33. manageAppointment(appointmentId, "DELETE", "delete");
  34. }
  35. async function lockAppointment(appointmentId: string) {
  36. manageAppointment(appointmentId, "POST", "lock");
  37. }
  38. async function manageAppointment(appointmentId: string, method: 'DELETE' | 'POST', path: string) {
  39. actionCounter += 1;
  40. if (action.action !== method || action.id !== appointmentId) {
  41. actionCounter = 1;
  42. action = {action: method, id: appointmentId};
  43. }
  44. if (actionCounter !== 3) {
  45. return;
  46. }
  47. actionCounter = 0;
  48. try {
  49. const response = await fetch(`${api_host}/api/users/${path}`, {
  50. method: method,
  51. headers: {
  52. Authorization: 'Bearer ' + $authToken,
  53. 'Content-Type': 'application/json'
  54. },
  55. body: JSON.stringify({
  56. appointmentId
  57. })
  58. });
  59. if (response.status === 404) {
  60. console.log(`entry not found`);
  61. errorMessage = `entry not found`;
  62. await fetchAppointments();
  63. setTimeout(() => errorMessage = "", 4000);
  64. }
  65. if (response.status === 200 && path === "delete") {
  66. appointmentData = appointmentData.filter((element) => element._id !== appointmentId);
  67. }
  68. if (response.status === 200 && path === "lock") {
  69. await fetchAppointments();
  70. errorMessage = "";
  71. }
  72. } catch (error) {
  73. console.log('Error during fetch:', error);
  74. }
  75. }
  76. function redirectEditCreator(appointmentId: string) {
  77. goto(`/user/creator?${appointmentId}`);
  78. }
  79. onMount(async () => {
  80. await fetchAppointments();
  81. ready = true;
  82. });
  83. </script>
  84. {#each appointmentData as data}
  85. <tr>
  86. <td>{data.title}</td>
  87. <td class="center-align">
  88. <a href={`/appointment?id=${data._id}`} class="button medium cyan">
  89. <span>Teilen</span>
  90. </a>
  91. </td>
  92. {#if !isDueDatePassed(data.dueDate)}
  93. <td on:click={() => lockAppointment(data._id)} class="center-align">
  94. <button class="medium">
  95. <span>Sperren</span>
  96. </button>
  97. </td>
  98. {:else}
  99. <td class="center-align">
  100. <button class="medium secondary">
  101. <span>Gesperrt</span>
  102. </button>
  103. </td>
  104. {/if}
  105. <!-- load into the creator -->
  106. <td class="center-align">
  107. <a href={`/user/creator?id=${data._id}`} class="button medium secondary blue">
  108. <span>Bearbeiten</span>
  109. </a>
  110. </td>
  111. <!-- send a delete request -->
  112. <td on:click={() => deleteEntry(data._id)} class="center-align">
  113. <button class="medium red">
  114. <span>Loeschen</span>
  115. </button>
  116. </td>
  117. </tr>
  118. {/each}
  119. {#if errorMessage.length > 0}
  120. <div class="snackbar error active">{errorMessage}</div>
  121. {/if}
  122. {#if actionCounter > 0}
  123. <div class="snackbar active">Click another two times to confirm</div>
  124. {/if}