Skip to content

Commit

Permalink
Planning copying translations v2 (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaskikutis authored Aug 3, 2023
1 parent 6b649c8 commit 3cf99a9
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 157 deletions.
89 changes: 39 additions & 50 deletions client/actions/agenda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {Moment} from 'moment';
import {IEventItem, IPlanningItem, IAgenda} from '../interfaces';

import {AGENDA, MODALS, EVENTS} from '../constants';
import {getErrorMessage, gettext, planningUtils, stringUtils} from '../utils';
import {getErrorMessage, gettext, planningUtils} from '../utils';
import {planning, showModal, main} from './index';
import {convertStringFields} from '../utils/strings';

const openAgenda = () => (
(dispatch) => (
Expand Down Expand Up @@ -238,6 +239,39 @@ const addEventToCurrentAgenda = (
}
);

export function convertEventToPlanningItem(event: IEventItem): Partial<IPlanningItem> {
let newPlanningItem: Partial<IPlanningItem> = {
type: 'planning',
event_item: event._id,
planning_date: event._sortDate || event.dates?.start,
place: event.place,
subject: event.subject,
anpa_category: event.anpa_category,
agendas: [],
language: event.language,
};

newPlanningItem = convertStringFields(
event,
newPlanningItem,
'event',
'planning',
[
['slugline', 'slugline'],
['internal_note', 'internal_note'],
['name', 'name'],
['definition_short', 'description_text'],
['ednote', 'ednote'],
],
) as Partial<IPlanningItem>;

if (event.languages != null) {
newPlanningItem.languages = event.languages;
}

return newPlanningItem;
}

/**
* Action dispatcher that creates a planning item from the supplied event,
* @param {object} event - The event used to create the planning item
Expand All @@ -249,58 +283,13 @@ const createPlanningFromEvent = (
planningDate: Moment = null,
agendas: Array<string> = []
) => {
const newPlanningItem: Partial<IPlanningItem> = {
event_item: event._id,
slugline: stringUtils.convertStringFieldForProfileFieldType(
'event',
'planning',
'slugline',
'slugline',
event.slugline
),
planning_date: planningDate || event._sortDate || event.dates.start,
internal_note: stringUtils.convertStringFieldForProfileFieldType(
'event',
'planning',
'internal_note',
'internal_note',
event.internal_note
),
name: stringUtils.convertStringFieldForProfileFieldType(
'event',
'planning',
'name',
'name',
event.name
),
place: event.place,
subject: event.subject,
anpa_category: event.anpa_category,
description_text: stringUtils.convertStringFieldForProfileFieldType(
'event',
'planning',
'definition_short',
'description_text',
event.definition_short
),
ednote: stringUtils.convertStringFieldForProfileFieldType(
'event',
'planning',
'ednote',
'ednote',
event.ednote
),
agendas: agendas,
language: event.language,
};
const newPlanningItem = convertEventToPlanningItem(event);

if (event.languages != null) {
newPlanningItem.languages = event.languages;
if (planningDate != null) {
newPlanningItem.planning_date = planningDate;
}

if (event.translations != null) {
newPlanningItem.translations = event.translations;
}
newPlanningItem.agendas = newPlanningItem.agendas.concat(agendas);

return (dispatch) => (
dispatch(planning.api.save({}, newPlanningItem))
Expand Down
60 changes: 21 additions & 39 deletions client/actions/events/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isItemPublic,
stringUtils,
} from '../../utils';
import {convertStringFields} from '../../utils/strings';

/**
* Action Dispatcher to fetch events from the server
Expand Down Expand Up @@ -748,46 +749,17 @@ const createEventFromPlanning = (plan: IPlanningItem) => (
name: 'Unplanned event',
};
const eventProfile = selectors.forms.eventProfile(getState());
const newEvent: Partial<IEventItem> = {
let newEvent: Partial<IEventItem> = {
dates: {
start: moment(plan.planning_date).clone(),
end: moment(plan.planning_date)
.clone()
.add(defaultDurationOnChange, 'h'),
tz: moment.tz.guess(),
},
name: plan.name?.length ?
stringUtils.convertStringFieldForProfileFieldType(
'planning',
'event',
'name',
'name',
plan.name
) :
stringUtils.convertStringFieldForProfileFieldType(
'planning',
'event',
'slugline',
'name',
plan.slugline
),
subject: plan.subject,
anpa_category: plan.anpa_category,
definition_short: stringUtils.convertStringFieldForProfileFieldType(
'planning',
'event',
'description_text',
'definition_short',
plan.description_text
),
calendars: [],
internal_note: stringUtils.convertStringFieldForProfileFieldType(
'planning',
'event',
'internal_note',
'internal_note',
plan.internal_note
),
place: plan.place,
occur_status: unplannedStatus,
_planning_item: plan._id,
Expand All @@ -798,20 +770,30 @@ const createEventFromPlanning = (plan: IPlanningItem) => (
newEvent.languages = plan.languages;
}

if (plan.translations != null) {
newEvent.translations = plan.translations;
const fieldsToConvert: Array<[keyof IPlanningItem, keyof IEventItem]> = [
['description_text', 'definition_short'],
['internal_note', 'internal_note'],
['slugline', 'slugline'],
];

if (plan.name?.length) {
fieldsToConvert.push(['name', 'name']);
} else {
fieldsToConvert.push(['slugline', 'name']);
}

if (get(eventProfile, 'editor.slugline.enabled', false)) {
newEvent.slugline = stringUtils.convertStringFieldForProfileFieldType(
'planning',
'event',
'slugline',
'slugline',
plan.slugline
);
fieldsToConvert.push(['slugline', 'slugline']);
}

newEvent = convertStringFields(
plan,
newEvent,
'planning',
'event',
fieldsToConvert,
);

return Promise.all([
planningApi.locks.lockItem(plan, 'add_as_event'),
dispatch(main.createNew(ITEM_TYPE.EVENT, newEvent)),
Expand Down
1 change: 1 addition & 0 deletions client/actions/tests/agenda_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ describe('agenda', () => {
expect(apiSpy.save.args[0]).toEqual([
{},
{
type: 'planning',
event_item: events[0]._id,
planning_date: events[0].dates.start,
slugline: events[0].slugline,
Expand Down
25 changes: 3 additions & 22 deletions client/api/editor/item_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {TEMP_ID_PREFIX} from '../../constants';

import {AddPlanningBookmark, AssociatedPlanningsBookmark} from '../../components/Editor/bookmarks';
import {RelatedPlanningItem} from '../../components/fields/editor/EventRelatedPlannings/RelatedPlanningItem';
import {convertEventToPlanningItem} from '../../actions';


export function getEventsInstance(type: EDITOR_TYPE): IEditorAPI['item']['events'] {
Expand Down Expand Up @@ -80,31 +81,11 @@ export function getEventsInstance(type: EDITOR_TYPE): IEditorAPI['item']['events
const plans = cloneDeep(event.associated_plannings || []);
const id = generateTempId();

const newPlanningItem: DeepPartial<IPlanningItem> = {
const newPlanningItem: Partial<IPlanningItem> = {
_id: id,
type: 'planning',
event_item: event._id,
slugline: event.slugline,
planning_date: event._sortDate || event.dates?.start,
internal_note: event.internal_note,
name: event.name,
place: event.place,
subject: event.subject,
anpa_category: event.anpa_category,
description_text: event.definition_short,
ednote: event.ednote,
agendas: [],
language: event.language,
...convertEventToPlanningItem(event as IEventItem),
};

if (event.languages != null) {
newPlanningItem.languages = event.languages;
}

if (event.translations != null) {
newPlanningItem.translations = event.translations;
}

plans.push(newPlanningItem);

editor.form.changeField('associated_plannings', plans)
Expand Down
3 changes: 3 additions & 0 deletions client/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ interface ResizeObserver {
unobserve(target: Element): void;
}

// eslint-disable-next-line no-redeclare
declare var ResizeObserver: {
prototype: ResizeObserver;
new(callback: ResizeObserverCallback): ResizeObserver;
Expand All @@ -126,6 +127,7 @@ interface ResizeObserverEntry {
readonly target: Element;
}

// eslint-disable-next-line no-redeclare
declare var ResizeObserverEntry: {
prototype: ResizeObserverEntry;
new(): ResizeObserverEntry;
Expand All @@ -136,6 +138,7 @@ interface ResizeObserverSize {
readonly inlineSize: number;
}

// eslint-disable-next-line no-redeclare
declare var ResizeObserverSize: {
prototype: ResizeObserverSize;
new(): ResizeObserverSize;
Expand Down
45 changes: 44 additions & 1 deletion client/utils/strings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import {IEventOrPlanningItem, IProfileSchemaTypeString} from '../interfaces';
import {IEventItem, IEventOrPlanningItem, IPlanningItem, IProfileSchemaTypeString} from '../interfaces';
import {planningApi} from '../superdeskApi';

function firstCharUpperCase(string?: string): string {
Expand Down Expand Up @@ -120,6 +120,49 @@ function convertStringFieldForProfileFieldType(
return value;
}


export function convertStringFields<Src extends IEventOrPlanningItem, Dest extends IEventOrPlanningItem>(
itemSrc: Partial<Src>,
_itemDest: Partial<Dest>,
srcItemType: IEventOrPlanningItem['type'] | 'coverage',
destItemType: IEventOrPlanningItem['type'] | 'coverage',
fieldsToCopy: Array<[keyof Src, keyof Dest]>, // array of tuples [srcField, destField]
): Partial<Dest> {
const itemDest: Partial<Dest> = {..._itemDest};

for (const [srcField, destField] of fieldsToCopy) {
const valSrc = itemSrc[srcField] as string;
const valDest = convertStringFieldForProfileFieldType(
srcItemType,
destItemType,
srcField as string,
destField as string,
valSrc,
);

itemDest[destField as string] = valDest;
}

const fieldsToCopyMap = fieldsToCopy.reduce((acc, [srcField, destField]) => {
acc[srcField as string] = destField;

return acc;
}, {});

const translationsDest: IPlanningItem['translations'] = (itemSrc.translations ?? [])
.filter((itemSrc) => fieldsToCopyMap[itemSrc.field] != null)
.map((translationSrc: IPlanningItem['translations']['0']) => ({
...translationSrc,
field: fieldsToCopyMap[translationSrc.field],
}));

if (translationsDest.length > 0) {
itemDest.translations = (itemSrc.translations ?? []).concat(translationsDest);
}

return itemDest;
}

// eslint-disable-next-line consistent-this
const self = {
convertNewlineToBreak,
Expand Down
Loading

0 comments on commit 3cf99a9

Please sign in to comment.