debian-mirror-gitlab/app/assets/javascripts/notes/stores/getters.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import _ from 'underscore';
2018-11-08 19:23:39 +05:30
import * as constants from '../constants';
import { collapseSystemNotes } from './collapse_utils';
export const discussions = state => collapseSystemNotes(state.discussions);
2018-03-17 18:26:18 +05:30
export const targetNoteHash = state => state.targetNoteHash;
export const getNotesData = state => state.notesData;
2018-11-08 19:23:39 +05:30
export const isNotesFetched = state => state.isNotesFetched;
2018-03-17 18:26:18 +05:30
export const getNotesDataByProp = state => prop => state.notesData[prop];
export const getNoteableData = state => state.noteableData;
2018-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
export const getNoteableDataByProp = state => prop => state.noteableData[prop];
2018-11-08 19:23:39 +05:30
2018-03-27 19:54:05 +05:30
export const openState = state => state.noteableData.state;
2018-03-17 18:26:18 +05:30
export const getUserData = state => state.userData || {};
2018-11-08 19:23:39 +05:30
export const getUserDataByProp = state => prop => state.userData && state.userData[prop];
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export const notesById = state =>
2018-11-08 19:23:39 +05:30
state.discussions.reduce((acc, note) => {
2018-05-09 12:01:36 +05:30
note.notes.every(n => Object.assign(acc, { [n.id]: n }));
return acc;
}, {});
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
export const discussionsByLineCode = state =>
state.discussions.reduce((acc, note) => {
if (note.diff_discussion && note.line_code && note.resolvable) {
// For context about line notes: there might be multiple notes with the same line code
const items = acc[note.line_code] || [];
items.push(note);
Object.assign(acc, { [note.line_code]: items });
}
return acc;
}, {});
export const noteableType = state => {
const { ISSUE_NOTEABLE_TYPE, MERGE_REQUEST_NOTEABLE_TYPE, EPIC_NOTEABLE_TYPE } = constants;
if (state.noteableData.noteableType === EPIC_NOTEABLE_TYPE) {
return EPIC_NOTEABLE_TYPE;
}
return state.noteableData.merge_params ? MERGE_REQUEST_NOTEABLE_TYPE : ISSUE_NOTEABLE_TYPE;
};
2018-03-17 18:26:18 +05:30
const reverseNotes = array => array.slice(0).reverse();
2018-11-08 19:23:39 +05:30
2018-05-09 12:01:36 +05:30
const isLastNote = (note, state) =>
2018-11-08 19:23:39 +05:30
!note.system && state.userData && note.author && note.author.id === state.userData.id;
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export const getCurrentUserLastNote = state =>
2018-11-08 19:23:39 +05:30
_.flatten(reverseNotes(state.discussions).map(note => reverseNotes(note.notes))).find(el =>
isLastNote(el, state),
);
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
export const getDiscussionLastNote = state => discussion =>
reverseNotes(discussion.notes).find(el => isLastNote(el, state));
2018-03-27 19:54:05 +05:30
2018-05-09 12:01:36 +05:30
export const discussionCount = state => {
2018-11-08 19:23:39 +05:30
const filteredDiscussions = state.discussions.filter(n => !n.individual_note && n.resolvable);
2018-03-27 19:54:05 +05:30
2018-11-08 19:23:39 +05:30
return filteredDiscussions.length;
2018-03-27 19:54:05 +05:30
};
export const unresolvedDiscussions = (state, getters) => {
const resolvedMap = getters.resolvedDiscussionsById;
2018-11-08 19:23:39 +05:30
return state.discussions.filter(n => !n.individual_note && !resolvedMap[n.id]);
};
export const allDiscussions = (state, getters) => {
const resolved = getters.resolvedDiscussionsById;
const unresolved = getters.unresolvedDiscussions;
return Object.values(resolved).concat(unresolved);
2018-03-27 19:54:05 +05:30
};
2018-05-09 12:01:36 +05:30
export const resolvedDiscussionsById = state => {
2018-03-27 19:54:05 +05:30
const map = {};
2018-11-08 19:23:39 +05:30
state.discussions.filter(d => d.resolvable).forEach(n => {
2018-03-27 19:54:05 +05:30
if (n.notes) {
2018-11-08 19:23:39 +05:30
const resolved = n.notes.filter(note => note.resolvable).every(note => note.resolved);
2018-03-27 19:54:05 +05:30
if (resolved) {
map[n.id] = n;
}
}
});
return map;
};
export const resolvedDiscussionCount = (state, getters) => {
const resolvedMap = getters.resolvedDiscussionsById;
return Object.keys(resolvedMap).length;
};
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
export const discussionTabCounter = state => {
let all = [];
state.discussions.forEach(discussion => {
all = all.concat(discussion.notes.filter(note => !note.system && !note.placeholder));
});
return all.length;
};
2018-10-15 14:42:47 +05:30
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};