DescriptorForm.tsx 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import * as React from 'react';
import { connect } from 'react-redux';
import { Field, reduxForm, formValueSelector } from 'redux-form';
import { translate } from 'react-i18next';

import { log } from 'utilities/debug';

import { DESCRIPTOR_FORM } from 'descriptors/constants';

import ItemsEditor from 'ui/common/ItemsEditor';
import VocabularyTerm from 'model/vocabulary/VocabularyTerm';
import Validators from 'utilities/Validators';

import { TextField } from 'ui/common/text-field';
Matija Obreza's avatar
Matija Obreza committed
15
import { MarkdownField } from 'ui/common/markdown';
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
import Heading from 'ui/common/heading';
import SelectPartner from 'partners/ui/c/SelectPartner';
import SelectVocabulary from './SelectVocabulary';

import Button from '@material-ui/core/Button';
import Radio from '@material-ui/core/Radio';
import RadioGroup from '@material-ui/core/RadioGroup';
import { Toggle, FormControl } from 'ui/common/forms';
import FormLabel from '@material-ui/core/FormLabel';
import FormControlLabel from '@material-ui/core/FormControlLabel';

import Grid from '@material-ui/core/Grid';
import { setPageTitle } from 'actions/pageTitle';
import { bindActionCreators } from 'redux';
import CropSelector from 'crop/ui/c/CropSelector';
import withStyles from '@material-ui/core/styles/withStyles';

/* tslint:disable */
const styles = (theme) => ({
  RadioGrid: {
    flexDirection: 'row' as 'row',
    justifyContent: 'space-evenly' as 'space-evenly',
    '& > label': {
      flexBasis: '45%',
    },
  }
});

/* tslint:enable */
// FIXME Use Descriptor.DATATYPES
const renderDataTypeRadioGroup = ({ input, meta, classes, t, ...rest }) => (
  <FormControl required fullWidth>
    <FormLabel>{ t(`descriptors.admin.c.form.dataType`) }</FormLabel>
    <RadioGroup
      { ...input }
      { ...rest }
      value={ input.value }
      onChange={
        (event, value) => input.onChange(value) // tslint:disable-line
      }
      className={ classes.RadioGrid }
    >
      <FormControlLabel value="TEXT" label={ t(`descriptors.common.dataType.text`) } control={ <Radio/> }/>
      <FormControlLabel value="BOOLEAN" label={ t(`descriptors.common.dataType.boolean`) } control={ <Radio/> }/>
      <FormControlLabel value="DATE" label={ t(`descriptors.common.dataType.date`) } control={ <Radio/> }/>
      <FormControlLabel value="CODED" label={ t(`descriptors.common.dataType.coded`) } control={ <Radio/> }/>
      <FormControlLabel value="NUMERIC" label={ t(`descriptors.common.dataType.numeric`) } control={ <Radio/> }/>
      <FormControlLabel value="SCALE" label={ t(`descriptors.common.dataType.scale`) } control={ <Radio/> }/>
    </RadioGroup>
  </FormControl>
);

// FIXME Use Descriptor.CATEGORIES
const renderCategoryRadioGroup = ({ input, meta, classes, t, ...rest }) => (
  <FormControl required fullWidth>
    <FormLabel>{ t(`descriptors.admin.c.form.descriptorCategory`) }</FormLabel>
    <RadioGroup
      { ...input }
      { ...rest }
      value={ input.value }
      onChange={
        (event, value) => input.onChange(value) // tslint:disable-line
      }
      className={ classes.RadioGrid }
    >
      <FormControlLabel value="PASSPORT" label={ t(`descriptors.common.category.passport`) } control={ <Radio/> }/>
      <FormControlLabel value="MANAGEMENT" label={ t(`descriptors.common.category.management`) } control={ <Radio/> }/>
      <FormControlLabel value="ENVIRONMENT" label={ t(`descriptors.common.category.environment`) } control={ <Radio/> }/>
      <FormControlLabel value="CHARACTERIZATION" label={ t(`descriptors.common.category.characterization`) } control={ <Radio/> }/>
      <FormControlLabel value="EVALUATION" label={ t(`descriptors.common.category.evaluation`) } control={ <Radio/> }/>
      <FormControlLabel value="ABIOTICSTRESS" label={ t(`descriptors.common.category.abioticstress`) } control={ <Radio/> }/>
      <FormControlLabel value="BIOTICSTRESS" label={ t(`descriptors.common.category.bioticstress`) } control={ <Radio/> }/>
      <FormControlLabel value="MOLECULAR" label={ t(`descriptors.common.category.molecular`) } control={ <Radio/> }/>
    </RadioGroup>
  </FormControl>
);

