Commit a8f27410 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Adds initialize action to prefill tokens

Adds an initialize action to the VSA
filter bar allowing tokens to be set on
component mount
parent 8c113249
......@@ -6,6 +6,29 @@ import MilestoneToken from '../../shared/components/tokens/milestone_token.vue';
import LabelToken from '../../shared/components/tokens/label_token.vue';
import UserToken from '../../shared/components/tokens/user_token.vue';
const prepareTokens = ({
selectedMilestone = null,
selectedAuthor = null,
selectedAssignees = [],
selectedLabels,
}) => {
const authorToken = selectedAuthor ? { type: 'author', value: { data: selectedAuthor } } : null;
const milestoneToken = selectedMilestone
? { type: 'milestone', value: { data: selectedMilestone } }
: null;
const assigneeTokens = selectedAssignees.length
? selectedAssignees.map(data => ({ type: 'assignees', value: { data } }))
: [];
const labelTokens = selectedLabels.length
? selectedLabels.map(data => ({ type: 'labels', value: { data } }))
: [];
let initTokens = [...assigneeTokens, ...labelTokens];
if (authorToken) initTokens = [...initTokens, authorToken];
if (milestoneToken) initTokens = [...initTokens, milestoneToken];
return initTokens;
};
export default {
name: 'FilterBar',
components: {
......@@ -33,6 +56,7 @@ export default {
authorsLoading: state => state.authors.isLoading,
assignees: state => state.assignees.data,
assigneesLoading: state => state.assignees.isLoading,
initialTokens: state => state.initialTokens,
}),
availableTokens() {
return [
......@@ -81,8 +105,15 @@ export default {
];
},
},
mounted() {
this.initializeTokens();
},
methods: {
...mapActions('filters', ['setFilters']),
initializeTokens() {
const preparedTokens = prepareTokens(this.initialTokens);
this.value = preparedTokens;
},
processFilters(filters) {
return filters.reduce((acc, token) => {
const { type, value } = token;
......
......@@ -93,3 +93,10 @@ export const setFilters = ({ dispatch, state }, params) => {
return dispatch('setSelectedFilters', nextFilters, { root: true });
};
export const initialize = ({ dispatch, commit }, initialFilters) => {
commit(types.INITIALIZE, initialFilters);
return Promise.resolve()
.then(() => dispatch('setPaths', initialFilters))
.then(() => dispatch('setFilters', initialFilters));
};
export const INITIALIZE = 'INITIALIZE';
export const SET_MILESTONES_PATH = 'SET_MILESTONES_PATH';
export const SET_LABELS_PATH = 'SET_LABELS_PATH';
......
import * as types from './mutation_types';
export default {
[types.INITIALIZE](
state,
{
selectedAuthor = null,
selectedMilestone = null,
selectedAssignees = [],
selectedLabels = [],
} = {},
) {
state.initialTokens = {
selectedAuthor,
selectedMilestone,
selectedAssignees,
selectedLabels,
};
},
[types.SET_MILESTONES_PATH](state, milestonesPath) {
state.milestonesPath = milestonesPath;
},
......
......@@ -17,4 +17,10 @@ export default () => ({
isLoading: false,
data: [],
},
initialTokens: {
selectedMilestone: null,
selectedAuthor: null,
selectedAssignees: [],
selectedLabels: [],
},
});
......@@ -25,6 +25,25 @@ describe('Filters actions', () => {
mock.restore();
});
describe('initialize', () => {
const initialData = {
milestonesPath,
labelsPath,
selectedAuthor: 'Mr cool',
selectedMilestone: 'NEXT',
};
it('initializes the state and dispatches setPaths and setFilters', () => {
return testAction(
actions.initialize,
initialData,
state,
[{ type: types.INITIALIZE, payload: initialData }],
[{ type: 'setPaths', payload: initialData }, { type: 'setFilters', payload: initialData }],
);
});
});
describe('setFilters', () => {
const nextFilters = {
selectedAuthor: 'Mr cool',
......
......@@ -27,4 +27,44 @@ describe('Filters mutations', () => {
expect(state[stateKey]).toEqual(value);
});
it.each`
mutation | rootKey | stateKey | value
${types.INITIALIZE} | ${'initialTokens'} | ${'selectedAuthor'} | ${null}
${types.INITIALIZE} | ${'initialTokens'} | ${'selectedMilestone'} | ${null}
${types.INITIALIZE} | ${'initialTokens'} | ${'selectedAssignees'} | ${[]}
${types.INITIALIZE} | ${'initialTokens'} | ${'selectedLabels'} | ${[]}
`('$mutation will set $stateKey with a given value', ({ mutation, rootKey, stateKey, value }) => {
mutations[mutation](state);
expect(state[rootKey][stateKey]).toEqual(value);
});
it.each`
mutation | rootKey | stateKey | value
${types.REQUEST_MILESTONES} | ${'milestones'} | ${'isLoading'} | ${true}
${types.RECEIVE_MILESTONES_SUCCESS} | ${'milestones'} | ${'isLoading'} | ${false}
${types.RECEIVE_MILESTONES_SUCCESS} | ${'milestones'} | ${'data'} | ${milestones}
${types.RECEIVE_MILESTONES_ERROR} | ${'milestones'} | ${'isLoading'} | ${false}
${types.RECEIVE_MILESTONES_ERROR} | ${'milestones'} | ${'data'} | ${[]}
${types.REQUEST_AUTHORS} | ${'authors'} | ${'isLoading'} | ${true}
${types.RECEIVE_AUTHORS_SUCCESS} | ${'authors'} | ${'isLoading'} | ${false}
${types.RECEIVE_AUTHORS_SUCCESS} | ${'authors'} | ${'data'} | ${users}
${types.RECEIVE_AUTHORS_ERROR} | ${'authors'} | ${'isLoading'} | ${false}
${types.RECEIVE_AUTHORS_ERROR} | ${'authors'} | ${'data'} | ${[]}
${types.REQUEST_LABELS} | ${'labels'} | ${'isLoading'} | ${true}
${types.RECEIVE_LABELS_SUCCESS} | ${'labels'} | ${'isLoading'} | ${false}
${types.RECEIVE_LABELS_SUCCESS} | ${'labels'} | ${'data'} | ${labels}
${types.RECEIVE_LABELS_ERROR} | ${'labels'} | ${'isLoading'} | ${false}
${types.RECEIVE_LABELS_ERROR} | ${'labels'} | ${'data'} | ${[]}
${types.REQUEST_ASSIGNEES} | ${'assignees'} | ${'isLoading'} | ${true}
${types.RECEIVE_ASSIGNEES_SUCCESS} | ${'assignees'} | ${'isLoading'} | ${false}
${types.RECEIVE_ASSIGNEES_SUCCESS} | ${'assignees'} | ${'data'} | ${users}
${types.RECEIVE_ASSIGNEES_ERROR} | ${'assignees'} | ${'isLoading'} | ${false}
${types.RECEIVE_ASSIGNEES_ERROR} | ${'assignees'} | ${'data'} | ${[]}
`('$mutation will set $stateKey with a given value', ({ mutation, rootKey, stateKey, value }) => {
mutations[mutation](state, value);
expect(state[rootKey][stateKey]).toEqual(value);
});
});
......@@ -9512,6 +9512,12 @@ msgstr ""
msgid "Failed to install."
msgstr ""
msgid "Failed to load assignees. Please try again."
msgstr ""
msgid "Failed to load authors. Please try again."
msgstr ""
msgid "Failed to load emoji list."
msgstr ""
......
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