about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/composer/upload_form/item/index.js
blob: ec67b8ef80d9d30a61ad3b108010d464f9ac1c91 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//  Package imports.
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';
import {
  FormattedMessage,
  defineMessages,
} from 'react-intl';
import spring from 'react-motion/lib/spring';

//  Components.
import IconButton from 'flavours/glitch/components/icon_button';

//  Utils.
import Motion from 'flavours/glitch/util/optional_motion';
import { assignHandlers } from 'flavours/glitch/util/react_helpers';

//  Messages.
const messages = defineMessages({
  undo: {
    defaultMessage: 'Undo',
    id: 'upload_form.undo',
  },
  description: {
    defaultMessage: 'Describe for the visually impaired',
    id: 'upload_form.description',
  },
});

//  Handlers.
const handlers = {

  //  On blur, we save the description for the media item.
  handleBlur () {
    const {
      id,
      onChangeDescription,
    } = this.props;
    const { dirtyDescription } = this.state;
    if (id && onChangeDescription && dirtyDescription !== null) {
      this.setState({
        dirtyDescription: null,
        focused: false,
      });
      onChangeDescription(id, dirtyDescription);
    }
  },

  //  When the value of our description changes, we store it in the
  //  temp value `dirtyDescription` in our state.
  handleChange ({ target: { value } }) {
    this.setState({ dirtyDescription: value });
  },

  //  Records focus on the media item.
  handleFocus () {
    this.setState({ focused: true });
  },

  //  Records the start of a hover over the media item.
  handleMouseEnter () {
    this.setState({ hovered: true });
  },

  //  Records the end of a hover over the media item.
  handleMouseLeave () {
    this.setState({ hovered: false });
  },

  //  Removes the media item.
  handleRemove () {
    const {
      id,
      onRemove,
    } = this.props;
    if (id && onRemove) {
      onRemove(id);
    }
  },
};

//  The component.
export default class ComposerUploadFormItem extends React.PureComponent {

  //  Constructor.
  constructor (props) {
    super(props);
    assignHandlers(this, handlers);
    this.state = {
      hovered: false,
      focused: false,
      dirtyDescription: null,
    };
  }

  //  Rendering.
  render () {
    const {
      handleBlur,
      handleChange,
      handleFocus,
      handleMouseEnter,
      handleMouseLeave,
      handleRemove,
    } = this.handlers;
    const {
      description,
      intl,
      preview,
    } = this.props;
    const {
      focused,
      hovered,
      dirtyDescription,
    } = this.state;
    const computedClass = classNames('composer--upload_form--item', { active: hovered || focused });

    //  The result.
    return (
      <div
        className={computedClass}
        onMouseEnter={handleMouseEnter}
        onMouseLeave={handleMouseLeave}
      >
        <Motion
          defaultStyle={{ scale: 0.8 }}
          style={{
            scale: spring(1, {
              stiffness: 180,
              damping: 12,
            }),
          }}
        >
          {({ scale }) => (
            <div
              style={{
                transform: `scale(${scale})`,
                backgroundImage: preview ? `url(${preview})` : null,
              }}
            >
              <IconButton
                className='close'
                icon='times'
                onClick={handleRemove}
                size={36}
                title={intl.formatMessage(messages.undo)}
              />
              <label>
                <span style={{ display: 'none' }}><FormattedMessage {...messages.description} /></span>
                <input
                  maxLength={420}
                  onBlur={handleBlur}
                  onChange={handleChange}
                  onFocus={handleFocus}
                  placeholder={intl.formatMessage(messages.description)}
                  type='text'
                  value={dirtyDescription || description || ''}
                />
              </label>
            </div>
          )}
        </Motion>
      </div>
    );
  }

}

//  Props.
ComposerUploadFormItem.propTypes = {
  description: PropTypes.string,
  id: PropTypes.string,
  intl: PropTypes.object.isRequired,
  onChangeDescription: PropTypes.func,
  onRemove: PropTypes.func,
  preview: PropTypes.string,
};