diff --git a/client/actions/events/ui.js b/client/actions/events/ui.js index 7a48ba467..dc2116386 100644 --- a/client/actions/events/ui.js +++ b/client/actions/events/ui.js @@ -657,13 +657,13 @@ const selectCalendar = (calendarId = '', params = {}) => ( } ); -const fetchEventWithFiles = (event, saveToStore = true) => ( +const fetchEventWithFiles = (event) => ( (dispatch) => { if (!isExistingItem(event) || get(event, 'files.length', 0) === 0) { return Promise.resolve(event); } - return dispatch(eventsApi.fetchById(event._id, {force: true, saveToStore: saveToStore})); + return dispatch(eventsApi.fetchById(event._id, {force: true, saveToStore: false})); } ); diff --git a/client/components/Contacts/ContactField.jsx b/client/components/Contacts/ContactField.jsx index 6c89806a5..85c28acae 100644 --- a/client/components/Contacts/ContactField.jsx +++ b/client/components/Contacts/ContactField.jsx @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import {connect} from 'react-redux'; import * as selectors from '../../selectors'; -import {get, isEqual} from 'lodash'; +import {get, isEqual, difference} from 'lodash'; import {ContactEditor, SelectSearchContactsField} from './index'; import eventsApi from '../../actions/events/api'; import {CONTACTS} from '../../constants'; @@ -75,11 +75,16 @@ export class ContactFieldComponent extends React.Component { } fetchEventContacts(values) { - this.props.fetchContacts(values) - .then(this.getResponseResult) - .then((results) => { - this.getOptions(results || []); - }); + if (get(values, 'length', 0) && + difference(values, this.props.eventContacts.map((c) => c._id)).length > 0) { + this.props.fetchContacts(values) + .then(this.getResponseResult) + .then((results) => { + this.getOptions(results || []); + }); + } else { + this.getOptions(this.props.eventContacts, false, values); + } } getResponseResult(data = null) { @@ -126,7 +131,7 @@ export class ContactFieldComponent extends React.Component { }; } - getOptions(filteredContacts = this.props.eventContacts, onSearch) { + getOptions(filteredContacts = this.props.eventContacts, onSearch, currentValues = this.props.value) { let options = []; let values = []; let _filteredValues = []; @@ -134,7 +139,8 @@ export class ContactFieldComponent extends React.Component { options = (filteredContacts).map((contact) => this.getOption(contact)); if (!onSearch) { - values = (filteredContacts).map((contact) => this.getValue(contact)); + values = (filteredContacts.filter((c) => currentValues.includes(c._id))) + .map((contact) => this.getValue(contact)); _filteredValues = values; } else { diff --git a/client/components/Events/EventEditor/index.jsx b/client/components/Events/EventEditor/index.jsx index b34de9df9..44b7e6b0e 100644 --- a/client/components/Events/EventEditor/index.jsx +++ b/client/components/Events/EventEditor/index.jsx @@ -47,13 +47,6 @@ export class EventEditorComponent extends React.Component { }; } - componentWillMount() { - if (this.props.itemExists) { - // Get the event with files with it - this.props.fetchEventWithFiles(this.props.item); - } - } - componentDidMount() { if (!get(this.props, 'navigation.scrollToViewItem')) { this.dom.slugline.focus(); @@ -88,8 +81,11 @@ export class EventEditorComponent extends React.Component { } } - if (eventUtils.shouldFetchFilesForEvent(this.props.item)) { - this.props.fetchEventWithFiles(this.props.item); + if (eventUtils.shouldFetchFilesForEvent(this.props.diff)) { + this.props.fetchEventWithFiles(this.props.item) + .then((eventWithFiles) => { + this.props.onChangeHandler('files', eventWithFiles.files, false, false); + }); } if (this.dom.top) { diff --git a/client/components/Events/EventPreviewContent.jsx b/client/components/Events/EventPreviewContent.jsx index 3a74c8840..ae041ff56 100644 --- a/client/components/Events/EventPreviewContent.jsx +++ b/client/components/Events/EventPreviewContent.jsx @@ -1,7 +1,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import {connect} from 'react-redux'; -import {gettext, getCreator, isValidFileInput} from '../../utils'; +import {gettext, getCreator, isValidFileInput, eventUtils} from '../../utils'; import * as selectors from '../../selectors'; import {get, isEqual} from 'lodash'; import {Row} from '../UI/Preview'; @@ -29,6 +29,7 @@ export class EventPreviewContentComponent extends React.Component { currentContact: [], editDetails: false, viewIndex: null, + files: [], }; this.fetchEventContacts = this.fetchEventContacts.bind(this); this.getResponseResult = this.getResponseResult.bind(this); @@ -43,7 +44,14 @@ export class EventPreviewContentComponent extends React.Component { } componentWillMount() { - this.props.fetchEventWithFiles(this.props.item); + if (eventUtils.shouldFetchFilesForEvent(this.props.item)) { + this.props.fetchEventWithFiles(this.props.item) + .then((eventWithFiles) => { + this.setState({files: eventWithFiles.files}); + }); + } else if (get(this.props, 'item.files.length', 0) > 0) { + this.setState({files: [...this.props.item.files]}); + } } componentDidMount() { @@ -248,7 +256,7 @@ export class EventPreviewContentComponent extends React.Component { badgeValue={get(item, 'files.length', 0) > 0 ? item.files.length : null}> {get(item, 'files.length') > 0 ?