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

Update with new superdesk content profile API #2089

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Changes from 2 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
60 changes: 37 additions & 23 deletions client/controllers/AddToPlanningController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {WORKSPACE, MODALS, MAIN} from '../constants';
import {GET_LABEL_MAP} from 'superdesk-core/scripts/apps/workspace/content/constants';
import {IArticle, IContentProfile} from 'superdesk-api';
import {planningApi, superdeskApi} from '../superdeskApi';
import {PLANNING_VIEW} from 'interfaces';

const DEFAULT_PLANNING_SCHEMA = {
anpa_category: {required: true},
Expand All @@ -20,6 +21,24 @@ const DEFAULT_PLANNING_SCHEMA = {
};

export class AddToPlanningController {
$scope: any;
notify: typeof superdeskApi['ui']['notify'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not correct to specify this type. You should not make assumptions on what superdesk API is using under the hood as implementation. It should be possible to easily change it without breaking anything.

gettext: (
value: string,
params?: {[placeholder: string]: string | number | React.ComponentType},
) => string;
api: any;
lock: any;
session: any;
userList: any;
$timeout: any;
superdeskFlags: any;
$element: any;
store: any;
newsItem: any;
item: any;
rendered: boolean;

constructor(
$element,
$scope,
Expand Down Expand Up @@ -51,17 +70,17 @@ export class AddToPlanningController {

this.store = null;
this.newsItem = null;
this.item = get($scope, 'locals.data.item', {});
this.item = $scope?.locals?.data?.item ?? {};
this.rendered = false;

if (get(this.item, 'archive_item')) {
if (this.item?.archive_item) {
this.item = this.item.archive_item;
}

$scope.$on('$destroy', this.onDestroy);
$scope.$on('item:unlock', this.onItemUnlock);

if (get(this.item, 'archive_item')) {
if (this.item?.archive_item) {
this.item = this.item.archive_item;
}

Expand Down Expand Up @@ -111,7 +130,7 @@ export class AddToPlanningController {
registerNotifications(this.$scope, this.store);

return Promise.all([
this.store.dispatch(actions.main.filter(MAIN.FILTERS.PLANNING)),
this.store.dispatch(actions.main.filter(PLANNING_VIEW.PLANNING)),
planningApi.locks.loadLockedItems(),
this.store.dispatch(actions.fetchAgendas()),
]);
Expand Down Expand Up @@ -140,7 +159,7 @@ export class AddToPlanningController {
}

// update the scope item.
if (this.item && get(this.newsItem, 'assignment_id')) {
if (this.item && this.newsItem?.assignment_id) {
this.item.assignment_id = this.newsItem.assignment_id;
}

Expand All @@ -151,8 +170,9 @@ export class AddToPlanningController {
}

// Only unlock the item if it was locked when launching this modal
if (get(this.newsItem, 'lock_session', null) !== null &&
get(this.newsItem, 'lock_action', 'edit') === 'add_to_planning') {
if ((this.newsItem?.lock_session ?? null) !== null
&& (this.newsItem.lock_action ?? 'edit') === 'add_to_planning'
) {
this.lock.unlock(this.newsItem);
}

Expand Down Expand Up @@ -197,20 +217,14 @@ export class AddToPlanningController {
contentProfile: IContentProfile
}> {
return this.api.find('archive', this.item._id)
.then((newsItem: IArticle) => (
superdeskApi.entities.contentProfile.get(newsItem.profile)
.then((contentProfile) => ({
newsItem,
contentProfile,
}))
.catch((error) => {
this.notify.error(
getErrorMessage(error, this.gettext('Failed to load content profile.'))
);
this.$scope.resolve(error);
return Promise.reject(error);
})
), (error) => {
.then((newsItem: IArticle) => {
const contentProfile = superdeskApi.entities.contentProfile.get(newsItem.profile);

return Promise.resolve({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for Promise.resolve here. You should return the object directly. Since it's inside a .then it's a promise already

newsItem,
contentProfile
});
}, (error) => {
this.notify.error(
getErrorMessage(error, this.gettext('Failed to load the item.'))
);
Expand All @@ -227,9 +241,9 @@ export class AddToPlanningController {
const planningSchema = profile.schema || DEFAULT_PLANNING_SCHEMA;
const requiredError = (field) => this.gettext('[{{ field }}] is a required field')
.replace('{{ field }}', field);
const labels = GET_LABEL_MAP(this.gettext);
const labels = GET_LABEL_MAP();

if (get(newsItem, 'assignment_id')) {
if (newsItem?.assignment_id) {
errMessages.push(this.gettext('Item already linked to a Planning item'));
}

Expand Down
Loading