VocabularyForm.tsx 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import * as React from 'react';
import { connect } from 'react-redux';
import { Field, reduxForm } from 'redux-form';
import { Link } from 'react-router-dom';
import { translate } from 'react-i18next';

import { log } from 'utilities/debug';

import { VOCABULARY_FORM } from 'vocabulary/constants';
import VocabularyTerm from 'model/vocabulary/VocabularyTerm';

import { TextField } from 'ui/common/text-field';
Matija Obreza's avatar
Matija Obreza committed
13
import { MarkdownField } from 'ui/common/markdown';
14 15 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
import Heading from 'ui/common/heading';
import Validators from 'utilities/Validators';

import ItemsEditor from 'ui/common/ItemsEditor';
import SelectPartner from 'partners/ui/c/SelectPartner';
import Card, { CardHeader, CardContent, CardActions } from 'ui/common/Card';

import Button from '@material-ui/core/Button';
import Grid from '@material-ui/core/Grid';

const TermEditor = (member, index, t, fields) => (
  <Grid container key={ index } justify="space-between" alignItems="flex-end">
    <Grid item xs={ 2 } md={ 2 } lg={ 1 }><Field required name={ `${member}.code` } type="text" component={ TextField } label={ t('vocabulary.common.code') } validate={ [Validators.required] }/></Grid>
    <Grid item xs={ 10 } md={ 10 } lg={ 5 }><Field required name={ `${member}.title` } type="text" component={ TextField } label={ t('common:label.title') } validate={ [Validators.required] }/></Grid>
    <Grid item xs={ 12 } lg={ 6 }><Field name={ `${member}.description` } type="text" component={ TextField } multiline label={ t('common:label.description') }/></Grid>
  </Grid>
);

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

  public render() {
    const { error, handleSubmit, initialValues, onDelete, partners, submitting, invalid, t } = this.props;

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

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

    if (!initialValues) {
      return null;
    }

    return (
      <form onSubmit={ handleSubmit }>
        <Card>
          <CardHeader title={ t('vocabulary.admin.c.form.details') }/>
          <CardContent>
            <Field
              required
              name="owner"
              editable={ !(initialValues.uuid && initialValues.version) }
              label="vocabulary.admin.c.form.owner"
              placeholder={ t('vocabulary.admin.c.form.ownerPlaceholder') }
              partners={ partners }
              component={ SelectPartner }
              validate={ [Validators.required] }
            />
            <Field
              required
              name="title"
              label={ t('vocabulary.admin.c.form.title') }
              placeholder={ t('vocabulary.admin.c.form.titlePlaceholder') }
              component={ TextField }
              validate={ [Validators.required] }
            />
            <Field
              required
              name="versionTag"
              label={ t('vocabulary.admin.c.form.versionTag') }
              placeholder="1.0"
              component={ TextField }
              validate={ [Validators.required] }
            />

            <Field
              name="publisher"
              label={ t('vocabulary.admin.c.form.publisher') }
              placeholder="IPGRI"
              component={ TextField }
            />
            <Field
              name="description"
              label={ t('common:label.description') }
              placeholder={ t('vocabulary.admin.c.form.descriptionPlaceholder') }
              component={ MarkdownField }
            />
            <Field
              name="url"
              label={ t('vocabulary.admin.c.form.url') }
              placeholder="https://"
              component={ TextField }
              validate={ [Validators.url] }
            />
            <Field
              name="termUrlPrefix"
              label={ t('vocabulary.admin.c.form.termUrlPrefix') }
              placeholder="???"
              component={ TextField }
            />
          </CardContent>
          <CardContent>
            <Heading title={ t('vocabulary.admin.c.form.term_plural') } level={ 3 }/>
            { initialValues.termCount > 50 ?
              <div className="error">{ t('vocabulary.admin.c.form.error') }</div>
              :
              <ItemsEditor
                name="terms"
                itemLabel={ t('vocabulary.admin.c.form.term') }
                addItem={ onAddMember }
                removeItem={ onRemoveMember }
                component={ TermEditor }/>
            }
          </CardContent>
          <CardActions>
            { error && (
              <div className="error">{ error }</div>
            ) }

            <Button variant="raised" type="submit" disabled={ submitting || invalid }>
              { t('common:action.saveChanges') }
            </Button>
            { (initialValues._permissions && initialValues._permissions.delete) &&
            <Button onClick={ onDelete } type="button">{ t('common:action.delete') }</Button>
            }
            <Link to="/vocabulary">
              <Button type="button">
                { t('common:action.backTo', { where: t('common:label.list') }) }
              </Button>
            </Link>
          </CardActions>
        </Card>
      </form>
    );
  }
}

export default translate()(connect((state) => ({}), null)(reduxForm({
  form: VOCABULARY_FORM,
  enableReinitialize: true,
})(VocabularyForm)));