Skip to content

Commit

Permalink
Make sure that editing a working plan exception and changing the date…
Browse files Browse the repository at this point in the history
… will not keep the previous entry in place (#1515)
  • Loading branch information
alextselegidis committed May 24, 2024
1 parent af2e3a0 commit 07bc7b3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions application/controllers/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ public function save_working_plan_exception(): void

$date = request('date');

$original_date = request('original_date');

$working_plan_exception = request('working_plan_exception');

if (!$working_plan_exception) {
Expand All @@ -478,6 +480,10 @@ public function save_working_plan_exception(): void

$this->providers_model->save_working_plan_exception($provider_id, $date, $working_plan_exception);

if ($date !== $original_date) {
$this->providers_model->delete_working_plan_exception($provider_id, $original_date);
}

json_response([
'success' => true,
]);
Expand Down
11 changes: 10 additions & 1 deletion assets/js/http/calendar_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,26 @@ App.Http.Calendar = (function () {
* @param {Number} providerId Contains the working plan exceptions data.
* @param {Function} successCallback The ajax success callback function.
* @param {Function} errorCallback The ajax failure callback function.
* @param {Date} [originalDate] On edit, provide the original date.
*
* @return {*|jQuery}
*/
function saveWorkingPlanException(date, workingPlanException, providerId, successCallback, errorCallback) {
function saveWorkingPlanException(
date,
workingPlanException,
providerId,
successCallback,
errorCallback,
originalDate,
) {
const url = App.Utils.Url.siteUrl('calendar/save_working_plan_exception');

const data = {
csrf_token: vars('csrf_token'),
date: date,
working_plan_exception: workingPlanException,
provider_id: providerId,
original_date: originalDate,
};

return $.post(url, data)
Expand Down
9 changes: 7 additions & 2 deletions assets/js/utils/calendar_default_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ App.Utils.CalendarDefaultView = (function () {
const data = lastFocusedEventData.extendedProps.data;

if (data.hasOwnProperty('workingPlanException')) {
const date = lastFocusedEventData.extendedProps.data.date;
const originalDate = lastFocusedEventData.extendedProps.data.date;
const workingPlanException = lastFocusedEventData.extendedProps.data.workingPlanException;
const provider = lastFocusedEventData.extendedProps.data.provider;

App.Components.WorkingPlanExceptionsModal.edit(date, workingPlanException).done(
App.Components.WorkingPlanExceptionsModal.edit(originalDate, workingPlanException).done(
(date, workingPlanException) => {
const successCallback = () => {
App.Layouts.Backend.displayNotification(lang('working_plan_exception_saved'));
Expand All @@ -103,6 +103,10 @@ App.Utils.CalendarDefaultView = (function () {

workingPlanExceptions[date] = workingPlanException;

if (date !== originalDate) {
delete workingPlanExceptions[originalDate];
}

for (const index in vars('available_providers')) {
const availableProvider = vars('available_providers')[index];

Expand All @@ -122,6 +126,7 @@ App.Utils.CalendarDefaultView = (function () {
provider.id,
successCallback,
null,
originalDate,
);
},
);
Expand Down
9 changes: 7 additions & 2 deletions assets/js/utils/calendar_table_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ App.Utils.CalendarTableView = (function () {
let endMoment;

if (lastFocusedEventData.extendedProps.data.workingPlanException) {
const date = lastFocusedEventData.extendedProps.data.date;
const originalDate = lastFocusedEventData.extendedProps.data.date;
const workingPlanException = lastFocusedEventData.extendedProps.data.workingPlanException;
const provider = lastFocusedEventData.extendedProps.data.provider;

App.Components.WorkingPlanExceptionsModal.edit(date, workingPlanException).done(
App.Components.WorkingPlanExceptionsModal.edit(originalDate, workingPlanException).done(
(date, workingPlanException) => {
const successCallback = () => {
App.Layouts.Backend.displayNotification(lang('working_plan_exception_saved'));
Expand All @@ -178,6 +178,10 @@ App.Utils.CalendarTableView = (function () {

workingPlanExceptions[date] = workingPlanException;

if (date !== originalDate) {
delete workingPlanExceptions[originalDate];
}

for (const index in vars('available_providers')) {
const availableProvider = vars('available_providers')[index];

Expand All @@ -197,6 +201,7 @@ App.Utils.CalendarTableView = (function () {
provider.id,
successCallback,
null,
originalDate,
);
},
);
Expand Down

0 comments on commit 07bc7b3

Please sign in to comment.