Commit a3c549f0 authored by Martin Wortschack's avatar Martin Wortschack

Refactor setPath action

- Rename to setFit lters and store
properties separately instead of URL query string
parent b317d3a3
import ProductivityAnalyticsFilteredSearchTokenKeys from './productivity_analytics_filtered_search_token_keys'; import ProductivityAnalyticsFilteredSearchTokenKeys from './productivity_analytics_filtered_search_token_keys';
import FilteredSearchManager from '~/filtered_search/filtered_search_manager'; import FilteredSearchManager from '~/filtered_search/filtered_search_manager';
import { urlParamsToObject } from '~/lib/utils/common_utils';
import store from './store'; import store from './store';
export default class FilteredSearchProductivityAnalytics extends FilteredSearchManager { export default class FilteredSearchProductivityAnalytics extends FilteredSearchManager {
...@@ -19,6 +20,7 @@ export default class FilteredSearchProductivityAnalytics extends FilteredSearchM ...@@ -19,6 +20,7 @@ export default class FilteredSearchProductivityAnalytics extends FilteredSearchM
* Updates filters in productivity analytics store * Updates filters in productivity analytics store
*/ */
updateObject = path => { updateObject = path => {
store.dispatch('filters/setPath', path); const filters = urlParamsToObject(path);
store.dispatch('filters/setFilters', filters);
}; };
} }
...@@ -29,8 +29,15 @@ export const setProjectPath = ({ commit, dispatch }, projectPath) => { ...@@ -29,8 +29,15 @@ export const setProjectPath = ({ commit, dispatch }, projectPath) => {
}); });
}; };
export const setPath = ({ commit, dispatch }, path) => { export const setFilters = (
commit(types.SET_PATH, path); { commit, dispatch },
{ author_username, label_name, milestone_title },
) => {
commit(types.SET_FILTERS, {
authorUsername: author_username,
labelName: label_name,
milestoneTitle: milestone_title,
});
dispatch('charts/resetMainChartSelection', true, { root: true }); dispatch('charts/resetMainChartSelection', true, { root: true });
......
import dateFormat from 'dateformat'; import dateFormat from 'dateformat';
import { urlParamsToObject } from '~/lib/utils/common_utils';
import { getDateInPast, beginOfDayTime, endOfDayTime } from '~/lib/utils/datetime_utility'; import { getDateInPast, beginOfDayTime, endOfDayTime } from '~/lib/utils/datetime_utility';
import { chartKeys, scatterPlotAddonQueryDays } from '../../../constants'; import { chartKeys, scatterPlotAddonQueryDays } from '../../../constants';
import { dateFormats } from '../../../../shared/constants'; import { dateFormats } from '../../../../shared/constants';
...@@ -21,8 +20,15 @@ import { dateFormats } from '../../../../shared/constants'; ...@@ -21,8 +20,15 @@ import { dateFormats } from '../../../../shared/constants';
* *
*/ */
export const getCommonFilterParams = state => chartKey => { export const getCommonFilterParams = state => chartKey => {
const { groupNamespace, projectPath, filters, startDate, endDate } = state; const {
const { author_username, milestone_title, label_name } = urlParamsToObject(filters); groupNamespace,
projectPath,
startDate,
endDate,
authorUsername,
labelName,
milestoneTitle,
} = state;
// for the scatterplot we need to remove 30 days from the state's merged_at_after date // for the scatterplot we need to remove 30 days from the state's merged_at_after date
const mergedAtAfterDate = const mergedAtAfterDate =
...@@ -33,9 +39,9 @@ export const getCommonFilterParams = state => chartKey => { ...@@ -33,9 +39,9 @@ export const getCommonFilterParams = state => chartKey => {
return { return {
group_id: groupNamespace, group_id: groupNamespace,
project_id: projectPath, project_id: projectPath,
author_username, author_username: authorUsername,
milestone_title, milestone_title: milestoneTitle,
label_name, label_name: labelName,
merged_at_after: `${mergedAtAfterDate}${beginOfDayTime}`, merged_at_after: `${mergedAtAfterDate}${beginOfDayTime}`,
merged_at_before: `${dateFormat(endDate, dateFormats.isoDate)}${endOfDayTime}`, merged_at_before: `${dateFormat(endDate, dateFormats.isoDate)}${endOfDayTime}`,
}; };
......
export const SET_GROUP_NAMESPACE = 'SET_GROUP_NAMESPACE'; export const SET_GROUP_NAMESPACE = 'SET_GROUP_NAMESPACE';
export const SET_PROJECT_PATH = 'SET_PROJECT_PATH'; export const SET_PROJECT_PATH = 'SET_PROJECT_PATH';
export const SET_PATH = 'SET_PATH'; export const SET_FILTERS = 'SET_FILTERS';
export const SET_DATE_RANGE = 'SET_DATE_RANGE'; export const SET_DATE_RANGE = 'SET_DATE_RANGE';
...@@ -8,8 +8,10 @@ export default { ...@@ -8,8 +8,10 @@ export default {
[types.SET_PROJECT_PATH](state, projectPath) { [types.SET_PROJECT_PATH](state, projectPath) {
state.projectPath = projectPath; state.projectPath = projectPath;
}, },
[types.SET_PATH](state, path) { [types.SET_FILTERS](state, { authorUsername, labelName, milestoneTitle }) {
state.filters = path; state.authorUsername = authorUsername;
state.labelName = labelName;
state.milestoneTitle = milestoneTitle;
}, },
[types.SET_DATE_RANGE](state, { startDate, endDate }) { [types.SET_DATE_RANGE](state, { startDate, endDate }) {
state.startDate = startDate; state.startDate = startDate;
......
export default () => ({ export default () => ({
groupNamespace: null, groupNamespace: null,
projectPath: null, projectPath: null,
filters: '', authorUsername: null,
labelName: [],
milestoneTitle: null,
startDate: null, startDate: null,
endDate: null, endDate: null,
}); });
import testAction from 'helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import * as actions from 'ee/analytics/productivity_analytics/store/actions'; import * as actions from 'ee/analytics/productivity_analytics/store/actions';
import SET_ENDPOINT from 'ee/analytics/productivity_analytics/store/mutation_types'; import SET_ENDPOINT from 'ee/analytics/productivity_analytics/store/mutation_types';
import getInitialState from 'ee/analytics/productivity_analytics/store/modules/filters/state'; import getInitialState from 'ee/analytics/productivity_analytics/store/state';
describe('Productivity analytics actions', () => { describe('Productivity analytics actions', () => {
describe('setEndpoint', () => { describe('setEndpoint', () => {
......
import * as actions from 'ee/analytics/productivity_analytics/store/modules/filters/actions'; import * as actions from 'ee/analytics/productivity_analytics/store/modules/filters/actions';
import * as types from 'ee/analytics/productivity_analytics/store/modules/filters/mutation_types'; import * as types from 'ee/analytics/productivity_analytics/store/modules/filters/mutation_types';
import testAction from 'helpers/vuex_action_helper';
import { chartKeys } from 'ee/analytics/productivity_analytics/constants'; import { chartKeys } from 'ee/analytics/productivity_analytics/constants';
import getInitialState from 'ee/analytics/productivity_analytics/store/modules/filters/state';
describe('Productivity analytics filter actions', () => { describe('Productivity analytics filter actions', () => {
let store; let store;
...@@ -9,7 +11,6 @@ describe('Productivity analytics filter actions', () => { ...@@ -9,7 +11,6 @@ describe('Productivity analytics filter actions', () => {
const endDate = new Date(currentYear, 8, 7); const endDate = new Date(currentYear, 8, 7);
const groupNamespace = 'gitlab-org'; const groupNamespace = 'gitlab-org';
const projectPath = 'gitlab-org/gitlab-test'; const projectPath = 'gitlab-org/gitlab-test';
const path = 'author_username=root';
beforeEach(() => { beforeEach(() => {
store = { store = {
...@@ -82,12 +83,12 @@ describe('Productivity analytics filter actions', () => { ...@@ -82,12 +83,12 @@ describe('Productivity analytics filter actions', () => {
}); });
}); });
describe('setPath', () => { describe('setFilters', () => {
it('commits the SET_PATH mutation', done => { it('commits the SET_FILTERS mutation', done => {
actions actions
.setPath(store, path) .setFilters(store, { author_username: 'root' })
.then(() => { .then(() => {
expect(store.commit).toHaveBeenCalledWith(types.SET_PATH, path); expect(store.commit).toHaveBeenCalledWith(types.SET_FILTERS, { authorUsername: 'root' });
expect(store.dispatch.mock.calls[0]).toEqual([ expect(store.dispatch.mock.calls[0]).toEqual([
'charts/resetMainChartSelection', 'charts/resetMainChartSelection',
...@@ -117,9 +118,9 @@ describe('Productivity analytics filter actions', () => { ...@@ -117,9 +118,9 @@ describe('Productivity analytics filter actions', () => {
describe('setDateRange', () => { describe('setDateRange', () => {
it('commits the SET_DATE_RANGE mutation and fetches data by default', done => { it('commits the SET_DATE_RANGE mutation and fetches data by default', done => {
actions actions
.setPath(store, { startDate, endDate }) .setDateRange(store, { startDate, endDate })
.then(() => { .then(() => {
expect(store.commit).toHaveBeenCalledWith(types.SET_PATH, { startDate, endDate }); expect(store.commit).toHaveBeenCalledWith(types.SET_DATE_RANGE, { startDate, endDate });
expect(store.dispatch.mock.calls[0]).toEqual([ expect(store.dispatch.mock.calls[0]).toEqual([
'charts/resetMainChartSelection', 'charts/resetMainChartSelection',
...@@ -145,18 +146,19 @@ describe('Productivity analytics filter actions', () => { ...@@ -145,18 +146,19 @@ describe('Productivity analytics filter actions', () => {
.catch(done.fail); .catch(done.fail);
}); });
it("commits the SET_DATE_RANGE mutation and doesn't fetch data when fetchData=false", done => { it("commits the SET_DATE_RANGE mutation and doesn't fetch data when skipFetch=true", done =>
actions testAction(
.setPath(store, { fetchData: false, startDate, endDate }) actions.setDateRange,
.then(() => { { skipFetch: true, startDate, endDate },
expect(store.commit).toHaveBeenCalledWith(types.SET_PATH, { getInitialState(),
fetchData: false, [
startDate, {
endDate, type: types.SET_DATE_RANGE,
}); payload: { startDate, endDate },
}) },
.then(done) ],
.catch(done.fail); [],
}); done,
));
}); });
}); });
...@@ -17,7 +17,9 @@ describe('Productivity analytics filter getters', () => { ...@@ -17,7 +17,9 @@ describe('Productivity analytics filter getters', () => {
state = { state = {
groupNamespace: 'gitlab-org', groupNamespace: 'gitlab-org',
projectPath: 'gitlab-org/gitlab-test', projectPath: 'gitlab-org/gitlab-test',
filters: '?author_username=root&milestone_title=foo&label_name[]=labelxyz', authorUsername: 'root',
milestoneTitle: 'foo',
labelName: ['labelxyz'],
startDate, startDate,
endDate, endDate,
}; };
......
...@@ -27,12 +27,16 @@ describe('Productivity analytics filter mutations', () => { ...@@ -27,12 +27,16 @@ describe('Productivity analytics filter mutations', () => {
}); });
}); });
describe(types.SET_PATH, () => { describe(types.SET_FILTERS, () => {
it('sets the filters string', () => { it('sets the authorUsername, milestoneTitle and labelName', () => {
const path = '?author_username=root&milestone_title=foo&label_name[]=labelxyz'; const authorUsername = 'root';
mutations[types.SET_PATH](state, path); const labelName = ['my label', 'yet another label'];
const milestoneTitle = 'my milestone';
expect(state.filters).toBe(path); mutations[types.SET_FILTERS](state, { authorUsername, labelName, milestoneTitle });
expect(state.authorUsername).toBe(authorUsername);
expect(state.labelName).toBe(labelName);
expect(state.milestoneTitle).toBe(milestoneTitle);
}); });
}); });
......
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