summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/stores/getters.js
blob: 1f0c6af61560ace768778bc702d44caaf7061b02 (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
import _ from 'underscore';

export const notes = state => state.notes;
export const targetNoteHash = state => state.targetNoteHash;

export const getNotesData = state => state.notesData;
export const getNotesDataByProp = state => prop => state.notesData[prop];

export const getIssueData = state => state.issueData;
export const getIssueDataByProp = state => prop => state.issueData[prop];

export const getUserData = state => state.userData || {};
export const getUserDataByProp = state => prop => state.userData && state.userData[prop];

export const notesById = state => state.notes.reduce((acc, note) => {
  note.notes.every(n => Object.assign(acc, { [n.id]: n }));
  return acc;
}, {});

const reverseNotes = array => array.slice(0).reverse();
const isLastNote = (note, state) => !note.system &&
  state.userData && note.author &&
  note.author.id === state.userData.id;

export const getCurrentUserLastNote = state => _.flatten(
    reverseNotes(state.notes)
    .map(note => reverseNotes(note.notes)),
  ).find(el => isLastNote(el, state));

export const getDiscussionLastNote = state => discussion => reverseNotes(discussion.notes)
  .find(el => isLastNote(el, state));