Commit b689ddd9 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Clement Ho

Do not persist notes filter when auto-switching

Send a `persist_filter: false` param to backend when
opening links to notes and auto-switching to show
all notes
parent af2edf28
...@@ -61,7 +61,7 @@ export default { ...@@ -61,7 +61,7 @@ export default {
}, },
methods: { methods: {
...mapActions(['filterDiscussion', 'setCommentsDisabled', 'setTargetNoteHash']), ...mapActions(['filterDiscussion', 'setCommentsDisabled', 'setTargetNoteHash']),
selectFilter(value) { selectFilter(value, persistFilter = true) {
const filter = parseInt(value, 10); const filter = parseInt(value, 10);
// close dropdown // close dropdown
...@@ -69,7 +69,11 @@ export default { ...@@ -69,7 +69,11 @@ export default {
if (filter === this.currentValue) return; if (filter === this.currentValue) return;
this.currentValue = filter; this.currentValue = filter;
this.filterDiscussion({ path: this.getNotesDataByProp('discussionsPath'), filter }); this.filterDiscussion({
path: this.getNotesDataByProp('discussionsPath'),
filter,
persistFilter,
});
this.toggleCommentsForm(); this.toggleCommentsForm();
}, },
toggleDropdown() { toggleDropdown() {
...@@ -85,7 +89,7 @@ export default { ...@@ -85,7 +89,7 @@ export default {
const hash = getLocationHash(); const hash = getLocationHash();
if (/^note_/.test(hash) && this.currentValue !== DISCUSSION_FILTERS_DEFAULT_VALUE) { if (/^note_/.test(hash) && this.currentValue !== DISCUSSION_FILTERS_DEFAULT_VALUE) {
this.selectFilter(this.defaultValue); this.selectFilter(this.defaultValue, false);
this.toggleDropdown(); // close dropdown this.toggleDropdown(); // close dropdown
this.setTargetNoteHash(hash); this.setTargetNoteHash(hash);
} }
......
...@@ -5,8 +5,11 @@ import * as constants from '../constants'; ...@@ -5,8 +5,11 @@ import * as constants from '../constants';
Vue.use(VueResource); Vue.use(VueResource);
export default { export default {
fetchDiscussions(endpoint, filter) { fetchDiscussions(endpoint, filter, persistFilter = true) {
const config = filter !== undefined ? { params: { notes_filter: filter } } : null; const config =
filter !== undefined
? { params: { notes_filter: filter, persist_filter: persistFilter } }
: null;
return Vue.http.get(endpoint, config); return Vue.http.get(endpoint, config);
}, },
replyToDiscussion(endpoint, data) { replyToDiscussion(endpoint, data) {
......
...@@ -46,9 +46,9 @@ export const setNotesFetchedState = ({ commit }, state) => ...@@ -46,9 +46,9 @@ export const setNotesFetchedState = ({ commit }, state) =>
export const toggleDiscussion = ({ commit }, data) => commit(types.TOGGLE_DISCUSSION, data); export const toggleDiscussion = ({ commit }, data) => commit(types.TOGGLE_DISCUSSION, data);
export const fetchDiscussions = ({ commit, dispatch }, { path, filter }) => export const fetchDiscussions = ({ commit, dispatch }, { path, filter, persistFilter }) =>
service service
.fetchDiscussions(path, filter) .fetchDiscussions(path, filter, persistFilter)
.then(res => res.json()) .then(res => res.json())
.then(discussions => { .then(discussions => {
commit(types.SET_INITIAL_DISCUSSIONS, discussions); commit(types.SET_INITIAL_DISCUSSIONS, discussions);
...@@ -411,9 +411,9 @@ export const setLoadingState = ({ commit }, data) => { ...@@ -411,9 +411,9 @@ export const setLoadingState = ({ commit }, data) => {
commit(types.SET_NOTES_LOADING_STATE, data); commit(types.SET_NOTES_LOADING_STATE, data);
}; };
export const filterDiscussion = ({ dispatch }, { path, filter }) => { export const filterDiscussion = ({ dispatch }, { path, filter, persistFilter }) => {
dispatch('setLoadingState', true); dispatch('setLoadingState', true);
dispatch('fetchDiscussions', { path, filter }) dispatch('fetchDiscussions', { path, filter, persistFilter })
.then(() => { .then(() => {
dispatch('setLoadingState', false); dispatch('setLoadingState', false);
dispatch('setNotesFetchedState', true); dispatch('setNotesFetchedState', true);
......
...@@ -127,7 +127,8 @@ module IssuableActions ...@@ -127,7 +127,8 @@ module IssuableActions
# GitLab Geo does not expect database UPDATE or INSERT statements to happen # GitLab Geo does not expect database UPDATE or INSERT statements to happen
# on GET requests. # on GET requests.
# This is just a fail-safe in case notes_filter is sent via GET request in GitLab Geo. # This is just a fail-safe in case notes_filter is sent via GET request in GitLab Geo.
if Gitlab::Database.read_only? # In some cases, we also force the filter to not be persisted with the `persist_filter` param
if Gitlab::Database.read_only? || params[:persist_filter] == 'false'
notes_filter_param || current_user&.notes_filter_for(issuable) notes_filter_param || current_user&.notes_filter_for(issuable)
else else
notes_filter = current_user&.set_notes_filter(notes_filter_param, issuable) || notes_filter_param notes_filter = current_user&.set_notes_filter(notes_filter_param, issuable) || notes_filter_param
......
---
title: Prevent discussion filter from persisting to `Show all activity` when opening
links to notes
merge_request: 31229
author:
type: fixed
...@@ -18,8 +18,13 @@ describe 'User opens link to comment', :js do ...@@ -18,8 +18,13 @@ describe 'User opens link to comment', :js do
visit Gitlab::UrlBuilder.build(note) visit Gitlab::UrlBuilder.build(note)
wait_for_requests
expect(page.find('#discussion-filter-dropdown')).to have_content('Show all activity') expect(page.find('#discussion-filter-dropdown')).to have_content('Show all activity')
expect(page).not_to have_content('Something went wrong while fetching comments') expect(page).not_to have_content('Something went wrong while fetching comments')
# Auto-switching to show all notes shouldn't be persisted
expect(user.reload.notes_filter_for(note.noteable)).to eq(UserPreference::NOTES_FILTERS[:only_activity])
end end
end end
......
...@@ -892,4 +892,31 @@ describe('Actions Notes Store', () => { ...@@ -892,4 +892,31 @@ describe('Actions Notes Store', () => {
}); });
}); });
}); });
describe('filterDiscussion', () => {
const path = 'some-discussion-path';
const filter = 0;
beforeEach(() => {
dispatch.and.returnValue(new Promise(() => {}));
});
it('fetches discussions with filter and persistFilter false', () => {
actions.filterDiscussion({ dispatch }, { path, filter, persistFilter: false });
expect(dispatch.calls.allArgs()).toEqual([
['setLoadingState', true],
['fetchDiscussions', { path, filter, persistFilter: false }],
]);
});
it('fetches discussions with filter and persistFilter true', () => {
actions.filterDiscussion({ dispatch }, { path, filter, persistFilter: true });
expect(dispatch.calls.allArgs()).toEqual([
['setLoadingState', true],
['fetchDiscussions', { path, filter, persistFilter: true }],
]);
});
});
}); });
...@@ -41,7 +41,15 @@ shared_examples 'issuable notes filter' do ...@@ -41,7 +41,15 @@ shared_examples 'issuable notes filter' do
get :discussions, params: params.merge(notes_filter: notes_filter) get :discussions, params: params.merge(notes_filter: notes_filter)
expect(user.reload.notes_filter_for(issuable)).to eq(0) expect(user.reload.notes_filter_for(issuable)).to eq(UserPreference::NOTES_FILTERS[:all_notes])
end
it 'does not set notes filter when persist_filter param is false' do
notes_filter = UserPreference::NOTES_FILTERS[:only_comments]
get :discussions, params: params.merge(notes_filter: notes_filter, persist_filter: false)
expect(user.reload.notes_filter_for(issuable)).to eq(UserPreference::NOTES_FILTERS[:all_notes])
end end
it 'returns only user comments' do it 'returns only user comments' do
......
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