From 926af37405347b92f16db50dbaf96a5593ec9240 Mon Sep 17 00:00:00 2001 From: Thanh Do Date: Wed, 5 Dec 2018 17:18:05 +0200 Subject: [PATCH 1/2] do validation and emptying invalid fields before copy from event to new draft Signed-off-by: Thanh Do --- src/actions/editor.js | 21 +++- src/utils/helpers.js | 24 +++++ src/views/Event/index.js | 3 +- .../Search/__snapshots__/Search.test.js.snap | 100 +++++++++--------- 4 files changed, 89 insertions(+), 59 deletions(-) diff --git a/src/actions/editor.js b/src/actions/editor.js index 9fb0ad4c6..3493d2581 100644 --- a/src/actions/editor.js +++ b/src/actions/editor.js @@ -8,6 +8,7 @@ import { calculateSuperEventTime, combineSubEventsFromEditor, } from '../utils/formDataMapping' +import {emptyField} from '../utils/helpers' import {push} from 'react-router-redux' import {setFlashMsg, confirmAction} from './app' @@ -90,14 +91,24 @@ export function setLanguages(languages) { * @param {obj} formValues new form values to replace all existing values */ export function replaceData(formValues) { - return (dispatch) => { - // Run validations - dispatch(validateFor(null)) - dispatch(setValidationErrors({})) + return (dispatch, getState) => { + const {contentLanguages} = getState().editor + let formObject = formValues + const publicationStatus = formValues.publication_status || constants.PUBLICATION_STATUS.PUBLIC + // run the validation before copy to a draft + const validationErrors = doValidations(formValues, contentLanguages, publicationStatus) + + // empty any field that has validation errors + keys(validationErrors).map(field => { + formObject = emptyField(formObject, field) + }) + + 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..a04cfc48b 100644 --- a/src/views/Event/index.js +++ b/src/views/Event/index.js @@ -43,7 +43,6 @@ class EventPage extends React.Component { let formData = mapAPIDataToUIFormat(event) formData.id = undefined delete formData.id - replaceData(formData) routerPush(`/event/create/new`) } @@ -235,7 +234,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: (formData, publication_status) => dispatch(replaceDataAction(formData, publication_status)), 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`] = `
-
+
-
- -
+
From da3544bdc6fc3aed96b34a93f4e01a67a106aa35 Mon Sep 17 00:00:00 2001 From: Thanh Do Date: Wed, 5 Dec 2018 18:32:41 +0200 Subject: [PATCH 2/2] remove event_status in draft, refactor code for copying existing event Signed-off-by: Thanh Do --- src/actions/editor.js | 15 +++++++++------ src/views/Event/index.js | 11 +++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/actions/editor.js b/src/actions/editor.js index 3493d2581..ed49784ff 100644 --- a/src/actions/editor.js +++ b/src/actions/editor.js @@ -4,6 +4,7 @@ import {includes, keys} from 'lodash'; import constants from '../constants' import { + mapAPIDataToUIFormat, mapUIDataToAPIFormat, calculateSuperEventTime, combineSubEventsFromEditor, @@ -90,20 +91,22 @@ export function setLanguages(languages) { * Replace all editor values * @param {obj} formValues new form values to replace all existing values */ -export function replaceData(formValues) { +export function replaceData(formData) { return (dispatch, getState) => { const {contentLanguages} = getState().editor - let formObject = formValues - const publicationStatus = formValues.publication_status || constants.PUBLICATION_STATUS.PUBLIC + let formObject = mapAPIDataToUIFormat(formData) + const publicationStatus = formObject.publication_status || constants.PUBLICATION_STATUS.PUBLIC // run the validation before copy to a draft - const validationErrors = doValidations(formValues, contentLanguages, publicationStatus) + const validationErrors = doValidations(formObject, contentLanguages, publicationStatus) - // empty any field that has validation errors + // 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({ diff --git a/src/views/Event/index.js b/src/views/Event/index.js index a04cfc48b..1a050b6c0 100644 --- a/src/views/Event/index.js +++ b/src/views/Event/index.js @@ -40,10 +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`) } } @@ -51,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}`) } } @@ -234,7 +229,7 @@ const mapStateToProps = (state) => ({ const mapDispatchToProps = (dispatch) => ({ fetchEventDetails: (eventId, user) => dispatch(fetchEventDetailsAction(eventId, user)), routerPush: (url) => dispatch(push(url)), - replaceData: (formData, publication_status) => dispatch(replaceDataAction(formData, publication_status)), + 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)),