Skip to content

Commit

Permalink
feat: handle display of validation states, checkbox functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
efloden committed Mar 19, 2018
1 parent b117753 commit 1ab7f27
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
Expand Up @@ -19,7 +19,6 @@
import org.zanata.rest.service.ETagUtils;
import org.zanata.rest.editor.service.resource.ProjectResource;
import org.zanata.service.ValidationService;
import org.zanata.webtrans.shared.model.ProjectIterationId;
import org.zanata.webtrans.shared.model.ValidationAction;
import org.zanata.webtrans.shared.model.ValidationId;

Expand Down
Expand Up @@ -8,17 +8,19 @@ class SettingOption extends React.Component {
label: PropTypes.string.isRequired,
active: PropTypes.bool.isRequired,
/* arguments: (string: settingId, bool: active) */
updateSetting: PropTypes.func.isRequired
updateSetting: PropTypes.func.isRequired,
disabled: PropTypes.bool
}

updateSetting = (event) => {
this.props.updateSetting(this.props.id, event.target.checked)
}

render () {
const { label, active } = this.props
const { label, active, disabled } = this.props
return (
<Checkbox checked={active}
disabled={disabled}
onChange={this.updateSetting}>
&nbsp;{label}
</Checkbox>
Expand Down
Expand Up @@ -3,11 +3,12 @@ import React from 'react'
import * as PropTypes from 'prop-types'
import SettingOption from '../SettingOption'

const SettingsOptions = ({settings, updateSetting}) => {
const SettingsOptions = ({settings, updateSetting, disabled}) => {
const checkboxes = settings.map((setting, index) => (
<li key={index}>
<SettingOption
updateSetting={updateSetting}
disabled={disabled}
{...setting} />
</li>
))
Expand All @@ -27,7 +28,8 @@ SettingsOptions.propTypes = {
active: PropTypes.bool.isRequired
})).isRequired,
/* arguments: (string: settingId, bool: active) */
updateSetting: PropTypes.func.isRequired
updateSetting: PropTypes.func.isRequired,
disabled: PropTypes.bool
}

export default SettingsOptions
40 changes: 26 additions & 14 deletions server/zanata-frontend/src/app/editor/containers/SettingsPanel.js
Expand Up @@ -34,6 +34,12 @@ import {
PRINTF_XSI_EXTENSION
} from '../reducers/settings-reducer'

// Validator error types
// TODO: convert to tuple
export const ERROR = 'Error'
export const WARNING = 'Warning'
export const OFF = 'Off'

export const SettingsPanel = ({
enterSavesImmediately,
syntaxHighligting,
Expand All @@ -50,6 +56,9 @@ export const SettingsPanel = ({
isRTL
}) => {
const directionClass = isRTL ? 'rtl' : 'ltr'
const validatorChecked = (validator) => {
return (validator === ERROR || validator === WARNING)
}
return (
<div>
<h1 className="SidebarEditor-heading">
Expand Down Expand Up @@ -83,37 +92,40 @@ export const SettingsPanel = ({
{
id: HTML_XML,
label: 'HTML/XML tags',
active: validateHtmlXml
active: validatorChecked(validateHtmlXml),
disabled: validateHtmlXml === ERROR
},
{
id: JAVA_VARIABLES,
label: 'Java variables',
active: validateJavaVariables
active: validatorChecked(validateJavaVariables),
disabled: validateJavaVariables === ERROR
},
{
id: NEW_LINE,
label: 'Leading/trailing newline (\\n)',
active: validateNewLine
active: validatorChecked(validateNewLine),
disabled: validateNewLine === ERROR
},
{
id: PRINTF_XSI_EXTENSION,
label: 'Positional printf (XSI extention)',
active: validatePrintfXsi
active: validatorChecked(validatePrintfXsi)
},
{
id: PRINTF_VARIABLES,
label: 'Printf variables',
active: validatePrintfVariables
active: validatorChecked(validatePrintfVariables)
},
{
id: TAB,
label: 'Tab characters (\\t)',
active: validateTab
active: validatorChecked(validateTab)
},
{
id: XML_ENTITY,
label: 'XML entity reference',
active: validateXmlEntity
active: validatorChecked(validateXmlEntity)
}
]}
updateSetting={updateValidationSetting} />
Expand All @@ -126,13 +138,13 @@ export const SettingsPanel = ({
SettingsPanel.propTypes = {
enterSavesImmediately: PropTypes.bool.isRequired,
syntaxHighligting: PropTypes.bool.isRequired,
validateHtmlXml: PropTypes.bool.isRequired,
validateNewLine: PropTypes.bool.isRequired,
validateTab: PropTypes.bool.isRequired,
validateJavaVariables: PropTypes.bool.isRequired,
validateXmlEntity: PropTypes.bool.isRequired,
validatePrintfVariables: PropTypes.bool.isRequired,
validatePrintfXsi: PropTypes.bool.isRequired,
validateHtmlXml: PropTypes.string.isRequired,
validateNewLine: PropTypes.string.isRequired,
validateTab: PropTypes.string.isRequired,
validateJavaVariables: PropTypes.string.isRequired,
validateXmlEntity: PropTypes.string.isRequired,
validatePrintfVariables: PropTypes.string.isRequired,
validatePrintfXsi: PropTypes.string.isRequired,
hideSettings: PropTypes.func.isRequired,
updateSetting: PropTypes.func.isRequired,
updateValidationSetting: PropTypes.func.isRequired,
Expand Down
46 changes: 31 additions & 15 deletions server/zanata-frontend/src/app/editor/reducers/settings-reducer.js
Expand Up @@ -10,7 +10,8 @@ import {
SETTING_UPDATE,
SETTINGS_SAVE_REQUEST,
SETTINGS_SAVE_SUCCESS,
SETTINGS_SAVE_FAILURE
SETTINGS_SAVE_FAILURE,
VALIDATORS_SUCCESS
} from '../actions/settings-action-types'
import { SHORTCUTS } from '../actions/key-shortcuts-actions'

Expand All @@ -20,13 +21,13 @@ export const SUGGESTIONS_DIFF = 'suggestions-diff'
export const KEY_SUGGESTIONS_VISIBLE = 'suggestions-visible'

/* Validation Options */
export const HTML_XML = 'html-xml-tags'
export const NEW_LINE = 'leading-trailing-newline'
export const TAB = 'tab-characters'
export const JAVA_VARIABLES = 'java-variables'
export const XML_ENTITY = 'xml-entity-reference'
export const PRINTF_VARIABLES = 'printf-variables'
export const PRINTF_XSI_EXTENSION = 'positional-printf-xsi'
export const HTML_XML = 'HTML_XML'
export const NEW_LINE = 'NEW_LINE'
export const TAB = 'TAB'
export const JAVA_VARIABLES = 'JAVA_VARIABLES'
export const XML_ENTITY = 'XML_ENTITY'
export const PRINTF_VARIABLES = 'PRINTF_VARIABLES'
export const PRINTF_XSI_EXTENSION = 'PRINTF_XSI_EXTENSION'

/* Parse values of known settings to appropriate types */
function parseKnownSettings (settings) {
Expand Down Expand Up @@ -58,6 +59,9 @@ function parseKnownSettings (settings) {
/* convenience function to construct an empty setting body */
const newSetting = value => ({ value, saving: false, error: undefined })

// Default validator value: one of ['Error','Warning','Off']
export const defaultValidation = 'Off'

export const defaultState = {
// state for all settings being requested on app load
fetching: false,
Expand All @@ -72,13 +76,13 @@ export const defaultState = {
[SUGGESTIONS_DIFF]: newSetting(true),
[KEY_SUGGESTIONS_VISIBLE]: newSetting(true),
// Validator options disabled by default
[HTML_XML]: newSetting(false),
[NEW_LINE]: newSetting(false),
[TAB]: newSetting(false),
[JAVA_VARIABLES]: newSetting(false),
[XML_ENTITY]: newSetting(false),
[PRINTF_VARIABLES]: newSetting(false),
[PRINTF_XSI_EXTENSION]: newSetting(false)
[HTML_XML]: newSetting(defaultValidation),
[NEW_LINE]: newSetting(defaultValidation),
[TAB]: newSetting(defaultValidation),
[JAVA_VARIABLES]: newSetting(defaultValidation),
[XML_ENTITY]: newSetting(defaultValidation),
[PRINTF_VARIABLES]: newSetting(defaultValidation),
[PRINTF_XSI_EXTENSION]: newSetting(defaultValidation)
}
}

Expand Down Expand Up @@ -160,6 +164,18 @@ export default handleActions({
}
})
},
[VALIDATORS_SUCCESS]: (state, { payload }) => {
const defined = keys(state.settings)
const parsed = parseKnownSettings(payload)
const updates = mapValues(pick(parsed, defined),
value => ({ value: {$set: value} }))
return update(state, {
fetching: {$set: false},
settings: {
...updates
}
})
},
[SETTINGS_FAILURE]: (state, { payload }) => update(state, {
fetching: {$set: false},
error: {$set: payload}
Expand Down

0 comments on commit 1ab7f27

Please sign in to comment.