From 714a82575b9fc412e702be10f2dd3ea0f834eca3 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Mon, 21 Oct 2024 23:22:45 +0300 Subject: [PATCH] add refresh button event listener --- script.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 81eed0d..63e4003 100644 --- a/script.js +++ b/script.js @@ -1,9 +1,11 @@ const form = document.getElementById('merge-form'); const calendars = document.getElementById('calendars'); const addCalendarButton = document.getElementById('add-calendar'); +const refreshCalendarsButton = document.getElementById('refresh-calendars'); const result = document.getElementById('result'); let calendarIndex = 1; +let mergedUrl = ''; // Function to validate URL format function isValidUrl(url) { @@ -64,6 +66,7 @@ function isValidUrl(url) { return response.json(); }) .then((data) => { + mergedUrl = data.url; result.innerHTML = `Merged calendar URL: ${data.url}`; console.log('Links added successfully!'); }) @@ -72,4 +75,29 @@ function isValidUrl(url) { result.innerHTML = `Error merging calendars: ${error.message || 'Unknown error'}` }); } - }); \ No newline at end of file + }); + + // Refresh button event listener +refreshCalendarsButton.addEventListener('click', () => { + if (mergedUrl) { + // Call the server to refresh the merged calendar + fetch(`/refresh/${mergedUrl.split('/').pop()}`) // Extract the calendar ID from the URL + .then((response) => { + if (!response.ok) { + throw new Error('Failed to refresh calendar data'); + } + return response.json(); + }) + .then((data) => { + result.innerHTML = `Merged calendar URL: ${mergedUrl}`; + console.log('Calendar data refreshed successfully!'); + }) + .catch((error) => { + console.error('Error refreshing calendar data:', error); + result.innerHTML = `Error refreshing calendar data: ${error.message || 'Unknown error'}`; + }); + } else { + alert('No merged calendar URL available to refresh.'); + } +}); +