summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/stores/actions.js
blob: 7f806fcd67576ec98bbf436b6ab383d9e939bef0 (plain)
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
/* global Flash */

import * as types from './mutation_types';
import * as utils from './utils';
import * as constants from '../constants';
import service from '../services/issue_notes_service';
import loadAwardsHandler from '../../awards_handler';
import sidebarTimeTrackingEventHub from '../../sidebar/event_hub';

export const setNotesData = ({ commit }, data) => commit(types.SET_NOTES_DATA, data);
export const setIssueData = ({ commit }, data) => commit(types.SET_ISSUE_DATA, data);
export const setUserData = ({ commit }, data) => commit(types.SET_USER_DATA, data);
export const setLastFetchedAt = ({ commit }, data) => commit(types.SET_LAST_FETCHED_AT, data);
export const setInitialNotes = ({ commit }, data) => commit(types.SET_INITAL_NOTES, data);
export const setTargetNoteHash = ({ commit }, data) => commit(types.SET_TARGET_NOTE_HASH, data);

export const fetchNotes = ({ commit }, path) => service
  .fetchNotes(path)
  .then(res => res.json())
  .then((res) => {
    commit(types.SET_INITAL_NOTES, res);
  });

export const deleteNote = ({ commit }, note) => service
  .deleteNote(note.path)
  .then(() => {
    commit(types.DELETE_NOTE, note);
  });

export const updateNote = ({ commit }, { endpoint, note }) => service
  .updateNote(endpoint, note)
  .then(res => res.json())
  .then((res) => {
    commit(types.UPDATE_NOTE, res);
  });

export const replyToDiscussion = ({ commit }, { endpoint, data }) => service
  .replyToDiscussion(endpoint, data)
  .then(res => res.json())
  .then((res) => {
    commit(types.ADD_NEW_REPLY_TO_DISCUSSION, res);

    return res;
  });

export const createNewNote = ({ commit }, { endpoint, data }) => service
  .createNewNote(endpoint, data)
  .then(res => res.json())
  .then((res) => {
    if (!res.errors) {
      commit(types.ADD_NEW_NOTE, res);
    }
    return res;
  });

export const saveNote = ({ commit, dispatch }, noteData) => {
  const { note } = noteData.data.note;
  let placeholderText = note;
  const hasQuickActions = utils.hasQuickActions(placeholderText);
  const replyId = noteData.data.in_reply_to_discussion_id;
  const methodToDispatch = replyId ? 'replyToDiscussion' : 'createNewNote';

  if (hasQuickActions) {
    placeholderText = utils.stripQuickActions(placeholderText);
  }

  if (placeholderText.length) {
    commit(types.SHOW_PLACEHOLDER_NOTE, {
      noteBody: placeholderText,
      replyId,
    });
  }

  if (hasQuickActions) {
    commit(types.SHOW_PLACEHOLDER_NOTE, {
      isSystemNote: true,
      noteBody: utils.getQuickActionText(note),
      replyId,
    });
  }

  return dispatch(methodToDispatch, noteData)
    .then((res) => {
      const { errors } = res;
      const commandsChanges = res.commands_changes;

      if (hasQuickActions && Object.keys(errors).length) {
        dispatch('poll');

        $('.js-gfm-input').trigger('clear-commands-cache.atwho');
        Flash('Commands applied', 'notice', $(noteData.flashContainer));
      }

      if (commandsChanges) {
        if (commandsChanges.emoji_award) {
          const votesBlock = $('.js-awards-block').eq(0);

          loadAwardsHandler()
            .then((awardsHandler) => {
              awardsHandler.addAwardToEmojiBar(votesBlock, commandsChanges.emoji_award);
              awardsHandler.scrollToAwards();
            })
            .catch(() => {
              Flash(
                'Something went wrong while adding your award. Please try again.',
                null,
                $(noteData.flashContainer),
              );
            });
        }

        if (commandsChanges.spend_time != null || commandsChanges.time_estimate != null) {
          sidebarTimeTrackingEventHub.$emit('timeTrackingUpdated', res);
        }
      }

      if (errors && errors.commands_only) {
        Flash(errors.commands_only, 'notice', $(noteData.flashContainer));
      }
      commit(types.REMOVE_PLACEHOLDER_NOTES);

      return res;
    })
    .catch(() => {
      Flash(
        'Your comment could not be submitted! Please check your network connection and try again.',
        'alert',
        $(noteData.flashContainer),
      );
      commit(types.REMOVE_PLACEHOLDER_NOTES);
    });
};

export const poll = ({ commit, state, getters }) => {
  return service.poll(state.notesData.notesPath, state.lastFetchedAt)
    .then(res => res.json())
    .then((res) => {
      if (res.notes.length) {
        const { notesById } = getters;

        res.notes.forEach((note) => {
          if (notesById[note.id]) {
            commit(types.UPDATE_NOTE, note);
          } else if (note.type === constants.DISCUSSION_NOTE) {
            const discussion = utils.findNoteObjectById(state.notes, note.discussion_id);

            if (discussion) {
              commit(types.ADD_NEW_REPLY_TO_DISCUSSION, note);
            } else {
              commit(types.ADD_NEW_NOTE, note);
            }
          } else {
            commit(types.ADD_NEW_NOTE, note);
          }
        });
      }
      return res;
    });
};

export const toggleAward = ({ commit, getters, dispatch }, data) => {
  const { endpoint, awardName, noteId, skipMutalityCheck } = data;
  const note = getters.notesById[noteId];

  return service
    .toggleAward(endpoint, { name: awardName })
    .then(res => res.json())
    .then(() => {
      commit(types.TOGGLE_AWARD, { awardName, note });

      if (!skipMutalityCheck &&
        (awardName === constants.EMOJI_THUMBSUP || awardName === constants.EMOJI_THUMBSDOWN)) {
        const counterAward = awardName === constants.EMOJI_THUMBSUP ?
          constants.EMOJI_THUMBSDOWN :
          constants.EMOJI_THUMBSUP;

        const targetNote = getters.notesById[noteId];
        let noteHasAward = false;

        targetNote.award_emoji.forEach((a) => {
          if (a.name === counterAward && a.user.id === window.gon.current_user_id) {
            noteHasAward = true;
          }
        });

        if (noteHasAward) {
          Object.assign(data, { awardName: counterAward });
          Object.assign(data, { kipMutalityCheck: true });

          dispatch(types.TOGGLE_AWARD, data);
        }
      }
    });
};

export const scrollToNoteIfNeeded = (context, el) => {
  if (!gl.utils.isInViewport(el[0])) {
    gl.utils.scrollToElement(el);
  }
};