File size: 8,053 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import Modal from 'react-modal';
import compose from 'recompose/compose';
import { withRouter, type History } from 'react-router';
import { closeModal } from 'src/actions/modals';
import { addToastWithTimeout } from 'src/actions/toasts';
import deleteCommunityMutation from 'shared/graphql/mutations/community/deleteCommunity';
import type { DeleteCommunityType } from 'shared/graphql/mutations/community/deleteCommunity';
import deleteChannelMutation from 'shared/graphql/mutations/channel/deleteChannel';
import type { DeleteChannelType } from 'shared/graphql/mutations/channel/deleteChannel';
import deleteThreadMutation from 'shared/graphql/mutations/thread/deleteThread';
import type { DeleteThreadType } from 'shared/graphql/mutations/thread/deleteThread';
import deleteMessage from 'shared/graphql/mutations/message/deleteMessage';
import type { DeleteMessageType } from 'shared/graphql/mutations/message/deleteMessage';

import ModalContainer from '../modalContainer';
import { TextButton, WarnButton } from 'src/components/button';
import { modalStyles } from '../styles';
import { Actions, Message } from './style';
import type { Dispatch } from 'redux';

/*
  Generic component that should be used to confirm any 'delete' action.
  Takes modalProps as an object with four fields:

  entity => represents the table for lookup in the backend. Currently can
  be either 'thread', 'channel', or 'community'

  id => id of the entity to be deleted

  message => components can construct a custom confirmation message

  redirect => optional => string which represents the path a user should return
  too after deleting a thing (e.g. '/foo/bar')
*/
type State = {
  isLoading: boolean,
};

type Props = {
  dispatch: Dispatch<Object>,
  modalProps: {
    id: string,
    entity: string,
    redirect?: ?string,
    message?: ?string,
    buttonLabel?: string,
    extraProps?: any,
  },
  deleteMessage: Function,
  deleteCommunity: Function,
  deleteThread: Function,
  deleteChannel: Function,
  dispatch: Dispatch<Object>,
  isOpen: boolean,
  history: History,
};

export const deleteMessageWithToast = (
  dispatch: Function,
  deleteMessage: Function,
  id: string
) => {
  return deleteMessage(id)
    .then(({ data }: DeleteMessageType) => {
      const { deleteMessage } = data;
      if (deleteMessage) {
        dispatch(addToastWithTimeout('neutral', 'Message deleted.'));
      }
    })
    .catch(err => {
      dispatch(
        addToastWithTimeout(
          'error',
          `Sorry, we weren't able to delete this message. ${err.message}`
        )
      );
    });
};

class DeleteDoubleCheckModal extends React.Component<Props, State> {
  state = {
    isLoading: false,
  };

  close = () => {
    this.props.dispatch(closeModal());
  };

  triggerDelete = () => {
    const {
      history,
      modalProps: { id, entity, redirect, extraProps },
      dispatch,
    } = this.props;

    this.setState({
      isLoading: true,
    });

    switch (entity) {
      case 'message':
        return deleteMessageWithToast(
          this.props.dispatch,
          this.props.deleteMessage,
          id
        ).then(() => {
          this.setState({
            isLoading: false,
          });
          this.close();
        });
      case 'thread': {
        if (!extraProps) return;
        const { community } = extraProps.thread;
        return this.props
          .deleteThread(id)
          .then(({ data }: DeleteThreadType) => {
            const { deleteThread } = data;
            if (deleteThread) {
              history.replace(`/${community.slug}?tab=posts`);
              dispatch(addToastWithTimeout('neutral', 'Thread deleted.'));
              this.setState({
                isLoading: false,
              });
              this.close();
            }
            return;
          })
          .catch(err => {
            dispatch(
              addToastWithTimeout(
                'error',
                `Sorry, we weren't able to delete this thread. ${err.message}`
              )
            );
          });
      }
      case 'channel': {
        return this.props
          .deleteChannel(id)
          .then(({ data }: DeleteChannelType) => {
            const { deleteChannel } = data;
            if (deleteChannel) {
              // TODO: When we figure out the mutation reducers in apollo
              // client we can just history push and trust the store to update
              // eslint-disable-next-line
              window.location.href = redirect ? redirect : '/';
              // history.push(redirect ? redirect : '/');
              dispatch(addToastWithTimeout('neutral', 'Channel deleted.'));
              this.setState({
                isLoading: false,
              });
              this.close();
            }
            return;
          })
          .catch(err => {
            dispatch(
              addToastWithTimeout(
                'error',
                `Sorry, we weren't able to delete this channel. ${err.message}`
              )
            );
          });
      }
      case 'community': {
        return this.props
          .deleteCommunity(id)
          .then(({ data }: DeleteCommunityType) => {
            const { deleteCommunity } = data;
            if (deleteCommunity) {
              // TODO: When we figure out the mutation reducers in apollo
              // client we can just history push and trust the store to update
              // eslint-disable-next-line
              window.location.href = redirect ? redirect : '/';
              // history.push(redirect ? redirect : '/');
              dispatch(addToastWithTimeout('neutral', 'Community deleted.'));
              this.setState({
                isLoading: false,
              });
              this.close();
            }
            return;
          })
          .catch(err => {
            dispatch(
              addToastWithTimeout(
                'error',
                `Sorry, we weren't able to delete this community. ${
                  err.message
                }`
              )
            );
            this.setState({
              isLoading: false,
            });
          });
      }
      default: {
        this.setState({
          isLoading: false,
        });

        return dispatch(
          addToastWithTimeout(
            'error',
            'Unable to figure out what you wanted to delete. Whoops!'
          )
        );
      }
    }
  };

  render() {
    const {
      isOpen,
      modalProps: { message, buttonLabel },
    } = this.props;
    const styles = modalStyles();

    return (
      <Modal
        /* TODO(@mxstbr): Fix this */
        ariaHideApp={false}
        isOpen={isOpen}
        contentLabel={'Are you sure?'}
        onRequestClose={this.close}
        shouldCloseOnOverlayClick={true}
        style={styles}
        closeTimeoutMS={330}
      >
        {/*
          We pass the closeModal dispatch into the container to attach
          the action to the 'close' icon in the top right corner of all modals
        */}
        <ModalContainer title={'Are you sure?'} closeModal={this.close}>
          <Message>{message ? message : 'Are you sure?'}</Message>

          <Actions>
            <TextButton onClick={this.close}>Cancel</TextButton>
            <WarnButton
              loading={this.state.isLoading}
              onClick={this.triggerDelete}
              data-cy={'delete-button'}
            >
              {buttonLabel || 'Delete'}
            </WarnButton>
          </Actions>
        </ModalContainer>
      </Modal>
    );
  }
}

const DeleteDoubleCheckModalWithMutations = compose(
  deleteCommunityMutation,
  deleteChannelMutation,
  deleteThreadMutation,
  deleteMessage,
  withRouter
)(DeleteDoubleCheckModal);

const map = state => ({
  isOpen: state.modals.isOpen,
  modalProps: state.modals.modalProps,
});

// $FlowIssue
export default connect(map)(DeleteDoubleCheckModalWithMutations);