aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/components/EditWorkspaceForm.js
blob: 9e87b56dd8db93d15328a40bba3b312bd093ff85 (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
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import { Link } from 'react-router';
import { Input, Button } from '@meetfranz/forms';

import Form from '../../../lib/Form';
import Workspace from '../models/Workspace';

const messages = defineMessages({
  buttonDelete: {
    id: 'settings.workspace.form.buttonDelete',
    defaultMessage: '!!!Delete workspace',
  },
  buttonSave: {
    id: 'settings.workspace.form.buttonSave',
    defaultMessage: '!!!Save workspace',
  },
  name: {
    id: 'settings.workspace.form.name',
    defaultMessage: '!!!Name',
  },
  yourWorkspaces: {
    id: 'settings.workspace.form.yourWorkspaces',
    defaultMessage: '!!!Your workspaces',
  },
});

@observer
class EditWorkspaceForm extends Component {
  static contextTypes = {
    intl: intlShape,
  };

  static propTypes = {
    workspace: PropTypes.instanceOf(Workspace).isRequired,
    onSave: PropTypes.func.isRequired,
    onDelete: PropTypes.func.isRequired,
    isSaving: PropTypes.bool.isRequired,
    isDeleting: PropTypes.bool.isRequired,
  };

  prepareForm(workspace) {
    const { intl } = this.context;
    const config = {
      fields: {
        name: {
          label: intl.formatMessage(messages.name),
          placeholder: intl.formatMessage(messages.name),
          value: workspace.name,
        },
      },
    };
    return new Form(config);
  }

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

  render() {
    const { intl } = this.context;
    const {
      workspace,
      isDeleting,
      isSaving,
      onDelete,
    } = this.props;
    if (!workspace) return null;

    const form = this.prepareForm(workspace);

    return (
      <div className="settings__main">
        <div className="settings__header">
          <span className="settings__header-item">
            <Link to="/settings/workspaces">
              {intl.formatMessage(messages.yourWorkspaces)}
            </Link>
          </span>
          <span className="separator" />
          <span className="settings__header-item">
            {workspace.name}
          </span>
        </div>
        <div className="settings__body">
          <form onSubmit={e => this.submitForm(e, form)} id="form">
            <div className="workspace-name">
              <Input {...form.$('name').bind()} />
            </div>
          </form>
        </div>
        <div className="settings__controls">
          {/* ===== Delete Button ===== */}
          {isDeleting ? (
            <Button
              label={intl.formatMessage(messages.buttonDelete)}
              loaded={false}
              buttonType="secondary"
              className="settings__delete-button"
              disabled
            />
          ) : (
            <Button
              buttonType="danger"
              label={intl.formatMessage(messages.buttonDelete)}
              className="settings__delete-button"
              onClick={onDelete}
            />
          )}
          {/* ===== Save Button ===== */}
          {isSaving ? (
            <Button
              type="submit"
              label={intl.formatMessage(messages.buttonSave)}
              loaded={!isSaving}
              buttonType="secondary"
              disabled
            />
          ) : (
            <Button
              type="submit"
              label={intl.formatMessage(messages.buttonSave)}
              htmlForm="form"
            />
          )}
        </div>
      </div>
    );
  }
}

export default EditWorkspaceForm;