Update the blocked period CRUD (#432)

This commit is contained in:
Alex Tselegidis 2023-11-17 08:04:27 +01:00
parent 9e2c4827aa
commit 1c3ae956aa
5 changed files with 80 additions and 24 deletions

View file

@ -120,6 +120,8 @@ const WEBHOOK_SECRETARY_SAVE = 'secretary_save';
const WEBHOOK_SECRETARY_DELETE = 'secretary_delete'; const WEBHOOK_SECRETARY_DELETE = 'secretary_delete';
const WEBHOOK_ADMIN_SAVE = 'admin_save'; const WEBHOOK_ADMIN_SAVE = 'admin_save';
const WEBHOOK_ADMIN_DELETE = 'admin_delete'; const WEBHOOK_ADMIN_DELETE = 'admin_delete';
const WEBHOOK_BLOCKED_PERIOD_SAVE = 'blocked_period_save';
const WEBHOOK_BLOCKED_PERIOD_DELETE = 'blocked_period_delete';
/* End of file constants.php */ /* End of file constants.php */
/* Location: ./application/config/constants.php */ /* Location: ./application/config/constants.php */

View file

@ -63,6 +63,9 @@ class Blocked_periods extends EA_Controller {
script_vars([ script_vars([
'user_id' => $user_id, 'user_id' => $user_id,
'role_slug' => $role_slug, 'role_slug' => $role_slug,
'date_format' => setting('date_format'),
'time_format' => setting('time_format'),
'first_weekday' => setting('first_weekday'),
]); ]);
html_vars([ html_vars([
@ -118,22 +121,24 @@ class Blocked_periods extends EA_Controller {
abort(403, 'Forbidden'); abort(403, 'Forbidden');
} }
$service_category = request('service_category'); $blocked_period = request('blocked_period');
$this->blocked_periods_model->only($service_category, [ $this->blocked_periods_model->only($blocked_period, [
'name', 'name',
'start_datetime',
'end_datetime',
'description' 'description'
]); ]);
$service_category_id = $this->blocked_periods_model->save($service_category); $blocked_period_id = $this->blocked_periods_model->save($blocked_period);
$service_category = $this->blocked_periods_model->find($service_category_id); $blocked_period = $this->blocked_periods_model->find($blocked_period_id);
$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category); $this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_SAVE, $blocked_period);
json_response([ json_response([
'success' => TRUE, 'success' => TRUE,
'id' => $service_category_id 'id' => $blocked_period_id
]); ]);
} }
catch (Throwable $e) catch (Throwable $e)
@ -154,23 +159,25 @@ class Blocked_periods extends EA_Controller {
abort(403, 'Forbidden'); abort(403, 'Forbidden');
} }
$service_category = request('service_category'); $blocked_period = request('blocked_period');
$this->blocked_periods_model->only($service_category, [ $this->blocked_periods_model->only($blocked_period, [
'id', 'id',
'name', 'name',
'start_datetime',
'end_datetime',
'description' 'description'
]); ]);
$service_category_id = $this->blocked_periods_model->save($service_category); $blocked_period_id = $this->blocked_periods_model->save($blocked_period);
$service_category = $this->blocked_periods_model->find($service_category_id); $blocked_period = $this->blocked_periods_model->find($blocked_period_id);
$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_SAVE, $service_category); $this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_SAVE, $blocked_period);
json_response([ json_response([
'success' => TRUE, 'success' => TRUE,
'id' => $service_category_id 'id' => $blocked_period_id
]); ]);
} }
catch (Throwable $e) catch (Throwable $e)
@ -191,13 +198,13 @@ class Blocked_periods extends EA_Controller {
abort(403, 'Forbidden'); abort(403, 'Forbidden');
} }
$service_category_id = request('service_category_id'); $blocked_period_id = request('blocked_period_id');
$service_category = $this->blocked_periods_model->find($service_category_id); $blocked_period = $this->blocked_periods_model->find($blocked_period_id);
$this->blocked_periods_model->delete($service_category_id); $this->blocked_periods_model->delete($blocked_period_id);
$this->webhooks_client->trigger(WEBHOOK_SERVICE_CATEGORY_DELETE, $service_category); $this->webhooks_client->trigger(WEBHOOK_BLOCKED_PERIOD_DELETE, $blocked_period);
json_response([ json_response([
'success' => TRUE, 'success' => TRUE,
@ -221,11 +228,11 @@ class Blocked_periods extends EA_Controller {
abort(403, 'Forbidden'); abort(403, 'Forbidden');
} }
$service_category_id = request('service_category_id'); $blocked_period_id = request('blocked_period_id');
$service_category = $this->blocked_periods_model->find($service_category_id); $blocked_period = $this->blocked_periods_model->find($blocked_period_id);
json_response($service_category); json_response($blocked_period);
} }
catch (Throwable $e) catch (Throwable $e)
{ {

View file

@ -71,19 +71,19 @@
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="start-datetime"> <label class="form-label" for="start-date-time">
<?= lang('start') ?> <?= lang('start') ?>
<span class="text-danger" hidden>*</span> <span class="text-danger" hidden>*</span>
</label> </label>
<input id="start-datetime" class="form-control required" disabled> <input id="start-date-time" class="form-control required" disabled>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label class="form-label" for="end-datetime"> <label class="form-label" for="end-date-time">
<?= lang('end') ?> <?= lang('end') ?>
<span class="text-danger" hidden>*</span> <span class="text-danger" hidden>*</span>
</label> </label>
<input id="end-datetime" class="form-control required" disabled> <input id="end-date-time" class="form-control required" disabled>
</div> </div>
<div class="mb-3"> <div class="mb-3">
@ -104,6 +104,7 @@
<script src="<?= asset_url('assets/js/utils/message.js') ?>"></script> <script src="<?= asset_url('assets/js/utils/message.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/validation.js') ?>"></script> <script src="<?= asset_url('assets/js/utils/validation.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/url.js') ?>"></script> <script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
<script src="<?= asset_url('assets/js/utils/ui.js') ?>"></script>
<script src="<?= asset_url('assets/js/http/blocked_periods_http_client.js') ?>"></script> <script src="<?= asset_url('assets/js/http/blocked_periods_http_client.js') ?>"></script>
<script src="<?= asset_url('assets/js/pages/blocked_periods.js') ?>"></script> <script src="<?= asset_url('assets/js/pages/blocked_periods.js') ?>"></script>

View file

@ -19,7 +19,11 @@ App.Pages.BlockedPeriods = (function () {
const $filterBlockedPeriods = $('#filter-blocked-periods'); const $filterBlockedPeriods = $('#filter-blocked-periods');
const $id = $('#id'); const $id = $('#id');
const $name = $('#name'); const $name = $('#name');
const $startDateTime = $('#start-date-time');
const $endDateTime = $('#end-date-time');
const $description = $('#description'); const $description = $('#description');
const moment = window.moment;
let filterResults = {}; let filterResults = {};
let filterLimit = 20; let filterLimit = 20;
@ -117,8 +121,15 @@ App.Pages.BlockedPeriods = (function () {
* Event: Blocked period Save Button "Click" * Event: Blocked period Save Button "Click"
*/ */
$blockedPeriods.on('click', '#save-blocked-period', () => { $blockedPeriods.on('click', '#save-blocked-period', () => {
const startDateTimeObject = App.Utils.UI.getDatetimepickerValue($startDateTime);
const startDateTimeMoment = moment(startDateTimeObject);
const endDateTimeObject = App.Utils.UI.getDatetimepickerValue($endDateTime);
const endDateTimeMoment = moment(endDateTimeObject);
const blockedPeriod = { const blockedPeriod = {
name: $name.val(), name: $name.val(),
start_datetime: startDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
end_datetime: endDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
description: $description.val() description: $description.val()
}; };
@ -222,6 +233,8 @@ App.Pages.BlockedPeriods = (function () {
function display(blockedPeriod) { function display(blockedPeriod) {
$id.val(blockedPeriod.id); $id.val(blockedPeriod.id);
$name.val(blockedPeriod.name); $name.val(blockedPeriod.name);
App.Utils.UI.setDatetimepickerValue($startDateTime, new Date(blockedPeriod.start_datetime));
App.Utils.UI.setDatetimepickerValue($endDateTime, new Date(blockedPeriod.end_datetime));
$description.val(blockedPeriod.description); $description.val(blockedPeriod.description);
} }
@ -322,6 +335,8 @@ App.Pages.BlockedPeriods = (function () {
resetForm(); resetForm();
filter(''); filter('');
addEventListeners(); addEventListeners();
App.Utils.UI.initializeDatetimepicker($startDateTime);
App.Utils.UI.initializeDatetimepicker($endDateTime);
} }
document.addEventListener('DOMContentLoaded', initialize); document.addEventListener('DOMContentLoaded', initialize);

View file

@ -225,11 +225,42 @@ window.App.Utils.UI = (function () {
$target.trumbowyg(params); $target.trumbowyg(params);
} }
/**
* Get Date, Date-Time or Time picker value.
*
* @param {jQuery} $target
*
* @return {Date}
*/
function getDatetimepickerValue($target) {
if (!$target?.length) {
throw new Error('Empty $target argument provided.')
}
return $target[0]._flatpickr.selectedDates[0];
}
/**
* Set Date, Date-Time or Time picker value.
*
* @param {jQuery} $target
* @param {Date} value
*/
function setDatetimepickerValue($target, value) {
if (!$target?.length) {
throw new Error('Empty $target argument provided.')
}
return $target[0]._flatpickr.setDate(value);
}
return { return {
initializeDatetimepicker, initializeDatetimepicker,
initializeDatepicker, initializeDatepicker,
initializeTimepicker, initializeTimepicker,
initializeDropdown, initializeDropdown,
initializeTextEditor, initializeTextEditor,
getDatetimepickerValue,
setDatetimepickerValue,
}; };
})(); })();