summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notes/stores
diff options
context:
space:
mode:
authorFilipa Lacerda <filipa@gitlab.com>2017-07-27 21:24:05 +0100
committerFilipa Lacerda <filipa@gitlab.com>2017-07-27 21:25:02 +0100
commitd34c620fae23597e0130474fe20883f0718ded58 (patch)
treeda89ba9fc2dcbc731fb3fd1e98dc25ad063455f8 /app/assets/javascripts/notes/stores
parent487ed06f444892abce47a6e21aaea79c9ca9c800 (diff)
downloadgitlab-ce-d34c620fae23597e0130474fe20883f0718ded58.tar.gz
[ci skip] Add issue data and notes data provided through haml to the store to stop querying the DOM everywhere
Diffstat (limited to 'app/assets/javascripts/notes/stores')
-rw-r--r--app/assets/javascripts/notes/stores/actions.js91
-rw-r--r--app/assets/javascripts/notes/stores/getters.js6
-rw-r--r--app/assets/javascripts/notes/stores/index.js5
-rw-r--r--app/assets/javascripts/notes/stores/mutation_types.js3
-rw-r--r--app/assets/javascripts/notes/stores/mutations.js12
5 files changed, 67 insertions, 50 deletions
diff --git a/app/assets/javascripts/notes/stores/actions.js b/app/assets/javascripts/notes/stores/actions.js
index 092049cb377..7f806fcd675 100644
--- a/app/assets/javascripts/notes/stores/actions.js
+++ b/app/assets/javascripts/notes/stores/actions.js
@@ -7,6 +7,13 @@ 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())
@@ -20,43 +27,31 @@ export const deleteNote = ({ commit }, note) => service
commit(types.DELETE_NOTE, note);
});
-export const updateNote = ({ commit }, data) => {
- const { endpoint, note } = data;
-
- return service
- .updateNote(endpoint, note)
- .then(res => res.json())
- .then((res) => {
- commit(types.UPDATE_NOTE, res);
- });
-};
-
-export const replyToDiscussion = ({ commit }, note) => {
- const { endpoint, data } = note;
-
- return service
- .replyToDiscussion(endpoint, data)
- .then(res => res.json())
- .then((res) => {
- commit(types.ADD_NEW_REPLY_TO_DISCUSSION, res);
+export const updateNote = ({ commit }, { endpoint, note }) => service
+ .updateNote(endpoint, note)
+ .then(res => res.json())
+ .then((res) => {
+ commit(types.UPDATE_NOTE, res);
+ });
- return 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);
-export const createNewNote = ({ commit }, note) => {
- const { endpoint, data } = note;
+ return res;
+ });
- return service
- .createNewNote(endpoint, data)
- .then(res => res.json())
- .then((res) => {
- if (!res.errors) {
- commit(types.ADD_NEW_NOTE, 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;
@@ -91,6 +86,7 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
if (hasQuickActions && Object.keys(errors).length) {
dispatch('poll');
+
$('.js-gfm-input').trigger('clear-commands-cache.atwho');
Flash('Commands applied', 'notice', $(noteData.flashContainer));
}
@@ -136,9 +132,7 @@ export const saveNote = ({ commit, dispatch }, noteData) => {
};
export const poll = ({ commit, state, getters }) => {
- const { notesPath } = $('.js-notes-wrapper')[0].dataset;
-
- return service.poll(`${notesPath}?full_data=1`, state.lastFetchedAt)
+ return service.poll(state.notesData.notesPath, state.lastFetchedAt)
.then(res => res.json())
.then((res) => {
if (res.notes.length) {
@@ -160,7 +154,6 @@ export const poll = ({ commit, state, getters }) => {
}
});
}
-
return res;
});
};
@@ -175,20 +168,24 @@ export const toggleAward = ({ commit, getters, dispatch }, data) => {
.then(() => {
commit(types.TOGGLE_AWARD, { awardName, note });
- if (!skipMutalityCheck && (awardName === 'thumbsup' || awardName === 'thumbsdown')) {
- const counterAward = awardName === 'thumbsup' ? 'thumbsdown' : 'thumbsup';
+ 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 amIAwarded = false;
+ let noteHasAward = false;
targetNote.award_emoji.forEach((a) => {
if (a.name === counterAward && a.user.id === window.gon.current_user_id) {
- amIAwarded = true;
+ noteHasAward = true;
}
});
- if (amIAwarded) {
- data.awardName = counterAward;
- data.skipMutalityCheck = true;
+ if (noteHasAward) {
+ Object.assign(data, { awardName: counterAward });
+ Object.assign(data, { kipMutalityCheck: true });
dispatch(types.TOGGLE_AWARD, data);
}
@@ -197,9 +194,7 @@ export const toggleAward = ({ commit, getters, dispatch }, data) => {
};
export const scrollToNoteIfNeeded = (context, el) => {
- const isInViewport = gl.utils.isInViewport(el[0]);
-
- if (!isInViewport) {
+ if (!gl.utils.isInViewport(el[0])) {
gl.utils.scrollToElement(el);
}
};
diff --git a/app/assets/javascripts/notes/stores/getters.js b/app/assets/javascripts/notes/stores/getters.js
index c3a9f0a5e89..2db3c3f02af 100644
--- a/app/assets/javascripts/notes/stores/getters.js
+++ b/app/assets/javascripts/notes/stores/getters.js
@@ -1,10 +1,12 @@
export const notes = state => state.notes;
-
export const targetNoteHash = state => state.targetNoteHash;
+export const getNotesDataByProp = state => prop => state.notesData[prop];
+export const getIssueDataByProp = state => prop => state.notesData[prop];
+export const getUserDataByProp = state => prop => state.notesData[prop];
export const notesById = (state) => {
const notesByIdObject = {};
-
+ // TODO: FILIPA: TRANSFORM INTO A REDUCE
state.notes.forEach((note) => {
note.notes.forEach((n) => {
notesByIdObject[n.id] = n;
diff --git a/app/assets/javascripts/notes/stores/index.js b/app/assets/javascripts/notes/stores/index.js
index edca63fae67..8e0c8531bbc 100644
--- a/app/assets/javascripts/notes/stores/index.js
+++ b/app/assets/javascripts/notes/stores/index.js
@@ -11,6 +11,11 @@ export default new Vuex.Store({
notes: [],
targetNoteHash: null,
lastFetchedAt: null,
+
+ // holds endpoints and permissions provided through haml
+ notesData: {},
+ userData: {},
+ issueData: {},
},
actions,
getters,
diff --git a/app/assets/javascripts/notes/stores/mutation_types.js b/app/assets/javascripts/notes/stores/mutation_types.js
index f84f26684ca..4eccc2af56e 100644
--- a/app/assets/javascripts/notes/stores/mutation_types.js
+++ b/app/assets/javascripts/notes/stores/mutation_types.js
@@ -2,6 +2,9 @@ export const ADD_NEW_NOTE = 'ADD_NEW_NOTE';
export const ADD_NEW_REPLY_TO_DISCUSSION = 'ADD_NEW_REPLY_TO_DISCUSSION';
export const DELETE_NOTE = 'DELETE_NOTE';
export const REMOVE_PLACEHOLDER_NOTES = 'REMOVE_PLACEHOLDER_NOTES';
+export const SET_NOTES_DATA = 'SET_NOTES_DATA';
+export const SET_ISSUE_DATA = 'SET_ISSUE_DATA';
+export const SET_USER_DATA = 'SET_USER_DATA';
export const SET_INITAL_NOTES = 'SET_INITIAL_NOTES';
export const SET_LAST_FETCHED_AT = 'SET_LAST_FETCHED_AT';
export const SET_TARGET_NOTE_HASH = 'SET_TARGET_NOTE_HASH';
diff --git a/app/assets/javascripts/notes/stores/mutations.js b/app/assets/javascripts/notes/stores/mutations.js
index bb2ed91e570..4af8b0e6b9d 100644
--- a/app/assets/javascripts/notes/stores/mutations.js
+++ b/app/assets/javascripts/notes/stores/mutations.js
@@ -58,6 +58,18 @@ export default {
}
},
+ [types.SET_NOTES_DATA](state, data) {
+ state.notesData = data;
+ },
+
+ [types.SET_ISSUE_DATA](state, data) {
+ state.issueData = data;
+ },
+
+ [types.SET_USER_DATA](state, data) {
+ state.userData = data;
+ },
+
[types.SET_INITAL_NOTES](state, notes) {
state.notes = notes;
},