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(ui): AddToPlanning action fails due to change in SuperdeskAPI #2092

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
66 changes: 40 additions & 26 deletions client/controllers/AddToPlanningController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {ModalsContainer} from '../components';
import {planning} from '../actions';
import {get, isEmpty, isNumber} from 'lodash';
import {registerNotifications, getErrorMessage, isExistingItem} from '../utils';
import {WORKSPACE, MODALS, MAIN} from '../constants';
import {WORKSPACE, MODALS} 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: {error: (message: string) => void};
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 @@ -96,7 +115,7 @@ export class AddToPlanningController {
return Promise.resolve();
}

loadWorkspace(store, workspaceChanged) {
loadWorkspace(store) {
this.store = store;

return this.loadArchiveItem()
Expand All @@ -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 {
newsItem,
contentProfile
};
}, (error) => {
this.notify.error(
getErrorMessage(error, this.gettext('Failed to load the item.'))
);
Expand All @@ -227,15 +241,15 @@ 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'));
}

Object.keys(planningSchema)
.filter((field) => (
contentProfile.schema[field] != null &&
contentProfile.schema?.[field] != null &&
planningSchema[field]?.required === true &&
isEmpty(newsItem[field]) &&
!isNumber(newsItem[field])
Expand Down
Loading