浏览代码

edit appointments implemented

Liontix 11 月之前
父节点
当前提交
e15d4f6aa7

+ 1 - 1
public/html/appointments.html

@@ -21,7 +21,7 @@
         <li><a href="#" id="logout-button">Logout</a></li>
     </ul>
 </nav>
-<div class="content">
+<div class="content hidden" id="content">
 
 <div class="container">
 

+ 1 - 0
public/javascripts/appointments.js

@@ -8,6 +8,7 @@ let click_counter = 0;
 // Call the function to render data
 window.addEventListener("load", async function () {
     await fetchAppointments();
+    document.getElementById("content").classList.remove("hidden");
 });
 
 

+ 25 - 4
public/javascripts/creator-appointments.js

@@ -29,6 +29,7 @@ window.addEventListener("load", async function() {
         } else {
             // modify appointment
             // TODO Add a modify appointment function
+            await editAppointment(appointment);
         }
     })
 });
@@ -45,14 +46,14 @@ async function displayAppointment(data) {
 async function loadAppointment(appointment) {
     let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
 
-    const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
+    const response = await fetch(`/api/schedule?appointmentId=${appointment}&isUser=true`, options)
         .catch(err => console.error('error:' + err));
 
     const js = await parseOrRedirect(response);
     await displayAppointment(js);
 }
 
-async function createAppointment() {
+function getParameters() {
     const title = titleInput.value;
     const description = descriptionInput.value;
     const dueDate = dueDateInput.value;
@@ -60,13 +61,28 @@ async function createAppointment() {
     const endDate = endDateInput.value;
     const place = placeInput.value;
 
+    return {title, description, dueDate, startDate, endDate, place};
+}
+
+
+async function editAppointment(appointment) {
+    await setAppointment("modify", appointment);
+}
+
+async function createAppointment() {
+    await setAppointment("create", "");
+}
+
+async function setAppointment(endpoint, appointment) {
+    const { title, description, dueDate, startDate, endDate, place } = getParameters();
+
     if (!title || !description || !dueDate || !startDate || !endDate) {
         appointmentSuccessParagraph.innerText = "";
         appointmentErrorParagraph.innerText = "Not fields filled";
         return;
     }
 
-    const response = await fetchWithToken('/api/users/create', {
+    const response = await fetchWithToken(`/api/users/${endpoint}`, {
         method: 'POST',
         body: JSON.stringify({
             title: title,
@@ -74,7 +90,8 @@ async function createAppointment() {
             dueDate: dueDate,
             startDate: startDate,
             endDate: endDate,
-            place: place
+            place: place,
+            appointmentId: appointment
         })
     });
     const js = await response.json();
@@ -87,6 +104,10 @@ async function createAppointment() {
         appointmentErrorParagraph.innerText = "";
         appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
             "was copied to your clipboard";
+        if (endpoint === "modify") {
+            appointmentSuccessParagraph.innerText = "Appointment edited successfully. The share link " +
+                "was copied to your clipboard";
+        }
         await navigator.clipboard.writeText(`${window.location.origin}${path}`);
     }
 }

+ 4 - 0
public/stylesheets/style.css

@@ -11,6 +11,10 @@ body {
   background-color: var(--background-color);
 }
 
+.hidden {
+  visibility: hidden;
+}
+
 .content {
   margin: 0;
   padding: 0;

+ 1 - 1
routes/members.js

@@ -52,7 +52,7 @@ router.post('/create', function(req, res, next) {
 
 });
 
-router.put('/modify', function (req, res, next) {
+router.post('/modify', function (req, res, next) {
     const user = req.userId;
 
     const { appointmentId, title, description, dueDate, place, startDate, endDate } = req.body;