From 1ad072071157956f87503c8cc7f7ea3e59196f25 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 20 May 2020 16:35:07 +0200 Subject: [PATCH] number-setting directive + refactored admin/settings to use new directive --- .../controllers/admin/settings.js.erb | 25 ----- .../directives/settings/number-setting.js.erb | 53 +++++++++ .../templates/admin/settings/number.html | 15 +++ .../admin/settings/reservations.html | 103 +++++++----------- 4 files changed, 108 insertions(+), 88 deletions(-) create mode 100644 app/assets/javascripts/directives/settings/number-setting.js.erb create mode 100644 app/assets/templates/admin/settings/number.html diff --git a/app/assets/javascripts/controllers/admin/settings.js.erb b/app/assets/javascripts/controllers/admin/settings.js.erb index 00bdab3e9..3f7fc53a4 100644 --- a/app/assets/javascripts/controllers/admin/settings.js.erb +++ b/app/assets/javascripts/controllers/admin/settings.js.erb @@ -88,31 +88,6 @@ Application.Controllers.controller('SettingsController', ['$scope', '$rootScope' $scope.customFavicon = faviconFile.custom_asset; $scope.profileImage = profileImageFile.custom_asset; - $scope.moveDelay = { - name: 'booking_move_delay', - value: parseInt(settingsPromise.booking_move_delay, 10) - }; - - $scope.cancelDelay = { - name: 'booking_cancel_delay', - value: parseInt(settingsPromise.booking_cancel_delay, 10) - }; - - $scope.reminderDelay = { - name: 'reminder_delay', - value: parseInt(settingsPromise.reminder_delay, 10) - }; - - $scope.visibilityYearly = { - name: 'visibility_yearly', - value: parseInt(settingsPromise.visibility_yearly, 10) - }; - - $scope.visibilityOthers = { - name: 'visibility_others', - value: parseInt(settingsPromise.visibility_others, 10) - }; - // By default, we display the currently published privacy policy $scope.privacyPolicy = { version: null, diff --git a/app/assets/javascripts/directives/settings/number-setting.js.erb b/app/assets/javascripts/directives/settings/number-setting.js.erb new file mode 100644 index 000000000..32e69f793 --- /dev/null +++ b/app/assets/javascripts/directives/settings/number-setting.js.erb @@ -0,0 +1,53 @@ +Application.Directives.directive('numberSetting', ['Setting', 'growl', '_t', + function (Setting, growl, _t) { + return ({ + restrict: 'E', + scope: { + name: '@', + label: '@', + settings: '=', + classes: '@', + faIcon: '@', + helperText: '@', + min: '@', + required: '<' + }, + templateUrl: '<%= asset_path "admin/settings/number.html" %>', + link ($scope, element, attributes) { + // The setting + $scope.setting = { + name: $scope.name, + value: parseInt($scope.settings[$scope.name], 10) + }; + + /** + * Callback to save the setting value to the database + * @param setting {{value:*, name:string}} note that the value will be stringified + */ + $scope.save = function (setting) { + let value; + if (typeof setting.value === 'number') { + value = setting.value.toString(); + } else { + ({ value } = setting); + } + + Setting.update( + { name: setting.name }, + { value }, + function () { + growl.success(_t('app.admin.settings.customization_of_SETTING_successfully_saved', { SETTING: _t(`app.admin.settings.${setting.name}`) })); + $scope.settings[$scope.name] = value; + }, + function (error) { + if (error.status === 304) return; + + growl.error(_t('app.admin.settings.an_error_occurred_saving_the_setting')); + console.log(error); + } + ); + }; + } + }); + } +]); diff --git a/app/assets/templates/admin/settings/number.html b/app/assets/templates/admin/settings/number.html new file mode 100644 index 000000000..e616d0e6a --- /dev/null +++ b/app/assets/templates/admin/settings/number.html @@ -0,0 +1,15 @@ +
+ +
+
+
+ +
+ +
+ + {{ helperText | translate }} + +
+ +
diff --git a/app/assets/templates/admin/settings/reservations.html b/app/assets/templates/admin/settings/reservations.html index b122f19d7..d4bc1693c 100644 --- a/app/assets/templates/admin/settings/reservations.html +++ b/app/assets/templates/admin/settings/reservations.html @@ -23,66 +23,50 @@

{{ 'app.admin.settings.max_visibility' }}

-
- -
-
-
- -
- -
-
- -
-
- -
-
-
- -
- -
-
- -
+ + + +

{{ 'app.admin.settings.ability_for_the_users_to_move_their_reservations' }}

-
- -
-
-
- -
- -
-
- -
+ +

{{ 'app.admin.settings.ability_for_the_users_to_cancel_their_reservations' }}

-
- -
-
-
- -
- -
-
- -
+ +

{{ 'app.admin.settings.book_overlapping_slots_info' }}

@@ -102,21 +86,14 @@
-
- -
-
-
- -
- -
- - {{ 'app.admin.settings.default_value_is_24_hours' | translate }} - -
- -
+ +