mutations.js 4.32 KB
Newer Older
1 2
import * as utils from './utils';
import * as types from './mutation_types';
Filipa Lacerda's avatar
Filipa Lacerda committed
3
import * as constants from '../constants';
4 5 6 7

export default {
  [types.ADD_NEW_NOTE](state, note) {
    const { discussion_id, type } = note;
8 9 10 11 12 13 14 15 16 17 18 19 20
    const [exists] = state.notes.filter(n => n.id === note.discussion_id);

    if (!exists) {
      const noteData = {
        expanded: true,
        id: discussion_id,
        individual_note: !(type === constants.DISCUSSION_NOTE),
        notes: [note],
        reply_id: discussion_id,
      };

      state.notes.push(noteData);
    }
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
  },

  [types.ADD_NEW_REPLY_TO_DISCUSSION](state, note) {
    const noteObj = utils.findNoteObjectById(state.notes, note.discussion_id);

    if (noteObj) {
      noteObj.notes.push(note);
    }
  },

  [types.DELETE_NOTE](state, note) {
    const noteObj = utils.findNoteObjectById(state.notes, note.discussion_id);

    if (noteObj.individual_note) {
      state.notes.splice(state.notes.indexOf(noteObj), 1);
    } else {
      const comment = utils.findNoteObjectById(noteObj.notes, note.id);
      noteObj.notes.splice(noteObj.notes.indexOf(comment), 1);

      if (!noteObj.notes.length) {
        state.notes.splice(state.notes.indexOf(noteObj), 1);
      }
    }
  },

  [types.REMOVE_PLACEHOLDER_NOTES](state) {
    const { notes } = state;

    for (let i = notes.length - 1; i >= 0; i -= 1) {
      const note = notes[i];
      const children = note.notes;

      if (children.length && !note.individual_note) { // remove placeholder from discussions
        for (let j = children.length - 1; j >= 0; j -= 1) {
          if (children[j].isPlaceholderNote) {
            children.splice(j, 1);
          }
        }
      } else if (note.isPlaceholderNote) { // remove placeholders from state root
        notes.splice(i, 1);
      }
    }
  },

65
  [types.SET_NOTES_DATA](state, data) {
66
    Object.assign(state, { notesData: data });
67 68 69
  },

  [types.SET_ISSUE_DATA](state, data) {
70
    Object.assign(state, { issueData: data });
71 72 73
  },

  [types.SET_USER_DATA](state, data) {
74
    Object.assign(state, { userData: data });
75
  },
76
  [types.SET_INITIAL_NOTES](state, notesData) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    const notes = [];

    notesData.forEach((note) => {
      // To support legacy notes, should be very rare case.
      if (note.individual_note && note.notes.length > 1) {
        note.notes.forEach((n) => {
          const nn = Object.assign({}, note);
          nn.notes = [n]; // override notes array to only have one item to mimick individual_note
          notes.push(nn);
        });
      } else {
        notes.push(note);
      }
    });

    Object.assign(state, { notes });
93 94 95
  },

  [types.SET_LAST_FETCHED_AT](state, fetchedAt) {
96
    Object.assign(state, { lastFetchedAt: fetchedAt });
97 98 99
  },

  [types.SET_TARGET_NOTE_HASH](state, hash) {
100
    Object.assign(state, { targetNoteHash: hash });
101 102 103 104 105 106 107 108 109 110 111
  },

  [types.SHOW_PLACEHOLDER_NOTE](state, data) {
    let notesArr = state.notes;
    if (data.replyId) {
      notesArr = utils.findNoteObjectById(notesArr, data.replyId).notes;
    }

    notesArr.push({
      individual_note: true,
      isPlaceholderNote: true,
Filipa Lacerda's avatar
Filipa Lacerda committed
112
      placeholderType: data.isSystemNote ? constants.SYSTEM_NOTE : constants.NOTE,
113 114 115 116 117 118 119 120 121 122
      notes: [
        {
          body: data.noteBody,
        },
      ],
    });
  },

  [types.TOGGLE_AWARD](state, data) {
    const { awardName, note } = data;
123
    const { id, name, username } = state.userData;
124

125 126
    const hasEmojiAwardedByCurrentUser = note.award_emoji
      .filter(emoji => emoji.name === data.awardName && emoji.user.id === id);
127

128 129 130
    if (hasEmojiAwardedByCurrentUser.length) {
      // If current user has awarded this emoji, remove it.
      note.award_emoji.splice(note.award_emoji.indexOf(hasEmojiAwardedByCurrentUser[0]), 1);
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
    } else {
      note.award_emoji.push({
        name: awardName,
        user: { id, name, username },
      });
    }
  },

  [types.TOGGLE_DISCUSSION](state, { discussionId }) {
    const discussion = utils.findNoteObjectById(state.notes, discussionId);

    discussion.expanded = !discussion.expanded;
  },

  [types.UPDATE_NOTE](state, note) {
    const noteObj = utils.findNoteObjectById(state.notes, note.discussion_id);

    if (noteObj.individual_note) {
      noteObj.notes.splice(0, 1, note);
    } else {
      const comment = utils.findNoteObjectById(noteObj.notes, note.id);
      noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note);
    }
  },
};