Skip to content

Commit

Permalink
Saving planning items (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecalcc authored Jan 23, 2025
1 parent f84614a commit c13fa34
Show file tree
Hide file tree
Showing 28 changed files with 1,008 additions and 921 deletions.
5 changes: 0 additions & 5 deletions client/components/Events/EventEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import * as React from 'react';
import {connect} from 'react-redux';
import {isEqual} from 'lodash';

import {
IEventItem,
IEventFormProfile,
IFile,
IPlanningItem,
IFormItemManager,
EDITOR_TYPE,
ILocation,
} from '../../../interfaces';
import {IVocabularyItem} from 'superdesk-api';
import {planningApi, superdeskApi} from '../../../superdeskApi';

import * as selectors from '../../../selectors';
import * as actions from '../../../actions';

import {EditorForm} from '../../Editor/EditorForm';
import {EventEditorHeader} from './EventEditorHeader';
import {ContentBlock} from '../../UI/SidePanel';
import {EventScheduleSummary} from '../EventScheduleSummary';
import {CreateNewGeoLookup} from '../../GeoLookupInput/CreateNewGeoLookup';

interface IProps {
original?: IEventItem;
Expand Down
47 changes: 18 additions & 29 deletions client/components/LockContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import React from 'react';
import PropTypes from 'prop-types';
import {get} from 'lodash';
import classNames from 'classnames';

import {UserAvatarWithMargin} from '../../components/UserAvatar';
import {UserAvatar} from '../../components/UserAvatar';
import {LockContainerPopup} from './LockContainerPopup';

import './style.scss';

export class LockContainer extends React.Component {
constructor(props) {
interface LockContainerProps {
lockedUser: any;
users: any[] | any;
displayText?: string;
showUnlock?: boolean;
onUnlock?: () => void;
noMargin?: boolean;
}

interface LockContainerState {
openUnlockPopup: boolean;
}

export class LockContainer extends React.Component<LockContainerProps, LockContainerState> {
constructor(props: LockContainerProps) {
super(props);
this.state = {openUnlockPopup: false};
this.toggleOpenUnlockPopup = this.toggleOpenUnlockPopup.bind(this);
Expand All @@ -24,10 +35,8 @@ export class LockContainer extends React.Component {
lockedUser,
users,
displayText,
showUnlock,
withLoggedInfo,
showUnlock = true,
onUnlock,
small,
noMargin,
} = this.props;

Expand All @@ -49,7 +58,7 @@ export class LockContainer extends React.Component {
)}
>
<a onClick={this.toggleOpenUnlockPopup}>
<UserAvatarWithMargin user={user} />
<UserAvatar user={user} />
</a>
</div>
{this.state.openUnlockPopup && (
Expand All @@ -66,23 +75,3 @@ export class LockContainer extends React.Component {
);
}
}

LockContainer.propTypes = {
lockedUser: PropTypes.object,
users: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
]),
displayText: PropTypes.string,
showUnlock: PropTypes.bool,
withLoggedInfo: PropTypes.bool,
onUnlock: PropTypes.func,
small: PropTypes.bool,
noMargin: PropTypes.bool,
};

LockContainer.defaultProps = {
showUnlock: true,
withLoggedInfo: true,
small: true,
};
110 changes: 50 additions & 60 deletions client/components/Main/ItemEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {ItemManager} from './ItemManager';
import {AutoSave} from './AutoSave';
import {EditorHeader} from './EditorHeader';
import {pickRelatedEventsForPlanning} from './../../../utils/planning';
import {embeddedPlanningHasUnsavedChanges} from '../../../components/editor-standalone/save-handling';

export class EditorComponent extends React.Component<IEditorProps, IEditorState> {
autoSave: AutoSave;
Expand Down Expand Up @@ -58,7 +59,8 @@ export class EditorComponent extends React.Component<IEditorProps, IEditorState>
tabProps: {
forEditor: !this.props.inModalView,
forEditorModal: this.props.inModalView,
}},
}
},
];

if (this.props.addNewsItemToPlanning) {
Expand Down Expand Up @@ -195,68 +197,54 @@ export class EditorComponent extends React.Component<IEditorProps, IEditorState>
return;
}

this.autoSave.flushAutosave()
.then(() => {
const {
openCancelModal,
itemId,
itemType,
addNewsItemToPlanning,
} = this.props;
const {dirty, errorMessages, initialValues} = this.state;

this.setState({submitting: true});
this.autoSave.flushAutosave().then(() => {
const {openCancelModal, itemId, itemType, addNewsItemToPlanning} = this.props;
const {dirty, errorMessages, initialValues} = this.state;
const updateStates = !addNewsItemToPlanning;

const updateStates = !addNewsItemToPlanning;
this.setState({submitting: true});

if (!dirty) {
this.onCancel();
} else {
const hasErrors = !isEqual(errorMessages, []);
const isKilled = isItemKilled(initialValues);
if (!(dirty || embeddedPlanningHasUnsavedChanges())) {
this.onCancel();
return;
}

const onCancel = () => {
if (updateStates) {
this.setState({submitting: false});
}
};

const onIgnore = () => {
this.itemManager.unlockAndCancel();
};

const onSave = (isKilled || hasErrors) ? null :
(withConfirmation, updateMethod, planningUpdateMethods) => (
this.itemManager.save(
withConfirmation,
{name: updateMethod, value: updateMethod},
true,
updateStates,
planningUpdateMethods
)
);

const onSaveAndPost = (!isKilled || hasErrors) ? null :
(withConfirmation, updateMethod, planningUpdateMethods) => (
this.itemManager.saveAndPost(
withConfirmation,
updateMethod,
true,
updateStates,
planningUpdateMethods
)
);

openCancelModal({
itemId: itemId,
itemType: itemType,
onCancel: onCancel,
onIgnore: onIgnore,
onSave: onSave,
onSaveAndPost: onSaveAndPost,
});
}
const hasErrors = !isEqual(errorMessages, []);
const isKilled = isItemKilled(initialValues);
const onSave = (isKilled || hasErrors) ? null : (withConfirmation, updateMethod) => (
this.itemManager.save(
withConfirmation,
{name: updateMethod, value: updateMethod},
true,
updateStates,
)
);
const onSaveAndPost = (!isKilled || hasErrors) ? null : (withConfirmation, updateMethod) => (
this.itemManager.saveAndPost(
withConfirmation,
updateMethod,
true,
updateStates,
)
);

openCancelModal({
itemId: itemId,
itemType: itemType,
onCancel: () => {
if (updateStates) {
this.setState({submitting: false});
}
},
onIgnore: () => {
this.itemManager.unlockAndCancel(
embeddedPlanningHasUnsavedChanges() ? 'HANDLE_UNSAVED_CHANGES' : 'DISCARD',
);
},
onSave: onSave,
onSaveAndPost: onSaveAndPost,
});
});
}

onCancel(updateStates = true) {
Expand All @@ -276,7 +264,9 @@ export class EditorComponent extends React.Component<IEditorProps, IEditorState>
this.props.onCancel();
}

return this.itemManager.unlockAndCancel();
return this.itemManager.unlockAndCancel(
embeddedPlanningHasUnsavedChanges() ? 'HANDLE_UNSAVED_CHANGES' : 'DISCARD',
);
}

setActiveTab(tab) {
Expand Down
Loading

0 comments on commit c13fa34

Please sign in to comment.