Commit 2845dad2 authored by Filipa Lacerda's avatar Filipa Lacerda

Find last note created by current user through vue instead of querying the DOM

parent 7a251207
<script> <script>
/* global Flash */ /* global Flash */
import { mapActions } from 'vuex'; import { mapActions, mapGetters } from 'vuex';
import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue'; import userAvatarLink from '../../vue_shared/components/user_avatar/user_avatar_link.vue';
import markdownField from '../../vue_shared/components/markdown/field.vue'; import markdownField from '../../vue_shared/components/markdown/field.vue';
import issueNoteSignedOutWidget from './issue_note_signed_out_widget.vue'; import issueNoteSignedOutWidget from './issue_note_signed_out_widget.vue';
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
issueNoteSignedOutWidget, issueNoteSignedOutWidget,
}, },
computed: { computed: {
...mapGetters([
'getCurrentUserLastNote',
]),
isLoggedIn() { isLoggedIn() {
return window.gon.current_user_id; return window.gon.current_user_id;
}, },
...@@ -126,13 +129,13 @@ ...@@ -126,13 +129,13 @@
setNoteType(type) { setNoteType(type) {
this.noteType = type; this.noteType = type;
}, },
editMyLastNote() { editCurrentUserLastNote() {
if (this.note === '') { if (this.note === '') {
const myLastNoteId = $('.js-my-note').last().attr('id'); const lastNote = this.getCurrentUserLastNote(window.gon.current_user_id);
debugger; console.log(lastNote)
if (myLastNoteId) { if (lastNote) {
eventHub.$emit('enterEditMode', { eventHub.$emit('enterEditMode', {
noteId: parseInt(myLastNoteId.replace('note_', ''), 10), noteId: lastNote.id,
}); });
} }
} }
...@@ -185,7 +188,7 @@ ...@@ -185,7 +188,7 @@
ref="textarea" ref="textarea"
slot="textarea" slot="textarea"
placeholder="Write a comment or drag your files here..." placeholder="Write a comment or drag your files here..."
@keydown.up="editMyLastNote" @keydown.up="editCurrentUserLastNote()"
@keydown.meta.enter="handleSave()"> @keydown.meta.enter="handleSave()">
</textarea> </textarea>
</markdown-field> </markdown-field>
......
...@@ -164,8 +164,9 @@ ...@@ -164,8 +164,9 @@
<issue-note-form <issue-note-form
v-if="isReplying" v-if="isReplying"
saveButtonTitle="Comment" saveButtonTitle="Comment"
:update-handler="saveReply" :discussion="note"
:cancel-handler="cancelReplyForm" @handleFormUpdate="saveReply"
@cancelFormEdition="cancelReplyForm"
ref="noteForm" ref="noteForm"
/> />
<issue-note-signed-out-widget v-if="!canReply" /> <issue-note-signed-out-widget v-if="!canReply" />
......
...@@ -118,7 +118,8 @@ ...@@ -118,7 +118,8 @@
<li <li
class="note timeline-entry" class="note timeline-entry"
:id="noteAnchorId" :id="noteAnchorId"
:class="classNameBindings"> :class="classNameBindings"
:note-id="note.id">
<div class="timeline-entry-inner"> <div class="timeline-entry-inner">
<div class="timeline-icon"> <div class="timeline-icon">
<user-avatar-link <user-avatar-link
......
...@@ -19,14 +19,6 @@ ...@@ -19,14 +19,6 @@
required: false, required: false,
default: false, default: false,
}, },
formUpdateHandler: {
type: Function,
required: true,
},
formCancelHandler: {
type: Function,
required: true,
},
}, },
components: { components: {
issueNoteEditedText, issueNoteEditedText,
...@@ -93,7 +85,7 @@ ...@@ -93,7 +85,7 @@
v-if="note.last_edited_by" v-if="note.last_edited_by"
:edited-at="note.last_edited_at" :edited-at="note.last_edited_at"
:edited-by="note.last_edited_by" :edited-by="note.last_edited_by"
actionText="Edited" action-text="Edited"
/> />
<issue-note-awards-list <issue-note-awards-list
v-if="note.award_emoji.length" v-if="note.award_emoji.length"
......
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
required: false, required: false,
default: 'Save comment', default: 'Save comment',
}, },
discussion: {
type: Array,
required: false,
}
}, },
data() { data() {
const { getIssueData, getNotesData } = this.$store.getters; const { getIssueData, getNotesData } = this.$store.getters;
...@@ -44,9 +48,14 @@ ...@@ -44,9 +48,14 @@
}, },
editMyLastNote() { editMyLastNote() {
if (this.note === '') { if (this.note === '') {
// TODO: HANDLE THIS WITHOUTH JQUERY OR QUERYING THE DOM
// FIND the discussion we are in and the last comment on that discussion
const discussion = $(this.$el).closest('.discussion-notes'); const discussion = $(this.$el).closest('.discussion-notes');
const myLastNoteId = discussion.find('.js-my-note').last().attr('id'); const myLastNoteId = discussion.find('.js-my-note').last().attr('id');
debugger;
const lastNoteInDiscussion = this.$store.getters.getDiscussionLastNote(this.discussion);
if (myLastNoteId) { if (myLastNoteId) {
eventHub.$emit('enterEditMode', { eventHub.$emit('enterEditMode', {
noteId: parseInt(myLastNoteId.replace('note_', ''), 10), noteId: parseInt(myLastNoteId.replace('note_', ''), 10),
...@@ -116,7 +125,7 @@ ...@@ -116,7 +125,7 @@
{{saveButtonTitle}} {{saveButtonTitle}}
</button> </button>
<button <button
@click="cancelHandler" @click="cancelHandler()"
class="btn btn-nr btn-cancel note-edit-cancel" class="btn btn-nr btn-cancel note-edit-cancel"
type="button"> type="button">
Cancel Cancel
......
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
setNotesData: 'setNotesData', setNotesData: 'setNotesData',
setIssueData: 'setIssueData', setIssueData: 'setIssueData',
setUserData: 'setUserData', setUserData: 'setUserData',
setLastFetchedAt: 'setLastFetchedAt'
}), }),
getComponentName(note) { getComponentName(note) {
if (note.isPlaceholderNote) { if (note.isPlaceholderNote) {
...@@ -86,7 +87,10 @@ ...@@ -86,7 +87,10 @@
this.checkLocationHash(); this.checkLocationHash();
}); });
}) })
.catch(() => Flash('Something went wrong while fetching issue comments. Please try again.')); .catch((error) => {
console.log(error)
Flash('Something went wrong while fetching issue comments. Please try again.')
});
}, },
initPolling() { initPolling() {
this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt')); this.setLastFetchedAt(this.getNotesDataByProp('lastFetchedAt'));
...@@ -98,7 +102,10 @@ ...@@ -98,7 +102,10 @@
.then((res) => { .then((res) => {
this.setLastFetchedAt(res.lastFetchedAt); this.setLastFetchedAt(res.lastFetchedAt);
}) })
.catch(() => Flash('Something went wrong while fetching latest comments.')); .catch((error) =>{
console.log(error)
Flash('Something went wrong while fetching latest comments.')
} );
}, 15000); }, 15000);
}, },
bindEventHubListeners() { bindEventHubListeners() {
......
...@@ -21,3 +21,16 @@ export const notesById = (state) => { ...@@ -21,3 +21,16 @@ export const notesById = (state) => {
return notesByIdObject; return notesByIdObject;
}; };
const reverseNotes = array => array.slice(0).reverse();
const isLastNote = (note, userId) => !note.system && note.author.id === userId;
export const getCurrentUserLastNote = state => userId => reverseNotes(state.notes)
.reduce((acc, note) => {
acc.push(reverseNotes(note.notes).find(el => isLastNote(el, userId)));
return acc;
}, []).filter(el => el !== undefined)[0];
export const getDiscussionLastNote = state => (discussion, userId) => reverseNotes(discussion[0].notes)
.find(el => isLastNote(el, userId));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment