Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDESK-5827] fix: Search param selector returning incorrect data #1560

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/actions/tests/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ describe('actions.main', () => {
page: 1,
fulltext: '',
spikeState: 'draft',
eventsPlanningFilter: 'ALL_EVENTS_PLANNING',
filter_id: null,
}]);
done();
Expand Down
1 change: 0 additions & 1 deletion client/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function convertCommonParams(params: ISearchParams): Partial<ISearchAPIPa
return {
item_ids: arrayToString(params.item_ids),
name: params.name,
tz_offset: params.tz_offset,
full_text: params.full_text,
anpa_category: cvsToString(params.anpa_category),
subject: cvsToString(params.subject),
Expand Down
29 changes: 29 additions & 0 deletions client/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ export enum FILTER_TYPE {
COMBINED = 'combined',
}

export enum PLANNING_VIEW {
EVENTS = 'EVENTS',
PLANNING = 'PLANNING',
COMBINED = 'COMBINED',
}

export enum DATE_RANGE {
TODAY = 'today',
TOMORROW = 'tomorrow',
Expand Down Expand Up @@ -1171,6 +1177,28 @@ export interface IContentTemplate extends IBaseRestApiResponse {
};
}

interface IMainStateSearch<T> {
lastRequestParams: T;
fulltext?: string;
currentSearch?: T;
totalItems: number;
jumpInterval: JUMP_INTERVAL;
}

export interface IMainState {
previewId?: IEventItem['_id'] | IPlanningItem['_id'];
previewType?: IEventItem['type'] | IPlanningItem['type'];
loadingPreview: boolean;
filter?: PLANNING_VIEW;
loadingIndicator: boolean;
itemHistory: Array<any>;
search: {
EVENTS: IMainStateSearch<IEventSearchParams>;
PLANNING: IMainStateSearch<IPlanningSearchParams>;
COMBINED: IMainStateSearch<ICombinedSearchParams>;
};
}

export interface IAgendaState {
agendas: Array<IAgenda>;
currentPlanningId?: IPlanningItem['_id'];
Expand All @@ -1191,6 +1219,7 @@ export interface IEventState {
}

export interface IPlanningAppState {
main: IMainState;
agenda: IAgendaState;
events: IEventState;
}
Expand Down
1 change: 0 additions & 1 deletion client/selectors/eventsplanning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export const getEventsPlanningViewParams = createSelector(
advancedSearch: get(search, 'advancedSearch', {}),
spikeState: get(search, 'spikeState', SPIKED_STATE.NOT_SPIKED),
fulltext: fullTextParam,
eventsPlanningFilter: filterId || EVENTS_PLANNING.FILTER.ALL_EVENTS_PLANNING,
filter_id: filterId,
page: 1,
})
Expand Down
55 changes: 43 additions & 12 deletions client/selectors/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {createSelector} from 'reselect';
import {get, isEmpty, isBoolean} from 'lodash';
import {get, isEmpty, isBoolean, cloneDeep} from 'lodash';
import moment from 'moment';

import {appConfig} from 'appConfig';
Expand Down Expand Up @@ -112,18 +112,49 @@ export const fullText = createSelector(

export const isViewFiltered = createSelector(
[activeFilter, searchParams],
(filter, params) => {
const advancedSearch = get(params, `${filter}.currentSearch.advancedSearch`, {});
const spikedState = get(params, `${filter}.currentSearch.spikeState`, SPIKED_STATE.NOT_SPIKED);
const fullText = get(params, `${filter}.fulltext`, '');

if (spikedState !== SPIKED_STATE.NOT_SPIKED || !isEmpty(fullText)) {
return true;
}

return Object.keys(advancedSearch)
(filter, mainSearch) => {
// Clone the params so we aren't affecting the original
const params = cloneDeep(mainSearch[filter].currentSearch ?? {});
const advancedSearch = cloneDeep(params.advancedSearch ?? {});

// Remove fields that we don't want to calculate
const exclude = [
'filter_id',
'timezoneOffset',
'advancedSearch',
'itemIds',
'page',
'startOfWeek',
'sortField',
'sortOrder',
];

exclude.forEach((field) => {
delete params[field];
});

// Remove params that are the same as the defaults
const defaults = {
spikeState: 'draft',
maxResults: 50,
onlyFuture: true,
};

Object.keys(defaults).forEach((field) => {
if (params[field] === defaults[field]) {
delete params[field];
}
});

// Flatten all params into a single dictionary
const allParams = {
...params,
...advancedSearch,
};

return Object.keys(allParams)
.some((key) => {
const value = advancedSearch[key];
const value = allParams[key];

if (key === 'spikeState') {
return value !== SPIKED_STATE.NOT_SPIKED;
Expand Down
33 changes: 13 additions & 20 deletions client/selectors/search.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
import {MAIN} from '../constants';
import {createSelector} from 'reselect';
import {ICombinedSearchParams, IEventSearchParams, IPlanningSearchParams, ISearchParams} from '../interfaces';
import {IPlanningAppState, ISearchParams, PLANNING_VIEW} from '../interfaces';
import {planningParamsToSearchParams, eventParamsToSearchParams, combinedParamsToSearchParams} from '../utils/search';

export function activeFilter(state): string {
return state.main?.filter ?? MAIN.FILTERS.COMBINED;
export function activeFilter(state: IPlanningAppState): PLANNING_VIEW {
return state.main?.filter ?? PLANNING_VIEW.COMBINED;
}

interface IStoredSearchParams {
COMBINED: ICombinedSearchParams;
EVENTS: IEventSearchParams;
PLANNING: IPlanningSearchParams;
}

export function allSearchParams(state): IStoredSearchParams {
export function allSearchParams(state: IPlanningAppState): IPlanningAppState['main']['search'] {
return state.main?.search;
}

export const currentSearchParams = createSelector<
any,
string,
IStoredSearchParams,
IPlanningAppState,
PLANNING_VIEW,
IPlanningAppState['main']['search'],
ISearchParams
>(
[activeFilter, allSearchParams],
(currentFilter, params) => {
switch (currentFilter) {
case MAIN.FILTERS.EVENTS:
return eventParamsToSearchParams(params.EVENTS);
case MAIN.FILTERS.PLANNING:
return planningParamsToSearchParams(params.PLANNING);
case MAIN.FILTERS.COMBINED:
return combinedParamsToSearchParams(params.COMBINED);
case PLANNING_VIEW.EVENTS:
return eventParamsToSearchParams(params.EVENTS.currentSearch);
case PLANNING_VIEW.PLANNING:
return planningParamsToSearchParams(params.PLANNING.currentSearch);
case PLANNING_VIEW.COMBINED:
return combinedParamsToSearchParams(params.COMBINED.currentSearch);
}
}
);
8 changes: 4 additions & 4 deletions client/selectors/tests/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ describe('main selectors', () => {

it('if fulltext is set for events', () => {
state.main.filter = MAIN.FILTERS.EVENTS;
state.main.search.EVENTS.fulltext = 'test';
state.main.search.EVENTS.currentSearch.fulltext = 'test';
expect(selectors.main.isViewFiltered(state)).toBe(true);
});

it('if fulltext is set for planning', () => {
state.main.filter = MAIN.FILTERS.PLANNING;
state.main.search.PLANNING.fulltext = '';
state.main.search.PLANNING.currentSearch.fulltext = '';
expect(selectors.main.isViewFiltered(state)).toBe(false);
});

it('if fulltext is set for combined', () => {
state.main.filter = MAIN.FILTERS.COMBINED;
state.main.search.COMBINED.fulltext = 'test';
state.main.search.COMBINED.currentSearch.fulltext = 'test';
expect(selectors.main.isViewFiltered(state)).toBe(true);
});

Expand Down Expand Up @@ -96,4 +96,4 @@ describe('main selectors', () => {
expect(selectors.main.isViewFiltered(state)).toBe(true);
});
});
});
});