about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/features/compose/containers/compose_form_container.js
blob: d12c98c01dbb43a6d1b23ca34a6a13c9823bf0cc (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
import { connect } from 'react-redux';
import { defineMessages, injectIntl } from 'react-intl';
import ComposeForm from '../components/compose_form';
import {
  changeCompose,
  changeComposeSpoilerText,
  changeComposeSpoilerness,
  changeComposeVisibility,
  clearComposeSuggestions,
  fetchComposeSuggestions,
  insertEmojiCompose,
  selectComposeSuggestion,
  submitCompose,
  uploadCompose,
} from 'flavours/glitch/actions/compose';
import {
  openModal,
} from 'flavours/glitch/actions/modal';
import { changeLocalSetting } from 'flavours/glitch/actions/local_settings';

import { privacyPreference } from 'flavours/glitch/utils/privacy_preference';

const messages = defineMessages({
  missingDescriptionMessage: {  id: 'confirmations.missing_media_description.message',
                                defaultMessage: 'At least one media attachment is lacking a description. Consider describing all media attachments for the visually impaired before sending your toot.' },
  missingDescriptionConfirm: {  id: 'confirmations.missing_media_description.confirm',
                                defaultMessage: 'Send anyway' },
  missingDescriptionEdit:    {  id: 'confirmations.missing_media_description.edit',
                                defaultMessage: 'Edit media' },
});

//  State mapping.
function mapStateToProps (state) {
  const spoilersAlwaysOn = state.getIn(['local_settings', 'always_show_spoilers_field']);
  const inReplyTo = state.getIn(['compose', 'in_reply_to']);
  const replyPrivacy = inReplyTo ? state.getIn(['statuses', inReplyTo, 'visibility']) : null;
  const sideArmBasePrivacy = state.getIn(['local_settings', 'side_arm']);
  const sideArmRestrictedPrivacy = replyPrivacy ? privacyPreference(replyPrivacy, sideArmBasePrivacy) : null;
  let sideArmPrivacy = null;
  switch (state.getIn(['local_settings', 'side_arm_reply_mode'])) {
    case 'copy':
      sideArmPrivacy = replyPrivacy;
      break;
    case 'restrict':
      sideArmPrivacy = sideArmRestrictedPrivacy;
      break;
  }
  sideArmPrivacy = sideArmPrivacy || sideArmBasePrivacy;
  return {
    advancedOptions: state.getIn(['compose', 'advanced_options']),
    focusDate: state.getIn(['compose', 'focusDate']),
    caretPosition: state.getIn(['compose', 'caretPosition']),
    isSubmitting: state.getIn(['compose', 'is_submitting']),
    isEditing: state.getIn(['compose', 'id']) !== null,
    isChangingUpload: state.getIn(['compose', 'is_changing_upload']),
    isUploading: state.getIn(['compose', 'is_uploading']),
    layout: state.getIn(['local_settings', 'layout']),
    media: state.getIn(['compose', 'media_attachments']),
    preselectDate: state.getIn(['compose', 'preselectDate']),
    privacy: state.getIn(['compose', 'privacy']),
    sideArm: sideArmPrivacy,
    sensitive: state.getIn(['compose', 'sensitive']),
    showSearch: state.getIn(['search', 'submitted']) && !state.getIn(['search', 'hidden']),
    spoiler: spoilersAlwaysOn || state.getIn(['compose', 'spoiler']),
    spoilerText: state.getIn(['compose', 'spoiler_text']),
    suggestions: state.getIn(['compose', 'suggestions']),
    text: state.getIn(['compose', 'text']),
    anyMedia: state.getIn(['compose', 'media_attachments']).size > 0,
    spoilersAlwaysOn: spoilersAlwaysOn,
    mediaDescriptionConfirmation: state.getIn(['local_settings', 'confirm_missing_media_description']),
    preselectOnReply: state.getIn(['local_settings', 'preselect_on_reply']),
    isInReply: state.getIn(['compose', 'in_reply_to']) !== null,
  };
};

//  Dispatch mapping.
const mapDispatchToProps = (dispatch, { intl }) => ({

  onChange(text) {
    dispatch(changeCompose(text));
  },

  onSubmit(routerHistory) {
    dispatch(submitCompose(routerHistory));
  },

  onClearSuggestions() {
    dispatch(clearComposeSuggestions());
  },

  onFetchSuggestions(token) {
    dispatch(fetchComposeSuggestions(token));
  },

  onSuggestionSelected(position, token, suggestion, path) {
    dispatch(selectComposeSuggestion(position, token, suggestion, path));
  },

  onChangeSpoilerText(text) {
    dispatch(changeComposeSpoilerText(text));
  },

  onPaste(files) {
    dispatch(uploadCompose(files));
  },

  onPickEmoji(position, emoji) {
    dispatch(insertEmojiCompose(position, emoji));
  },

  onChangeSpoilerness() {
    dispatch(changeComposeSpoilerness());
  },

  onChangeVisibility(value) {
    dispatch(changeComposeVisibility(value));
  },

  onMediaDescriptionConfirm(routerHistory, mediaId, overriddenVisibility = null) {
    dispatch(openModal('CONFIRM', {
      message: intl.formatMessage(messages.missingDescriptionMessage),
      confirm: intl.formatMessage(messages.missingDescriptionConfirm),
      onConfirm: () => {
        if (overriddenVisibility) {
          dispatch(changeComposeVisibility(overriddenVisibility));
        };
        dispatch(submitCompose(routerHistory));
      },
      secondary: intl.formatMessage(messages.missingDescriptionEdit),
      onSecondary: () => dispatch(openModal('FOCAL_POINT', { id: mediaId })),
      onDoNotAsk: () => dispatch(changeLocalSetting(['confirm_missing_media_description'], false)),
    }));
  },

});

export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ComposeForm));