Skip to content

Commit

Permalink
Refactoring more toolbar actions (#4301)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Aug 14, 2023
1 parent 701d6d0 commit a66075e
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 159 deletions.
168 changes: 84 additions & 84 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"sass-loader": "6.0.6",
"shortid": "2.2.8",
"style-loader": "0.20.2",
"superdesk-ui-framework": "^3.0.47",
"superdesk-ui-framework": "^3.0.50",
"ts-loader": "3.5.0",
"tslint": "5.11.0",
"typescript": "4.9.5",
Expand Down
60 changes: 55 additions & 5 deletions scripts/api/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ import {IPublishingDateOptions} from 'core/interactive-article-actions-panel/sub
import {notify} from 'core/notify/notify';
import ng from 'core/services/ng';
import {gettext} from 'core/utils';
import {flatMap, trim} from 'lodash';
import {flatMap, keys, pick, trim, union} from 'lodash';
import {IArticle, IDangerousArticlePatchingOptions, IDesk, IStage, onPublishMiddlewareResult} from 'superdesk-api';
import {duplicateItems} from './article-duplicate';
import {fetchItems, fetchItemsToCurrentDesk} from './article-fetch';
import {patchArticle} from './article-patch';
import {sendItems} from './article-send';
import {authoringApiCommon} from 'apps/authoring-bridge/authoring-api-common';
import {IArticleAction} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
import {CONTENT_FIELDS_DEFAULTS} from 'apps/authoring/authoring/helpers';
import _ from 'lodash';

type IArticleActionType = string | 'publish' | 'edit';
export type IArticleActionType = 'correct' | 'publish' | 'edit' | 'kill' | 'unpublish' | 'takedown';
const isLocked = (_article: IArticle) => _article.lock_session != null;
const isLockedInCurrentSession = (_article: IArticle) => _article.lock_session === ng.get('session').sessionId;
const isLockedInOtherSession = (_article: IArticle) => isLocked(_article) && !isLockedInCurrentSession(_article);
Expand Down Expand Up @@ -104,6 +106,29 @@ function doSpike(item: IArticle) {
});
}

function deschedule(item: IArticle): Promise<void> {
return httpRequestJsonLocal<IArticle>({
method: 'PATCH',
path: `/archive/${item._id}`,
payload: {
publish_schedule: null,
},
headers: {
'If-Match': item._etag,
},
}).then(() => {
const $location = ng.get('$location');

if ($location.search()._id === item._id) {
$location.search('_id', null);
}

if (applicationState.articleInEditMode === item._id) {
ng.get('authoringWorkspace').close();
}
});
}

function doUnspike(item: IArticle, deskId: IDesk['_id'], stageId: IStage['_id']): Promise<void> {
return httpRequestJsonLocal<IArticle>({
method: 'PATCH',
Expand Down Expand Up @@ -279,7 +304,7 @@ function publishItem_legacy(
orig: IArticle,
item: IArticle,
scope: IScope,
action: string | 'publish' | 'edit' = 'publish',
action: IArticleActionType = 'publish',
onError?: (error: IPublishingError) => void,
): Promise<boolean> {
let warnings: Array<{text: string}> = [];
Expand Down Expand Up @@ -398,6 +423,25 @@ function edit(
}
}

function getItemPatchWithKillOrTakedownTemplate(item: IArticle, action: IArticleAction): Promise<IArticle> {
const itemForTemplate = {
template_name: action,
item: pick(item, union(keys(CONTENT_FIELDS_DEFAULTS), ['_id', 'versioncreated', 'task'])),
};

return httpRequestJsonLocal({
method: 'POST',
path: '/content_templates_apply',
payload: itemForTemplate,
}).then((result: IArticle) => {
return {
...result,
...(action === 'kill' && {operation: 'kill'}),
state: ITEM_STATE.PUBLISHED,
};
});
}

/**
* Gets opened items from your workspace.
*/
Expand Down Expand Up @@ -467,6 +511,8 @@ interface IArticleApi {
doSpike(item: IArticle): Promise<void>;
doUnspike(item: IArticle, deskId: IDesk['_id'], stageId: IStage['_id']): Promise<void>;

deschedule(item: IArticle): Promise<void>;

fetchItems(
items: Array<IArticle>,
selectedDestination: ISendToDestinationDesk,
Expand Down Expand Up @@ -500,7 +546,9 @@ interface IArticleApi {
canPublishOnDesk(deskType: string): boolean;
showCloseAndContinue(item: IArticle, dirty: boolean): boolean;
showPublishAndContinue(item: IArticle, dirty: boolean): boolean;
publishItem_legacy(orig: IArticle, item: IArticle, $scope: any, action?: string): Promise<boolean>;
publishItem_legacy(orig: IArticle, item: IArticle, $scope: any, action?: IArticleActionType): Promise<boolean>;

getItemPatchWithKillOrTakedownTemplate(item: IArticle, action: IArticleAction): Promise<IArticle>;

// `openArticle` - a similar function exists, TODO: in the future we'll have to unify these two somehow
edit(
Expand All @@ -518,7 +566,7 @@ interface IArticleApi {
publishItem(
orig: IArticle,
item: IArticle,
action?: string,
action?: IArticleActionType,

// onError is optional in this function and in `publishItem_legacy` since when you're calling
// it from React you want to pass only it to handle certain errors and apply them to the scope
Expand Down Expand Up @@ -562,4 +610,6 @@ export const article: IArticleApi = {
publishItem_legacy,
publishItem,
edit,
deschedule,
getItemPatchWithKillOrTakedownTemplate,
};
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
rewritten_by,
} = article;

const {onArticleChange} = this.props;
const {onItemChange} = this.props;

const allVocabularies = sdApi.vocabularies.getAll();

Expand All @@ -112,7 +112,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
<Switch
label={{content: ''}} // TODO: Implement accessibility
onChange={() => {
onArticleChange({
onItemChange({
...article,
flags: {
...flags,
Expand All @@ -133,7 +133,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
<Switch
label={{content: ''}} // TODO: Implement accessibility
onChange={() => {
onArticleChange({
onItemChange({
...article,
flags: {...flags, marked_for_legal: !flags.marked_for_legal},
});
Expand All @@ -150,7 +150,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
type="text"
value={usageterms}
onChange={(value) => {
onArticleChange({
onItemChange({
...article,
usageterms: value,
});
Expand All @@ -164,7 +164,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
label={gettext('Language').toUpperCase()}
value={language}
onChange={(val) => {
onArticleChange({
onItemChange({
...article,
language: val,
});
Expand Down Expand Up @@ -429,7 +429,7 @@ class MetadataWidget extends React.PureComponent<IProps, IState> {
type="text"
value={unique_name}
onChange={(value) => {
onArticleChange({
onItemChange({
...article,
unique_name: value,
});
Expand Down
Loading

0 comments on commit a66075e

Please sign in to comment.