Example #1
0
    return (dispatch) => {

        publicationStatus = publicationStatus || formValues.publication_status

        if(!publicationStatus) {
            return
        }

        // Set publication status for editor values. This is used by the validation to determine
        // which set of rules to use
        dispatch(validateFor(publicationStatus))

        // Run validations
        let validationErrors = doValidations(formValues, publicationStatus)

        // There are validation errors, don't continue sending
        if (_.keys(validationErrors).length > 0) {
            return dispatch(setValidationErrors(validationErrors))
        }

        let url = `${appSettings.api_base}/event/`

        if(updateExisting) {
            url += `${formValues.id}/`
        }

        let token = ''
        if(user) {
             token = user.token
        }

        let data = Object.assign({}, formValues, { publication_status: publicationStatus })

        return fetch(url, {
            method: updateExisting ? 'PUT' : 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'JWT ' + token,
            },
            body: JSON.stringify(mapUIDataToAPIFormat(data))
        }).then(response => {
            //console.log('Received', response)
            let jsonPromise = response.json()

            jsonPromise.then(json => {
                let actionName = updateExisting ? 'update' : 'create'

                // The publication_status was changed to public. The event was published!
                if(json.publication_status === constants.PUBLICATION_STATUS.PUBLIC && json.publication_status !== formValues.publication_status) {
                    actionName = 'publish'
                } else if ( json.publication_status === constants.PUBLICATION_STATUS.PUBLIC ) {
                    actionName ='savepublic'
                } else if ( json.publication_status === constants.PUBLICATION_STATUS.DRAFT ) {
                    actionName ='savedraft'
                }

                if(response.status === 200 || response.status === 201) {
                    dispatch(sendDataComplete(json, actionName))
                }
                // Validation errors
                else if(response.status === 400) {
                    json.apiErrorMsg = 'validation-error'
                    dispatch(sendDataComplete(json, actionName))
                }

                // Auth errors
                else if(response.status === 401 || response.status === 403) {
                    json.apiErrorMsg = 'authorization-required'
                    dispatch(sendDataComplete(json, actionName))
                }

                else {
                    json.apiErrorMsg = 'server-error'
                    dispatch(sendDataComplete(json, actionName))
                }
            })
        })
        .catch(e => {
            // Error happened while fetching ajax (connection or javascript)
        })
    }
Example #2
0
function update(state = initialState, action) {
    if(action.type === constants.EDITOR_SETDATA) {

        // Merge new values to existing values
        let newValues = Object.assign({}, state.values, action.values)

        // Local storage saving disabled for now
        // localStorage.setItem('EDITOR_VALUES', JSON.stringify(newValues))

        let validationErrors = Object.assign({}, state.validationErrors)
        // If there are validation errors, check if they are fixed
        if (_.keys(state.validationErrors).length > 0) {
            validationErrors = doValidations(newValues, state.validateFor)
        }

        return Object.assign({}, state, {
            values: newValues,
            validationErrors: validationErrors
        })
    }

    if (action.type === constants.EDITOR_SETLANGUAGES) {
        return Object.assign({}, state, {
            contentLanguages: action.languages
        });
    }

    if(action.type === constants.VALIDATE_FOR) {
        return Object.assign({}, state, {
            validateFor: action.validateFor
        })
    }

    if(action.type === constants.EDITOR_REPLACEDATA) {

        // Replace new values to existing values
        let newValues = Object.assign({}, action.values)

        // Local storage saving disabled for now
        // localStorage.setItem('EDITOR_VALUES', JSON.stringify(newValues))

        return Object.assign({}, state, {
            values: newValues
        })
    }

    if(action.type === constants.EDITOR_CLEARDATA) {
        clearEventDataFromLocalStorage()

        return Object.assign({}, state, {
            values: {},
            validationErrors: {},
            validateFor: null,
            validationStatus: constants.VALIDATION_STATUS.CLEARED
        })
    }

    if(action.type === constants.EDITOR_SENDDATA_SUCCESS) {
        clearEventDataFromLocalStorage()

        return Object.assign({}, state, {
            createdEvent: action.data.event,
            createdAt: action.data.createdAt,
            values: {}
        })
    }

    if(action.type === constants.EDITOR_SENDDATA_ERROR) {
        return state;
    }

    if(action.type === constants.EDITOR_RECEIVE_KEYWORDSETS) {
        return Object.assign({}, state, {
            keywordSets: action.keywordSets
        })
    }

    if(action.type === constants.EDITOR_RECEIVE_LANGUAGES) {
        return Object.assign({}, state, {
            languages: action.languages
        })
    }

    if(action.type === constants.RECEIVE_EVENT_FOR_EDITING) {
        let newValues = Object.assign({}, mapAPIDataToUIFormat(action.event))

        return Object.assign({}, state, {
            values: newValues,
            contentLanguages: getContentLanguages(action.event),
        })
    }

    if (action.type === constants.SELECT_IMAGE_BY_ID) {
        let newVal = getIfExists(action, 'img', null)
        // Merge new values to existing values
        let newValues = Object.assign({}, state.values, {image: newVal})
        return Object.assign({}, state, {
            values: newValues
        })
    }

    if(action.type === constants.SET_VALIDATION_ERRORS) {
        return Object.assign({}, state, {
            validationErrors: action.errors,
            validationStatus: constants.VALIDATION_STATUS.RESOLVE
        })
    }

    return state
}