DescriptorListForm.tsx 4.34 KB
Newer Older
1 2 3 4 5 6
import * as React from 'react';
import { Field, reduxForm } from 'redux-form';
import { translate } from 'react-i18next';

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

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

  public render() {
17
    const {error, handleSubmit, initialValues, partners, crops, t} = this.props;
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
    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
          name="crop"
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
59
          formLabel={ t('descriptorlists.public.common.crop') }
60
          placeholder={ t('descriptorlists.public.c.form.maize') }
61 62 63 64
          component={ RadioSelection }
          options={ crops }
          valueField="shortName"
          renderOptionLabel={ (crop) => crop.name }
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
        />
        <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>
    );
  }
}

export default translate()(reduxForm({
  enableReinitialize: true, // https://redux-form.com/7.1.0/examples/initializefromstate/
  form: DESCRIPTORLIST_FORM,
  onSubmit: (values, dispatch) => dispatch(saveDescriptorList(values)),
})(DescriptorListForm));