class DescriptorForm extends React.Component<any, any> {

  private requiresMaxValue = () => {
    const { dataType } = this.props;
    return dataType === 'SCALE';
  }

  public constructor(props: any) {
    super(props);
    const { setPageTitle, pageTitle } = this.props;
    setPageTitle(pageTitle);
  }

  public render() {
    const { error, handleSubmit, initialValues, pageTitle, onPublish, invalid, submitting, anyTouched, currentOwner, currentVocabulary, classes, t } = this.props;

    if (!initialValues) {
      return null;
    }

    const onAddMember = () => {
      log('Adding new term');
      return new VocabularyTerm();
    };

    const onRemoveMember = (item) => {
      log('Removing term', item);
    };

    const TermEditor = (member, index, fields) => (
      <Grid container key={ index } justify="space-between" alignItems="flex-end">
        <Grid item xs={ 2 } md={ 2 } lg={ 1 }>
          <Field name={ `${member}.code` } type="text" component={ TextField } label={ t(`descriptors.admin.c.form.code`) }/>
        </Grid>
        <Grid item xs={ 10 } md={ 10 } lg={ 5 }>
          <Field name={ `${member}.title` } type="text" component={ TextField } label={ t(`descriptors.admin.c.form.title`) }/>
        </Grid>
        <Grid item xs={ 12 } lg={ 6 }>
          <Field name={ `${member}.description` } type="text" component={ TextField } multiline label={ t(`descriptors.admin.c.form.description`) }/>
        </Grid>
      </Grid>
    );

    return (
      <div>
        <form onSubmit={ handleSubmit }>
          <Heading title={ pageTitle } level={ 2 }/>

          <Field
            required
            name="owner"
            editable={ !(initialValues.uuid && initialValues.version) }
            label={ t(`descriptors.admin.c.form.owner`) }
            placeholder={ t(`descriptors.admin.c.form.partner`) }
            onlyMine
            component={ SelectPartner }
            validate={ [Validators.required] }
          />
          <Field
            name="crop"
            label={ t(`descriptors.admin.c.form.crop`) }
            component={ CropSelector }
            single
          />
          <Field
            required
            name="title"
            label={ t(`descriptors.admin.c.form.descriptorTitle`) }
            placeholder={ t(`descriptors.admin.c.form.color`) }
            component={ TextField }
            validate={ [Validators.required] }
          />
          <Field
            required
            name="versionTag"
            label={ t(`descriptors.admin.c.form.version`) }
            placeholder="1.0"
            component={ TextField }
            validate={ [Validators.required] }
          />
          <Field
            name="publisher"
            label={ t(`descriptors.admin.c.form.publisher`) }
            placeholder="IPGRI"
            component={ TextField }
          />
          <Field
            required
            name="description"
            label={ t(`descriptors.admin.c.form.description`) }
            placeholder={ t(`descriptors.admin.c.form.fullDescription`) }
            component={ MarkdownField }
          />
          <Field
            name="key"
            label={ t(`descriptors.admin.c.form.key`) }
            component={ Toggle }
          />
          <Field
            required
            name="category"
            classes={ classes }
            component={ renderCategoryRadioGroup }
            t={ t }
            validate={ [Validators.required] }
          />

          <Field
            name="columnName"
            label={ t(`descriptors.admin.c.form.column`) }
            placeholder={ t(`descriptors.admin.c.form.columnDescription`) }
            component={ TextField }
          />
          <Field
            name="uom"
            label={ t(`descriptors.admin.c.form.unit`) }
            placeholder={ t(`descriptors.admin.c.form.unitPlaceholder`) }
            component={ TextField }
          />
          <Field
            name="bibliographicCitation"
            label={ t(`descriptors.admin.c.form.citation`) }
            placeholder={ t(`descriptors.admin.c.form.citationPlaceholder`) }
            component={ TextField }
            multiline
          />
          <Field
            required name="dataType"
            classes={ classes }
            component={ renderDataTypeRadioGroup }
            t={ t }
            validate={ [Validators.required] }
          />

          { this.allowsIntegerOnly() && (
            <Field
              name="integerOnly"
              label={ t(`descriptors.admin.c.form.integers`) }
              component={ Toggle }/>
          ) }
          { this.allowsNumericRange() && (
            <Field
              required={ this.requiresMinValue() }
              name="minValue"
              label={ t(`descriptors.admin.c.form.min`) }
              placeholder={ t(`descriptors.admin.c.form.minPlaceholder`) }
              component={ TextField }
              validate={ [Validators.decimalNumber] }
            />
          ) }
          { this.allowsNumericRange() && (
            <Field
              required={ this.requiresMaxValue() }
              name="maxValue"
              label={ t(`descriptors.admin.c.form.max`) }
              placeholder={ t(`descriptors.admin.c.form.maxPlaceholder`) }
              component={ TextField }
              validate={ [Validators.decimalNumber] }
            />
          ) }

          { this.withVocabulary() && <Heading title={ t(`descriptors.admin.c.form.term_plural`) } level={ 3 }/> }

          { (this.withVocabulary() && !currentVocabulary) && (
            <ItemsEditor name="terms" itemLabel={ t(`descriptors.admin.c.form.term`) } addItem={ onAddMember } removeItem={ onRemoveMember } component={ TermEditor }/>
          ) }

          { this.withVocabulary() && this.props.dataType === 'CODED' && (
            <Field
              required={ !this.props.currentTerms || this.props.currentTerms.length === 0 }
              name="vocabulary"
              label={ t(`descriptors.admin.c.form.vocabulary`) }
              component={ SelectVocabulary }
              owner={ currentOwner }
            />
          ) }

          <div>{ error && <strong>{ error }</strong> }</div>
          <Button variant="raised" type="submit" disabled={ submitting || invalid }>{ t('common:action.saveChanges') }</Button>
          { (initialValues && initialValues.id > 0 && !initialValues.published) &&
          <Button disabled={ anyTouched } type="button" onClick={ onPublish }>
            { t(`descriptors.admin.c.form.approvePublish`) }
          </Button>
          }
        </form>
      </div>
    );
  }

  private allowsNumericRange(): boolean {
    const { dataType } = this.props;
    return dataType === 'NUMERIC' || dataType === 'SCALE';
  }

  private allowsIntegerOnly(): boolean {
    const { dataType } = this.props;
    return dataType === 'NUMERIC' || dataType === 'SCALE';
  }

  private requiresMinValue(): boolean {
    const { dataType } = this.props;
    return dataType === 'SCALE';
  }

  private withVocabulary(): boolean {
    const { dataType } = this.props;
    return dataType === 'SCALE' || dataType === 'CODED';
  }
}

const selector = formValueSelector(DESCRIPTOR_FORM);

const mapStateToProps = (state, ownProps) => ({
  dataType: selector(state, 'dataType'),
  currentTerms: selector(state, 'terms'),
  currentOwner: selector(state, 'owner'),
  currentVocabulary: selector(state, 'vocabulary'),
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  setPageTitle,
}, dispatch);

export default translate()(connect(mapStateToProps, mapDispatchToProps)(
  withStyles(styles)(
    reduxForm({
      form: DESCRIPTOR_FORM,
      enableReinitialize: true,
    })
    (DescriptorForm),
  ),
));