Skip to content

Commit

Permalink
fetch available output formats from new api (#4096)
Browse files Browse the repository at this point in the history
* fetch available output formats from new api

SDESK-6212

* fix empty subscriber types dropdown
  • Loading branch information
petrjasek authored May 24, 2022
1 parent 2e72474 commit 090300f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
52 changes: 15 additions & 37 deletions scripts/apps/publish/directives/SubscribersDirective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {appConfig} from 'appConfig';
* @requires subscribersService
* @requires adminPublishSettingsService
* @requires modal
* @requires metadata
* @requires contentFilters
* @requires $q
* @requires $filter
Expand All @@ -22,12 +21,12 @@ import {appConfig} from 'appConfig';
*/
SubscribersDirective.$inject = [
'notify', 'api', 'subscribersService', 'adminPublishSettingsService', 'modal',
'metadata', 'contentFilters', '$q', '$filter', 'products', '$rootScope',
'contentFilters', '$q', '$filter', 'products', '$rootScope',
];

export function SubscribersDirective(
notify, api, subscribersService, adminPublishSettingsService,
modal, metadata, contentFilters, $q, $filter, products, $rootScope,
modal, contentFilters, $q, $filter, products, $rootScope,
) {
return {
scope: {
Expand All @@ -43,23 +42,28 @@ export function SubscribersDirective(
$scope.contentFilters = null;
$scope.apiProducts = null;
$scope.directProducts = null;
$scope.subTypes = null;
$scope.search = {};
$scope.highPriorityQueueEnabled = appConfig.high_priority_queue_enabled;

if (angular.isDefined(metadata.values.subscriber_types)) {
$scope.subTypes = metadata.values.subscriber_types;
} else {
metadata.fetchMetadataValues().then(() => {
$scope.subTypes = metadata.values.subscriber_types;
});
}
api.query('output_formats').then((result) => {
$scope.formats = result._items.map((format) => ({
name: format.name,
qcode: format.type,
}));
});

$scope.statusFilters = [
{label: gettext('Active'), value: true, id: 'active'},
{label: gettext('Both'), value: null, id: 'both'},
{label: gettext('Inactive'), value: false, id: 'inactive'},
];

$scope.subTypes = [
{name: gettext('All'), qcode: 'all'},
{name: gettext('Digital/Internet'), qcode: 'digital'},
{name: gettext('Wire/Paper'), qcode: 'wire'},
];

$scope.search.subscriber_status = $scope.statusFilters[0];
/**
* Fetches all subscribers from backend
Expand Down Expand Up @@ -236,7 +240,6 @@ export function SubscribersDirective(
}

$scope.subscriberType = $scope.subscriber.subscriber_type || '';
$scope.changeFormats($scope.subscriberType);
initGlobalFilters();
}, () => {
notify.error(gettext('Subscriber could not be initialized!'));
Expand Down Expand Up @@ -277,31 +280,6 @@ export function SubscribersDirective(

$rootScope.$on('subcriber: saveEnabled', () => $scope.saveEnabled = true);

/**
* Invoked when Subscriber Type is changed. Responsible for populating $scope.formats variable.
* The $scope.formats variable is used to display format field in destination. The new value is changed.
*/
$scope.changeFormats = function(newSubscriberType) {
var formats = _.result(_.find($scope.subTypes, {qcode: newSubscriberType}), 'formats');

if ($scope.destinations.length > 0 && $scope.subscriberType !== '' &&
$scope.subscriberType !== newSubscriberType) {
var oldFormats = _.result(_.find($scope.subTypes, {qcode: $scope.subscriberType}), 'formats');

if (!_.isEqual(oldFormats, formats)) {
notify.error(gettext('Error: Please re-assign new format for each destination as the changed ' +
'subscriber type has formats which are not supported by existing destination(s).'));

_.each($scope.destinations, (destination) => {
destination.format = null;
});
}
}

$scope.subscriberType = $scope.subscriber.subscriber_type;
$scope.formats = formats;
};

// If subscribers list provided don't fetch subscribers
if (!$scope.subscribersList) {
fetchSubscribers();
Expand Down
2 changes: 1 addition & 1 deletion scripts/apps/publish/views/subscribers.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ <h3 class="modal__heading" translate ng-hide="subscriber._id">Add New Subscriber
id="subType"
ng-model="subscriber.subscriber_type"
ng-options="s.qcode as s.name for s in subTypes"
ng-change="changeFormats(subscriber.subscriber_type)" required>
required>
<option value=""></option>
</select>
</div>
Expand Down

0 comments on commit 090300f

Please sign in to comment.