feat: update ticket management and calendar functionality with improved data handling and local storage support
This commit is contained in:
@@ -35,6 +35,10 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
const storageKeys = {
|
||||
selectedGroup: 'admin_calendar_selected_group',
|
||||
visibleMonth: 'admin_calendar_visible_month'
|
||||
};
|
||||
let enabledDatesSet = new Set();
|
||||
|
||||
const calendarInstance = flatpickr("#admin-flatpickr", {
|
||||
@@ -43,6 +47,12 @@
|
||||
dateFormat: "Y-m-d",
|
||||
disableMobile: true,
|
||||
locale: "pl",
|
||||
onMonthChange: function() {
|
||||
persistVisibleMonth();
|
||||
},
|
||||
onYearChange: function() {
|
||||
persistVisibleMonth();
|
||||
},
|
||||
onChange: function(selectedDates, dateStr, instance) {
|
||||
syncSetWithPicker(instance);
|
||||
}
|
||||
@@ -62,7 +72,47 @@
|
||||
}
|
||||
|
||||
function renderCurrentSetToPicker() {
|
||||
const currentViewDate = new Date(calendarInstance.currentYear, calendarInstance.currentMonth, 1);
|
||||
calendarInstance.setDate(Array.from(enabledDatesSet), false, "Y-m-d");
|
||||
calendarInstance.jumpToDate(currentViewDate, false);
|
||||
}
|
||||
|
||||
function persistSelectedGroup() {
|
||||
try {
|
||||
localStorage.setItem(storageKeys.selectedGroup, getCurrentGroup());
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
function restoreSelectedGroup() {
|
||||
try {
|
||||
const savedGroup = localStorage.getItem(storageKeys.selectedGroup);
|
||||
if (!savedGroup) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($('#ticket_group option[value="' + savedGroup + '"]').length > 0) {
|
||||
$('#ticket_group').val(savedGroup);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
function persistVisibleMonth() {
|
||||
try {
|
||||
const month = String(calendarInstance.currentMonth + 1).padStart(2, "0");
|
||||
const year = String(calendarInstance.currentYear);
|
||||
localStorage.setItem(storageKeys.visibleMonth, year + "-" + month);
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
function restoreVisibleMonth() {
|
||||
try {
|
||||
const savedMonth = localStorage.getItem(storageKeys.visibleMonth);
|
||||
if (!savedMonth || !/^\d{4}-\d{2}$/.test(savedMonth)) {
|
||||
return;
|
||||
}
|
||||
|
||||
calendarInstance.jumpToDate(savedMonth + "-01", false);
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
function getCurrentGroup() {
|
||||
@@ -77,16 +127,33 @@
|
||||
}
|
||||
}
|
||||
|
||||
function parseServerResponse(rawResponse) {
|
||||
if (rawResponse && typeof rawResponse === 'object') {
|
||||
return rawResponse;
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(rawResponse);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function fetchDatesForGroup() {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
url: '/apanel/calendar_dates/',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
ticket_group: getCurrentGroup()
|
||||
},
|
||||
success: function(rawResponse) {
|
||||
const response = $.parseJSON(rawResponse);
|
||||
const response = parseServerResponse(rawResponse);
|
||||
if (!response) {
|
||||
handleError('Nie udalo sie odczytac odpowiedzi serwera.');
|
||||
return;
|
||||
}
|
||||
if (response.status !== 'ok') {
|
||||
handleError(response.message || 'Nie udało się pobrać danych kalendarza.');
|
||||
return;
|
||||
@@ -124,13 +191,18 @@
|
||||
type: 'POST',
|
||||
cache: false,
|
||||
url: '/apanel/calendar_save/',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
ticket_group: getCurrentGroup(),
|
||||
dates: Array.from(enabledDatesSet),
|
||||
csrf_token: $('#calendar_csrf_token').val()
|
||||
},
|
||||
success: function(rawResponse) {
|
||||
const response = $.parseJSON(rawResponse);
|
||||
const response = parseServerResponse(rawResponse);
|
||||
if (!response) {
|
||||
handleError('Nie udalo sie odczytac odpowiedzi serwera.');
|
||||
return;
|
||||
}
|
||||
if (response.status !== 'ok') {
|
||||
handleError(response.message || 'Nie udało się zapisać kalendarza.');
|
||||
return;
|
||||
@@ -147,6 +219,7 @@
|
||||
}
|
||||
|
||||
$('body').on('change', '#ticket_group', function() {
|
||||
persistSelectedGroup();
|
||||
fetchDatesForGroup();
|
||||
});
|
||||
|
||||
@@ -162,6 +235,8 @@
|
||||
saveCalendar();
|
||||
});
|
||||
|
||||
restoreSelectedGroup();
|
||||
restoreVisibleMonth();
|
||||
fetchDatesForGroup();
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user