Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix target subscribers loading in templates #4358

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions scripts/apps/authoring/metadata/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {IArticle, ISubject, IVocabularyItem} from 'superdesk-api';
import {reactToAngular1} from 'superdesk-ui-framework';
import {MetaDataDropdownSingleSelectReact} from './views/MetaDataDropdownSingleSelectReact';
import {sdApi} from 'api';
import {PublishingTargetSelect} from 'core/interactive-article-actions-panel/subcomponents/publishing-target-select';

MetadataCtrl.$inject = [
'$scope', 'desks', 'metadata', 'privileges', 'datetimeHelper', 'userList',
Expand Down Expand Up @@ -1436,6 +1437,16 @@ angular.module('superdesk.apps.authoring.metadata', [
.directive('sdMetaTerms', MetaTermsDirective)
.directive('sdMetaTags', MetaTagsDirective)
.directive('sdMetaDropdown', MetaDropdownDirective)
.component(
'sdTargetSubscribers',
reactToAngular1(
PublishingTargetSelect,
[
'value',
'onChange',
],
),
)
.component(
'sdMetaDataDropdownSingleSelectReact',
reactToAngular1(
Expand Down
7 changes: 7 additions & 0 deletions scripts/apps/templates/directives/TemplateEditorModal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import {ITemplate} from 'superdesk-api';

export function TemplateEditorModal() {
return {
Expand All @@ -19,6 +20,12 @@ export function TemplateEditorModal() {
scope.$apply();
};

scope.onChangeData = (item: ITemplate['data']) => {
scope.template.data = item;

scope.setDirtyFromReact();
};

scope.isDirty = (templateForm, metadataForm) => templateForm.$dirty || metadataForm.$dirty || _isDirty;

scope.templatesFilter = function(templateType) {
Expand Down
26 changes: 1 addition & 25 deletions scripts/apps/templates/views/template-editor-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,7 @@ <h3 class="modal__heading" ng-hide="template._id" translate>Add New Template</h3

<div class="form__row">
<label class="form-label" translate>Target Subscribers</label>
<div class="term-editor"
sd-meta-terms
data-item="item"
data-unique="_id"
data-list="metadata.customSubscribers"
data-field="target_subscribers"
data-reload-list="true"
data-header="true"
></div>
</div>

<div class="form__row">
<label class="form-label" translate>Target Regions</label>
<div sd-meta-target
data-list="metadata.regions"
data-targets="item.target_regions"
></div>
</div>

<div class="form__row">
<label class="form-label" translate>Target Types</label>
<div sd-meta-target
data-list="metadata.subscriberTypes"
data-targets="item.target_types"
></div>
<sd-target-subscribers on-change="onChangeData" value="template.data"></sd-target-subscribers>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface IValue {
}

interface IProps {
zIndex?: number;
required?: boolean;
vocabularies: Array<{qcode: string; name: string}>;
value: Array<IValue>;
Expand Down Expand Up @@ -53,6 +54,7 @@ export class ControlledVocabulariesSelect extends React.PureComponent<IProps, IS
type={this.state.allow ? 'default' : 'primary'}
/>
<SelectFilterable
zIndex={this.props.zIndex}
items={itemsExcludingSelected}
value={this.state.vocabulary}
getLabel={(vocabulary) => vocabulary?.name ?? ''}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,69 @@ interface IProps {
onChange(value: IPublishingTarget): void;
}

export class PublishingTargetSelect extends React.PureComponent<IProps> {
render() {
const metadata = ng.get('metadata');
const subscribers: Array<{_id: string; name: string}> = metadata.values.customSubscribers;
const regions: Array<IRegion> = metadata.values.geographical_restrictions ?? [];
const subscriberTypes: Array<ISubscriberType> = metadata.values.subscriberTypes;
interface IState {
loading: boolean;
subscribers: Array<{_id: string; name: string}>;
regions: Array<IRegion>;
subscriberTypes: Array<ISubscriberType>;
}

export class PublishingTargetSelect extends React.PureComponent<IProps, IState> {
constructor(props: IProps) {
super(props);

this.state = {
loading: true,
regions: [],
subscribers: [],
subscriberTypes: [],
};

this.setMetadataValues = this.setMetadataValues.bind(this);
}

setMetadataValues(metadataService: any) {
this.setState({
loading: false,
subscribers: metadataService.values.customSubscribers ?? [],
regions: metadataService.values.geographical_restrictions ?? [],
subscriberTypes: metadataService.values.subscriberTypes ?? [],
});
}

componentDidMount(): void {
const metadataService = ng.get('metadata');

return (
if (metadataService.values.customSubscribers != null) {
this.setMetadataValues(metadataService);
} else {
metadataService.fetchSubscribers().then(() => {
this.setMetadataValues(metadataService);
});
}
}

render() {
return this.state.loading === false && (
<ToggleBox title={gettext('Target')} initiallyOpen>
<FormLabel text={gettext('Target subscribers')} />

<div style={{paddingTop: 5}}>
<TreeSelect
zIndex={2000}
label=""
inlineLabel
labelHidden
kind="synchronous"
allowMultiple
getId={(item) => item._id}
getLabel={(item) => item.name}
getOptions={() => subscribers.map((x) => ({value: x}))}
getOptions={() => this.state.subscribers.map((x) => ({value: x}))}
value={this.props.value.target_subscribers}
onChange={(val) => {
this.props.onChange({
...this.props.value,
target_subscribers: subscribers
target_subscribers: this.state.subscribers
.filter(({_id}) => val.map((sub) => sub._id).includes(_id)),
});
}}
Expand All @@ -96,8 +133,9 @@ export class PublishingTargetSelect extends React.PureComponent<IProps> {

<div style={{paddingTop: 5}}>
<ControlledVocabulariesSelect
vocabularies={regions}
value={this.props.value.target_regions}
zIndex={2000}
vocabularies={this.state.regions}
value={this.props.value.target_regions ?? []}
onChange={(val) => {
this.props.onChange({
...this.props.value,
Expand All @@ -113,8 +151,9 @@ export class PublishingTargetSelect extends React.PureComponent<IProps> {

<div style={{paddingTop: 5}}>
<ControlledVocabulariesSelect
vocabularies={subscriberTypes}
value={this.props.value.target_types}
zIndex={2000}
vocabularies={this.state.subscriberTypes}
value={this.props.value.target_types ?? []}
onChange={(val) => {
this.props.onChange({
...this.props.value,
Expand Down
Loading