Skip to content

Commit

Permalink
move logic from "onCoverageChange" to coverage editor so it also appl…
Browse files Browse the repository at this point in the history
…ies to embedded plannings
  • Loading branch information
tomaskikutis committed Dec 24, 2024
1 parent 45d6be9 commit 2f0a45f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 59 deletions.
91 changes: 84 additions & 7 deletions client/components/Coverages/CoverageEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {get} from 'lodash';
import {connect} from 'react-redux';

import {
EDITOR_TYPE,
Expand All @@ -25,8 +26,9 @@ import {getUserInterfaceLanguageFromCV} from '../../../utils/users';
import {getRelatedEventIdsForPlanning} from '../../../utils/planning';
import {planningApi} from '../../../superdeskApi';
import {planningApis} from '../../../api';
import * as selectors from '../../../selectors';

interface IProps {
interface IOwnProps {
testId?: string;
field: string;
value: IPlanningCoverageItem;
Expand Down Expand Up @@ -69,6 +71,12 @@ interface IProps {
onPopupClose(): void;
}

interface IReduxStateProps {
defaultDesk: IDesk | undefined;
}

type IProps = IOwnProps & IReduxStateProps;

function duplicateCoverage(
{
planning,
Expand Down Expand Up @@ -99,19 +107,75 @@ function duplicateCoverage(
return coverages;
}

export class CoverageEditor extends React.PureComponent<IProps> {
function assignCoverageToDefaultDesk(coverage: DeepPartial<IPlanningCoverageItem>, defaultDesk: IDesk) {
if (!Object.keys(coverage.assigned_to ?? {}).length) {
coverage.assigned_to = {desk: defaultDesk._id};
} else {
// TODO: Fix IDesk['members'] type in client-core
// @ts-ignore
const deskMembers = (defaultDesk?.members ?? []).map((m) => m.user);

coverage.assigned_to.desk = defaultDesk._id;

// If the user does not belong to default desk, remove the user
if (coverage.assigned_to.user && !deskMembers.includes(coverage.assigned_to.user)) {
coverage.assigned_to.user = null;
}
}
}

export class CoverageEditorComponent extends React.PureComponent<IProps> {
collapseBox: React.RefObject<CollapseBox>;

constructor(props) {
super(props);

this.collapseBox = React.createRef<CollapseBox>();
this.onChange = this.onChange.bind(this);
}

scrollInView() {
this.collapseBox.current?.scrollInView(true);
}

onChange(field, value) {
let valueToUpdate = value;

if (field.match(/^coverages\[/)) {
const {newsCoverageStatus} = this.props;
const coverage = value;

// If there is an assignment and coverage status not planned,
// change it to 'planned'
if (newsCoverageStatus.length > 0 &&
coverage?.news_coverage_status?.qcode !== newsCoverageStatus[0].qcode &&
coverage?.assigned_to?.desk != null
) {
valueToUpdate = {
...coverage,
news_coverage_status: this.props.newsCoverageStatus[0],
};
}

if (field.match(/g2_content_type$/) &&
value === 'text' &&
this.props.defaultDesk?._id != null
) {
const coverageStr = field.substr(0, field.indexOf('.'));
let existingCoverage: IPlanningCoverageItem = {...get(this.props, `diff.${coverageStr}`)};

if (existingCoverage?.assigned_to?.desk !== this.props.defaultDesk._id) {
existingCoverage.planning.g2_content_type = value;
assignCoverageToDefaultDesk(existingCoverage, this.props.defaultDesk);
this.props.onChange(coverageStr, existingCoverage);
return;
}
}
}

this.props.onChange(field, valueToUpdate);
}

render() {
const {
diff,
Expand All @@ -124,7 +188,6 @@ export class CoverageEditor extends React.PureComponent<IProps> {
contentTypes,
genres,
newsCoverageStatus,
onChange,
coverageProviders,
priorities,
keywords,
Expand All @@ -139,9 +202,12 @@ export class CoverageEditor extends React.PureComponent<IProps> {
openCoverageIds,
testId,
includeScheduledUpdates,
onChange: __unused, // destructure to avoid it being present in "...props"
...props
} = this.props;

const {onChange} = this;

// Coverage item actions
let itemActions = [];

Expand All @@ -156,7 +222,7 @@ export class CoverageEditor extends React.PureComponent<IProps> {
label: gettext('Duplicate'),
icon: 'icon-copy',
callback: () => {
this.props.onChange(
onChange(
fieldOfArray,
duplicateCoverage({
planning: diff,
Expand All @@ -179,7 +245,7 @@ export class CoverageEditor extends React.PureComponent<IProps> {
language
),
callback: () => {
this.props.onChange(
onChange(
fieldOfArray,
duplicateCoverage({
planning: diff,
Expand All @@ -199,7 +265,7 @@ export class CoverageEditor extends React.PureComponent<IProps> {
callback: () => {
planningApis.coverages.cancelCoverage(this.props.coverages, value)
.then((nextCoverages) => {
this.props.onChange(fieldOfArray, nextCoverages);
onChange(fieldOfArray, nextCoverages);
});
},
});
Expand All @@ -211,7 +277,7 @@ export class CoverageEditor extends React.PureComponent<IProps> {
label: gettext('Add to workflow'),
icon: 'icon-assign',
callback: () => {
this.props.onChange(
onChange(
fieldOfArray,
planningApis.planning.coverages.addCoverageToWorkflow(this.props.coverages, value)
);
Expand Down Expand Up @@ -325,3 +391,14 @@ export class CoverageEditor extends React.PureComponent<IProps> {
);
}
}

function mapStateToProps(state: IPlanningAppState): IReduxStateProps {
return {
defaultDesk: selectors.general.defaultDesk(state),
};
}


export const CoverageEditor = connect(
mapStateToProps,
)(CoverageEditorComponent);
53 changes: 1 addition & 52 deletions client/components/Planning/PlanningEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,41 +249,7 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
}

onCoverageChange(field: string, value: any, planningFormEdited: boolean = true) {
let valueToUpdate = value;

if (field.match(/^coverages\[/)) {
const {newsCoverageStatus} = this.props;
const coverage = value as IPlanningCoverageItem;

// If there is an assignment and coverage status not planned,
// change it to 'planned'
if (newsCoverageStatus.length > 0 &&
coverage?.news_coverage_status?.qcode !== newsCoverageStatus[0].qcode &&
coverage?.assigned_to?.desk != null
) {
valueToUpdate = {
...coverage,
news_coverage_status: this.props.newsCoverageStatus[0],
};
}

if (field.match(/g2_content_type$/) &&
value === 'text' &&
this.props.defaultDesk?._id != null
) {
const coverageStr = field.substr(0, field.indexOf('.'));
let existingCoverage = {...get(this.props, `diff.${coverageStr}`)};

if (get(existingCoverage, 'assigned_to.desk') !== this.props.defaultDesk._id) {
existingCoverage.planning.g2_content_type = value;
this.assignCoverageToDefaultDesk(existingCoverage);
this.props.onChangeHandler(coverageStr, existingCoverage);
return;
}
}
}

this.props.onChangeHandler(field, valueToUpdate, planningFormEdited);
this.props.onChangeHandler(field, value, planningFormEdited);

if (field === 'coverages') {
// Flush the autosave so the Redux Store get's updated with the
Expand All @@ -307,23 +273,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
this.props.onChangeHandler('_time_to_be_confirmed', true);
}

assignCoverageToDefaultDesk(coverage: DeepPartial<IPlanningCoverageItem>) {
if (!Object.keys(coverage.assigned_to ?? {}).length) {
coverage.assigned_to = {desk: this.props.defaultDesk._id};
} else {
// TODO: Fix IDesk['members'] type in client-core
// @ts-ignore
const deskMembers = (this.props.defaultDesk?.members ?? []).map((m) => m.user);

coverage.assigned_to.desk = this.props.defaultDesk._id;

// If the user does not belong to default desk, remove the user
if (coverage.assigned_to.user && !deskMembers.includes(coverage.assigned_to.user)) {
coverage.assigned_to.user = null;
}
}
}

renderHeader() {
return !this.props.itemExists ? null : (
<PlanningEditorHeader
Expand Down

0 comments on commit 2f0a45f

Please sign in to comment.