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.'); + } +}); +