Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

validate and remove conflict data when copy event to new draft #375

Merged
merged 2 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/actions/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
})
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {isArray} from 'lodash';

import CONSTANTS from '../constants'

const {VALIDATION_RULES, CHARACTER_LIMIT} = CONSTANTS
Expand Down Expand Up @@ -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})
}
12 changes: 3 additions & 9 deletions src/views/Event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,15 @@ 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`)
}
}

editEvent() {
const {events:{event}, replaceData, routerPush} = this.props
if(event) {
let formData = mapAPIDataToUIFormat(event)

replaceData(formData)
replaceData(event)
routerPush(`/event/update/${event.id}`)
}
}
Expand Down Expand Up @@ -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)),
Expand Down
100 changes: 48 additions & 52 deletions src/views/Search/__snapshots__/Search.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,32 @@ exports[`Search Snapshot should render view correctly 1`] = `
<div
className="hel-text-field"
>
<div>
<div
className="react-datepicker-wrapper"
>
<div
className="react-datepicker-wrapper"
className="react-datepicker__input-container"
>
<div
className="react-datepicker__input-container"
>
<input
autoComplete={undefined}
autoFocus={undefined}
className=""
disabled={false}
id={undefined}
name="startDate"
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
placeholder="pp.kk.vvvv"
readOnly={false}
required={undefined}
tabIndex={undefined}
title={undefined}
type="text"
value=""
/>
</div>
<input
autoComplete={undefined}
autoFocus={undefined}
className=""
disabled={false}
id={undefined}
name="startDate"
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
placeholder="pp.kk.vvvv"
readOnly={false}
required={undefined}
tabIndex={undefined}
title={undefined}
type="text"
value=""
/>
</div>
</div>
</div>
Expand All @@ -71,34 +69,32 @@ exports[`Search Snapshot should render view correctly 1`] = `
<div
className="hel-text-field"
>
<div>
<div
className="react-datepicker-wrapper"
>
<div
className="react-datepicker-wrapper"
className="react-datepicker__input-container"
>
<div
className="react-datepicker__input-container"
>
<input
autoComplete={undefined}
autoFocus={undefined}
className=""
disabled={false}
id={undefined}
name="endDate"
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
placeholder="pp.kk.vvvv"
readOnly={false}
required={undefined}
tabIndex={undefined}
title={undefined}
type="text"
value=""
/>
</div>
<input
autoComplete={undefined}
autoFocus={undefined}
className=""
disabled={false}
id={undefined}
name="endDate"
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
placeholder="pp.kk.vvvv"
readOnly={false}
required={undefined}
tabIndex={undefined}
title={undefined}
type="text"
value=""
/>
</div>
</div>
</div>
Expand Down