aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/components/CreateWorkspaceForm.js
blob: aebe740d081b5db2da224fa41c9c8d68066571f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
import { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import { defineMessages, injectIntl } from 'react-intl';
import injectSheet from 'react-jss';

import { Input } from '../../../components/ui/input/index';
import Button from '../../../components/ui/button';
import Form from '../../../lib/Form';
import { required } from '../../../helpers/validation-helpers';
import { workspaceStore } from '../index';

const messages = defineMessages({
  submitButton: {
    id: 'settings.workspace.add.form.submitButton',
    defaultMessage: 'Create workspace',
  },
  name: {
    id: 'settings.workspace.add.form.name',
    defaultMessage: 'Name',
  },
});

const styles = {
  form: {
    display: 'flex',
  },
  input: {
    flexGrow: 1,
    marginRight: '10px',
  },
  submitButton: {
    height: 'inherit',
  },
};

class CreateWorkspaceForm extends Component {
  static propTypes = {
    classes: PropTypes.object.isRequired,
    isSubmitting: PropTypes.bool.isRequired,
    onSubmit: PropTypes.func.isRequired,
  };

  form = (() => {
    const { intl } = this.props;
    return new Form({
      fields: {
        name: {
          label: intl.formatMessage(messages.name),
          placeholder: intl.formatMessage(messages.name),
          value: '',
          validators: [required],
        },
      },
    });
  })();

  submitForm() {
    const { form } = this;
    form.submit({
      onSuccess: async f => {
        const { onSubmit } = this.props;
        const values = f.values();
        onSubmit(values);
      },
    });
  }

  render() {
    const { intl } = this.props;
    const { classes, isSubmitting } = this.props;
    const { form } = this;
    return (
      <div className={classes.form}>
        <Input
          className={classes.input}
          {...form.$('name').bind()}
          showLabel={false}
          onEnterKey={this.submitForm.bind(this, form)}
          focus={workspaceStore.isUserAllowedToUseFeature}
        />
        <Button
          className={`${classes.submitButton} franz-form__button`}
          type="submit"
          label={intl.formatMessage(messages.submitButton)}
          onClick={this.submitForm.bind(this, form)}
          busy={isSubmitting}
          buttonType={isSubmitting ? 'secondary' : 'primary'}
        />
      </div>
    );
  }
}

export default injectIntl(
  injectSheet(styles, { injectTheme: true })(observer(CreateWorkspaceForm)),
);