DescriptorListForm.tsx 4.75 KB
Newer Older
1 2 3
import * as React from 'react';
import { Field, reduxForm } from 'redux-form';
import { translate } from 'react-i18next';
Oleksii Savran's avatar
Oleksii Savran committed
4
import withStyles from '@material-ui/core/styles/withStyles';
5 6 7

import { DESCRIPTORLIST_FORM } from 'descriptors/constants';
import { TextField } from 'ui/common/text-field';
Matija Obreza's avatar
Matija Obreza committed
8
import { MarkdownField } from 'ui/common/markdown';
9 10 11
import Authorize from 'ui/common/authorized/Authorize';
import SelectPartner from 'partners/ui/c/SelectPartner';
import Validators from 'utilities/Validators';
12
import RadioSelection from 'ui/common/forms/RadioSelection';
13 14
import { saveDescriptorList } from 'descriptorlists/actions/editor';

Oleksii Savran's avatar
Oleksii Savran committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* tslint:disable */
const styles = (theme) => ({
  cropRadio: {
    '&:nth-child(n)': {
      [theme.breakpoints.up('lg')]: {
        flexBasis: '16%',
      },
      [theme.breakpoints.up('xl')]: {
        flexBasis: '12%',
      },
    },
  }
});
/* tslint:enable */

30 31 32
class DescriptorListForm extends React.Component<any, any> {

  public render() {
Oleksii Savran's avatar
Oleksii Savran committed
33
    const {error, handleSubmit, initialValues, partners, crops, t, classes} = this.props;
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
    return (
      <form onSubmit={ handleSubmit } className="p-20 m-20 even-row">
        { initialValues && initialValues.version && <div>Version: { initialValues.version }</div> }

        <Field
          required
          name="owner"
          onlyMine
          label={ t('descriptorlists.public.c.form.selectPartner') }
          editable={ !(initialValues.uuid && initialValues.version) }
          component={ SelectPartner } partners={ partners }
          validate={ [Validators.required] }
        />

        <Authorize role="ROLE_ADMINISTRATOR">
          { !(initialValues.uuid && initialValues.version) &&
          <div>
            <div className="p-20 even-row">
              <p>
                { t('descriptorlists.public.c.form.you') } <b>{ t('descriptorlists.public.c.form.admin') } </b>
                { t('descriptorlists.public.c.form.get') } <code>UUID </code>
                { t('descriptorlists.public.c.form.ifYou') }.
                { t('descriptorlists.public.c.form.text') }:
              </p>
              <ul>
                <li><code>dc1d4e81-a6dd-4f03-b682-53a3a1383988 </code>
                  { t('descriptorlists.public.c.form.descriptors') }
                </li>
              </ul>
            </div>
            <Field name="uuid"
                   label="UUID"
                   placeholder="ihope-youk-noww-hatyouredoing"
                   component={ TextField }
            />
          </div>
          }
        </Authorize>

        <Field
Oleksii Savran's avatar
Oleksii Savran committed
74
          className={ classes.cropRadio }
75
          name="crop"
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
76
          formLabel={ t('descriptorlists.public.common.crop') }
77
          placeholder={ t('descriptorlists.public.c.form.maize') }
78 79 80 81
          component={ RadioSelection }
          options={ crops }
          valueField="shortName"
          renderOptionLabel={ (crop) => crop.name }
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
        />
        <Field
          required
          name="title"
          label={ t('descriptorlists.public.c.form.listTitle') }
          placeholder={ t('descriptorlists.public.c.form.titlePlaceholder') }
          basicMarkdown
          component={ MarkdownField }
          validate={ [Validators.required] }
        />
        <Field
          required
          disabled={ !!initialValues.uuid }
          name="versionTag"
          label={ t('descriptorlists.public.common.version') }
          placeholder="1.0"
          component={ TextField }
          validate={ [Validators.required] }
        />
        <Field
          name="description"
          label={ t('descriptorlists.public.c.form.description') }
          placeholder={ t('descriptorlists.public.c.form.descriptionPlaceholder') }
          component={ MarkdownField }
        />
        <Field
          name="url"
          label={ t('descriptorlists.public.c.form.urlToDef') }
          placeholder="https://"
          component={ TextField }
          validate={ [Validators.url] }
        />
        <Field
          name="publisher"
          label={ t('descriptorlists.public.c.form.publisher') }
          placeholder="IPGRI"
          component={ TextField }
        />
        <Field
          name="bibliographicCitation"
          label={ t('descriptorlists.public.c.form.citation') }
          placeholder={ t('descriptorlists.public.c.form.citationPlaceholder') }
          component={ MarkdownField }
          basicMarkdown
          multiline
        />

        <div>{ error && <strong>{ error }</strong> }</div>
      </form>
    );
  }
}

Oleksii Savran's avatar
Oleksii Savran committed
135 136 137 138 139 140 141
export default translate()(withStyles(styles)(
  reduxForm({
    enableReinitialize: true, // https://redux-form.com/7.1.0/examples/initializefromstate/
    form: DESCRIPTORLIST_FORM,
    onSubmit: (values, dispatch) => dispatch(saveDescriptorList(values)),
  })(DescriptorListForm)),
);