Skip to content

Commit

Permalink
move onDuplicateCoverage inside the field component
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaskikutis committed Dec 9, 2024
1 parent 8bc430c commit 248c01b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 44 deletions.
1 change: 0 additions & 1 deletion client/components/Coverages/CoverageArrayInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ interface IProps {
setCoverageDefaultDesk(coverage: IPlanningCoverageItem): void;
setCoverageAddAdvancedMode(enabled: boolean): Promise<void>;
createUploadLink(file: IFile): void;
onDuplicateCoverage(coverage: IPlanningCoverageItem, duplicateAs: IG2ContentType['qcode']): void;
onCancelCoverage(
coverage: IPlanningCoverageItem,
index: number,
Expand Down
99 changes: 74 additions & 25 deletions client/components/Coverages/CoverageEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ICoverageProvider,
IG2ContentType,
IGenre,
IPlanningAppState,
IPlanningCoverageItem,
IPlanningNewsCoverageStatus
} from '../../../interfaces';
Expand All @@ -22,6 +23,8 @@ import {planningUtils, gettext, editorMenuUtils} from '../../../utils';
import {getVocabularyItemFieldTranslated} from '../../../utils/vocabularies';
import {getUserInterfaceLanguageFromCV} from '../../../utils/users';
import {COVERAGES} from '../../../constants';
import {getRelatedEventIdsForPlanning} from '../../../utils/planning';
import {planningApi} from '../../../superdeskApi';

interface IProps {
testId?: string;
Expand All @@ -38,7 +41,7 @@ interface IProps {
readOnly: boolean;
message: any;
item: any;
diff: any;
diff: any; // planning item
formProfile: any;
errors: {[key: string]: any};
showErrors: boolean;
Expand All @@ -53,7 +56,6 @@ interface IProps {

onChange(field: string, value: any): void;
remove(): void;
onDuplicateCoverage(coverage: DeepPartial<IPlanningCoverageItem>, duplicateAs?: IG2ContentType['qcode']): void;
onCancelCoverage?(): void;
onAddCoverageToWorkflow?(): void;
onRemoveAssignment?(coverage: IPlanningCoverageItem): void;
Expand All @@ -63,6 +65,36 @@ interface IProps {
onPopupClose(): void;
}

function duplicateCoverage(
{
planning,
coverage,
duplicateAs,
} : {
planning: IPlanningItem;
coverage: IPlanningCoverageItem;
duplicateAs?: IG2ContentType['qcode'];
}
): Array<DeepPartial<IPlanningCoverageItem>> {
const state: IPlanningAppState = planningApi.redux.store.getState();

// TAG: MULTIPLE_PRIMARY_EVENTS
const relatedEventId = getRelatedEventIdsForPlanning(planning, 'primary')[0];

const relatedEvent: IEventItem | undefined = relatedEventId == null
? undefined
: state.events.events[relatedEventId];

const coverages = planningUtils.duplicateCoverage(
planning,
coverage,
duplicateAs,
relatedEvent
);

return coverages;
}

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

Expand Down Expand Up @@ -92,7 +124,6 @@ export class CoverageEditor extends React.PureComponent<IProps> {
coverageProviders,
priorities,
keywords,
onDuplicateCoverage,
onCancelCoverage,
onAddCoverageToWorkflow,
onRemoveAssignment,
Expand All @@ -116,29 +147,47 @@ export class CoverageEditor extends React.PureComponent<IProps> {

if (!readOnly && !addNewsItemToPlanning) {
const language = value.planning?.language ?? getUserInterfaceLanguageFromCV();
const duplicateActions = contentTypes
.filter((contentType) => (
contentType.qcode !== get(value, 'planning.g2_content_type')
))
.map((contentType) => ({
label: getVocabularyItemFieldTranslated(
contentType,
'name',
language
),
callback: onDuplicateCoverage.bind(null, value, contentType.qcode),
}));

itemActions = [{
label: gettext('Duplicate'),
icon: 'icon-copy',
callback: onDuplicateCoverage.bind(null, value),
},
{
label: gettext('Duplicate As'),
icon: 'icon-copy',
callback: duplicateActions,
}];
itemActions = [
{
label: gettext('Duplicate'),
icon: 'icon-copy',
callback: () => {
this.props.onChange(
'coverages',
duplicateCoverage({
planning: diff,
coverage: value,
}),
);
},
},
{
label: gettext('Duplicate As'),
icon: 'icon-copy',
callback: contentTypes
.filter((contentType) => (
contentType.qcode !== get(value, 'planning.g2_content_type')
))
.map((contentType) => ({
label: getVocabularyItemFieldTranslated(
contentType,
'name',
language
),
callback: () => {
this.props.onChange(
'coverages',
duplicateCoverage({
planning: diff,
coverage: value,
duplicateAs: contentType.qcode,
}),
);
},
})),
},
];

if (onCancelCoverage != null && planningUtils.canCancelCoverage(value, diff)) {
itemActions.push({
Expand Down
13 changes: 0 additions & 13 deletions client/components/Planning/PlanningEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
this.onCoverageChange = this.onCoverageChange.bind(this);
this.onPlanningDateChange = this.onPlanningDateChange.bind(this);
this.onTimeToBeConfirmed = this.onTimeToBeConfirmed.bind(this);
this.onDuplicateCoverage = this.onDuplicateCoverage.bind(this);
this.onCancelCoverage = this.onCancelCoverage.bind(this);
this.onAddCoverageToWorkflow = this.onAddCoverageToWorkflow.bind(this);
this.onAddScheduledUpdateToWorkflow = this.onAddScheduledUpdateToWorkflow.bind(this);
Expand Down Expand Up @@ -261,17 +260,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
}
}

onDuplicateCoverage(coverage: IPlanningCoverageItem, duplicateAs: IG2ContentType['qcode']) {
const coverages = planningUtils.duplicateCoverage(
this.props.diff,
coverage,
duplicateAs,
this.props.event
);

this.props.onChangeHandler('coverages', coverages);
}

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

Expand Down Expand Up @@ -519,7 +507,6 @@ class PlanningEditorComponent extends React.Component<IProps, IState> {
preferredCoverageDesks: this.props.preferredCoverageDesks,
setCoverageDefaultDesk: this.props.setCoverageDefaultDesk,
setCoverageAddAdvancedMode: this.props.setCoverageAddAdvancedMode,
onDuplicateCoverage: this.onDuplicateCoverage,
onCancelCoverage: this.onCancelCoverage,
onAddCoverageToWorkflow: this.onAddCoverageToWorkflow,
onAddScheduledUpdateToWorkflow: this.onAddScheduledUpdateToWorkflow,
Expand Down
1 change: 0 additions & 1 deletion client/components/fields/editor/Coverages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface IProps extends IEditorFieldProps {
onPopupClose?(): void;
setCoverageDefaultDesk(coverage: IPlanningCoverageItem): void;
setCoverageAddAdvancedMode(enabled: boolean): Promise<void>;
onDuplicateCoverage(coverage: IPlanningCoverageItem, duplicateAs: IG2ContentType['qcode']): void;
onCancelCoverage(
coverage: IPlanningCoverageItem,
index: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class RelatedCoverageItemsComponent extends React.PureComponent<IProps> {

onChange={this.props.updateCoverage}
remove={() => this.props.removeCoverage(coverage)}
onDuplicateCoverage={this.props.duplicateCoverage}
setCoverageDefaultDesk={() => this.props.setCoverageDefaultDesk(coverage)}
onAddCoverageToWorkflow={() => this.props.onAddCoverageToWorkflow(coverage, index)}
openCoverageIds={[]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ export class Editor extends React.PureComponent<IProps> {
return (
<Container>
<EditorFieldCoverages
item={{coverages: this.props.value}}
item={{
// coverages are the main value
coverages: this.props.value,

// related_events are used if available to prefill coverage fields when adding a new coverage
related_events: this.props.item.related_events,
}}
// PR-TODO: implement functions below
onDuplicateCoverage={noop}
onCancelCoverage={noop}
onAddCoverageToWorkflow={noop}
onRemoveAssignment={noop}
setCoverageDefaultDesk={noop}
onChange={(fieldPath, value) => { // 'coverages[0].planning.slugline', 'planning 1 coverage12'
onChange={(fieldPath: any, value: any) => {
/**
* sample of arguments:
* fieldPath - 'coverages[0].planning.slugline'
* value - 'slugline 123'
*/

const item = cloneDeep({coverages: this.props.value});
const nextValue = set(item, fieldPath, value);

Expand Down

0 comments on commit 248c01b

Please sign in to comment.