update notifications UI for new default messages structure

This commit is contained in:
Keith Grant
2019-08-13 09:13:03 -07:00
committed by Jim Ladd
parent 150de6a70b
commit 965dc79a0a
3 changed files with 103 additions and 48 deletions

View File

@@ -32,7 +32,7 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
$state.go("^"); $state.go("^");
Alert('Permission Error', 'You do not have permission to add a notification template.', 'alert-info'); Alert('Permission Error', 'You do not have permission to add a notification template.', 'alert-info');
} }
defaultMessages = data.actions.POST.messages.default; defaultMessages = data.actions.GET.messages;
MessageUtils.setMessagesOnScope($scope, null, defaultMessages); MessageUtils.setMessagesOnScope($scope, null, defaultMessages);
}); });
// apply form definition's default field values // apply form definition's default field values
@@ -168,8 +168,13 @@ export default ['Rest', 'Wait', 'NotificationsFormObject',
$scope.toggleForm = function(key) { $scope.toggleForm = function(key) {
$scope[key] = !$scope[key]; $scope[key] = !$scope[key];
}; };
$scope.$watch('notification_type', (value) => { $scope.$watch('notification_type', (newValue, oldValue = {}) => {
if (value) { if (newValue) {
MessageUtils.updateDefaultsOnScope(
$scope,
defaultMessages[oldValue.value],
defaultMessages[newValue.value]
);
$scope.$broadcast('reset-code-mirror', { $scope.$broadcast('reset-code-mirror', {
customize_messages: $scope.customize_messages, customize_messages: $scope.customize_messages,
}); });

View File

@@ -41,7 +41,7 @@ export default ['Rest', 'Wait',
Rest.setUrl(GetBasePath('notification_templates')); Rest.setUrl(GetBasePath('notification_templates'));
Rest.options() Rest.options()
.then(({data}) => { .then(({data}) => {
defaultMessages = data.actions.GET.messages.default; defaultMessages = data.actions.GET.messages;
}); });
GetChoices({ GetChoices({
@@ -257,8 +257,13 @@ export default ['Rest', 'Wait',
$scope.toggleForm = function(key) { $scope.toggleForm = function(key) {
$scope[key] = !$scope[key]; $scope[key] = !$scope[key];
}; };
$scope.$watch('notification_type', (value) => { $scope.$watch('notification_type', (newValue, oldValue = {}) => {
if (value) { if (newValue) {
MessageUtils.updateDefaultsOnScope(
$scope,
defaultMessages[oldValue.value],
defaultMessages[newValue.value]
);
$scope.$broadcast('reset-code-mirror', { $scope.$broadcast('reset-code-mirror', {
customize_messages: $scope.customize_messages, customize_messages: $scope.customize_messages,
}); });

View File

@@ -1,66 +1,111 @@
export default [function () {
const emptyDefaults = {
started: {
message: '',
body: '',
},
success: {
message: '',
body: '',
},
error: {
message: '',
body: '',
},
}
export default [function() {
return { return {
getMessagesObj: function ($scope, defaultMessages) { getMessagesObj: function ($scope, defaultMessages) {
if (!$scope.customize_messages) { if (!$scope.customize_messages) {
return null; return null;
} }
const defaults = defaultMessages[$scope.notification_type.value] || {};
return { return {
started: { started: {
message: $scope.started_message === defaultMessages.started.message ? message: $scope.started_message === defaults.started.message ?
null : $scope.started_message, null : $scope.started_message,
body: $scope.started_body === defaultMessages.started.body ? body: $scope.started_body === defaults.started.body ?
null : $scope.started_body, null : $scope.started_body,
}, },
success: { success: {
message: $scope.success_message === defaultMessages.success.message ? message: $scope.success_message === defaults.success.message ?
null : $scope.success_message, null : $scope.success_message,
body: $scope.success_body === defaultMessages.success.body ? body: $scope.success_body === defaults.success.body ?
null : $scope.success_body, null : $scope.success_body,
}, },
error: { error: {
message: $scope.error_message === defaultMessages.error.message ? message: $scope.error_message === defaults.error.message ?
null : $scope.error_message, null : $scope.error_message,
body: $scope.error_body === defaultMessages.error.body ? body: $scope.error_body === defaults.error.body ?
null : $scope.error_body, null : $scope.error_body,
} }
}; };
}, },
setMessagesOnScope: function ($scope, messages, defaultMessages) { setMessagesOnScope: function ($scope, messages, defaultMessages) {
$scope.started_message = defaultMessages.started.message; let defaults;
$scope.started_body = defaultMessages.started.body; if ($scope.notification_type) {
$scope.success_message = defaultMessages.success.message; defaults = defaultMessages[$scope.notification_type.value] || emptyDefaults;
$scope.success_body = defaultMessages.success.body; } else {
$scope.error_message = defaultMessages.error.message; defaults = emptyDefaults;
$scope.error_body = defaultMessages.error.body; }
if (!messages) { $scope.started_message = defaults.started.message;
return; $scope.started_body = defaults.started.body;
} $scope.success_message = defaults.success.message;
let isCustomized = false; $scope.success_body = defaults.success.body;
if (messages.started.message) { $scope.error_message = defaults.error.message;
isCustomized = true; $scope.error_body = defaults.error.body;
$scope.started_message = messages.started.message; if (!messages) {
} return;
if (messages.started.body) { }
isCustomized = true; let isCustomized = false;
$scope.started_body = messages.started.body; if (messages.started.message) {
} isCustomized = true;
if (messages.success.message) { $scope.started_message = messages.started.message;
isCustomized = true; }
$scope.success_message = messages.success.message; if (messages.started.body) {
} isCustomized = true;
if (messages.success.body) { $scope.started_body = messages.started.body;
isCustomized = true; }
$scope.success_body = messages.success.body; if (messages.success.message) {
} isCustomized = true;
if (messages.error.message) { $scope.success_message = messages.success.message;
isCustomized = true; }
$scope.error_message = messages.error.message; if (messages.success.body) {
} isCustomized = true;
if (messages.error.body) { $scope.success_body = messages.success.body;
isCustomized = true; }
$scope.error_body = messages.error.body; if (messages.error.message) {
} isCustomized = true;
$scope.customize_messages = isCustomized; $scope.error_message = messages.error.message;
}
if (messages.error.body) {
isCustomized = true;
$scope.error_body = messages.error.body;
}
$scope.customize_messages = isCustomized;
},
updateDefaultsOnScope: function($scope, oldDefaults = emptyDefaults, newDefaults) {
if ($scope.started_message === oldDefaults.started.message) {
$scope.started_message = newDefaults.started.message;
}
if ($scope.started_body === oldDefaults.started.body) {
$scope.started_body = newDefaults.started.body;
}
if ($scope.success_message === oldDefaults.success.message) {
$scope.success_message = newDefaults.success.message;
}
if ($scope.success_body === oldDefaults.success.body) {
$scope.success_body = newDefaults.success.body;
}
if ($scope.error_message === oldDefaults.error.message) {
$scope.error_message = newDefaults.error.message;
}
if ($scope.error_body === oldDefaults.error.body) {
$scope.error_body = newDefaults.error.body;
}
} }
}; };
}]; }];