diff --git a/src/actions/editor.js b/src/actions/editor.js index 9fb0ad4c6..ed49784ff 100644 --- a/src/actions/editor.js +++ b/src/actions/editor.js @@ -4,10 +4,12 @@ import {includes, keys} from 'lodash'; import constants from '../constants' import { + mapAPIDataToUIFormat, mapUIDataToAPIFormat, calculateSuperEventTime, combineSubEventsFromEditor, } from '../utils/formDataMapping' +import {emptyField} from '../utils/helpers' import {push} from 'react-router-redux' import {setFlashMsg, confirmAction} from './app' @@ -89,15 +91,27 @@ export function setLanguages(languages) { * Replace all editor values * @param {obj} formValues new form values to replace all existing values */ -export function replaceData(formValues) { - return (dispatch) => { - // Run validations - dispatch(validateFor(null)) - dispatch(setValidationErrors({})) +export function replaceData(formData) { + return (dispatch, getState) => { + const {contentLanguages} = getState().editor + let formObject = mapAPIDataToUIFormat(formData) + const publicationStatus = formObject.publication_status || constants.PUBLICATION_STATUS.PUBLIC + // run the validation before copy to a draft + const validationErrors = doValidations(formObject, contentLanguages, publicationStatus) + + // empty id, event_status, and any field that has validation errors + keys(validationErrors).map(field => { + formObject = emptyField(formObject, field) + }) + delete formObject.id + delete formObject.event_status + + dispatch(validateFor(publicationStatus)) + dispatch(setValidationErrors({})) dispatch({ type: constants.EDITOR_REPLACEDATA, - values: formValues, + values: formObject, }) } } diff --git a/src/utils/helpers.js b/src/utils/helpers.js index d3db3effc..7e176c2eb 100644 --- a/src/utils/helpers.js +++ b/src/utils/helpers.js @@ -1,3 +1,5 @@ +import {isArray} from 'lodash'; + import CONSTANTS from '../constants' const {VALIDATION_RULES, CHARACTER_LIMIT} = CONSTANTS @@ -44,3 +46,25 @@ export function textLimitValidator(value, limit) { } return true } + +// set a property of an object to empty value based on its type +export const emptyField = (object, field) => { + let value = object[field]; + const fieldValueType = isArray(value) ? 'array' : typeof value; + + switch (fieldValueType) { + case 'array': + value = [] + break + case 'object': + value = {} + break + case 'string': + case 'number': + value = '' + break + default: + } + + return Object.assign({}, object, {[field]: value}) +} diff --git a/src/views/Event/index.js b/src/views/Event/index.js index 6b45d282b..1a050b6c0 100644 --- a/src/views/Event/index.js +++ b/src/views/Event/index.js @@ -40,11 +40,7 @@ class EventPage extends React.Component { copyAsTemplate() { const {events:{event}, replaceData, routerPush} = this.props if(event) { - let formData = mapAPIDataToUIFormat(event) - formData.id = undefined - delete formData.id - - replaceData(formData) + replaceData(event) routerPush(`/event/create/new`) } } @@ -52,9 +48,7 @@ class EventPage extends React.Component { editEvent() { const {events:{event}, replaceData, routerPush} = this.props if(event) { - let formData = mapAPIDataToUIFormat(event) - - replaceData(formData) + replaceData(event) routerPush(`/event/update/${event.id}`) } } @@ -235,7 +229,7 @@ const mapStateToProps = (state) => ({ const mapDispatchToProps = (dispatch) => ({ fetchEventDetails: (eventId, user) => dispatch(fetchEventDetailsAction(eventId, user)), routerPush: (url) => dispatch(push(url)), - replaceData: (formData) => dispatch(replaceDataAction(formData)), + replaceData: (event) => dispatch(replaceDataAction(event)), confirm: (msg, style, actionButtonLabel, data) => dispatch(confirmAction(msg, style, actionButtonLabel, data)), deleteEvent: (eventId, user) => dispatch(deleteEventAction(eventId, user)), cancelEvent: (eventId, user, values) => dispatch(cancelEventAction(eventId, user, values)), diff --git a/src/views/Search/__snapshots__/Search.test.js.snap b/src/views/Search/__snapshots__/Search.test.js.snap index 36c25285d..91d116052 100644 --- a/src/views/Search/__snapshots__/Search.test.js.snap +++ b/src/views/Search/__snapshots__/Search.test.js.snap @@ -33,34 +33,32 @@ exports[`Search Snapshot should render view correctly 1`] = `
-
+
-
- -
+
@@ -71,34 +69,32 @@ exports[`Search Snapshot should render view correctly 1`] = `
-
+
-
- -
+