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

DATAP-1583 - company received date filter fixes #557

Closed
wants to merge 2 commits into from
Closed
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
50 changes: 25 additions & 25 deletions dist/ccdb5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ccdb5.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/components/Filters/CompanyReceivedFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dayjsCustomParseFormat from 'dayjs/plugin/customParseFormat';
import dayjsIsBetween from 'dayjs/plugin/isBetween';
import { formatDate } from '../../utils/formatDate';
import getIcon from '../iconMap';
import { datesChanged } from '../../reducers/query/querySlice';
import { companyReceivedDateChanged } from '../../reducers/query/querySlice';

dayjs.extend(dayjsCustomParseFormat);
dayjs.extend(dayjsIsBetween);
Expand Down Expand Up @@ -95,7 +95,7 @@ export const CompanyReceivedFilter = () => {
const isDateDifferent =
dateFrom !== _fromDate || dateThrough !== _throughDate;
if (isDateDifferent) {
dispatch(datesChanged(fieldName, _fromDate, _throughDate));
dispatch(companyReceivedDateChanged(_fromDate, _throughDate));
}
};

Expand Down
4 changes: 1 addition & 3 deletions src/components/Filters/CompanyReceivedFilter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('component::CompanyReceivedFilter', () => {
const user = userEvent.setup();
const companyReceivedDateUpdatedSpy = jest.spyOn(
filterActions,
'datesChanged',
'companyReceivedDateChanged',
);
it('Renders', async () => {
renderComponent({});
Expand All @@ -31,14 +31,12 @@ describe('component::CompanyReceivedFilter', () => {
await user.type(screen.getByLabelText('From'), '2018-09-03{Enter}');
// expect(screen.getByText())
expect(companyReceivedDateUpdatedSpy).toHaveBeenCalledWith(
'company_received',
'2018-09-03',
'',
);

await user.type(screen.getByLabelText('Through'), '2021-09-03{Enter}');
expect(companyReceivedDateUpdatedSpy).toHaveBeenCalledWith(
'company_received',
'2018-09-03',
'2021-09-03',
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Filters/DateFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const DateFilter = () => {
const isDateDifferent =
dateFrom !== _fromDate || dateThrough !== _throughDate;
if (dayjs(_throughDate).isAfter(_fromDate) && isDateDifferent) {
dispatch(datesChanged(fieldName, _fromDate, _throughDate));
dispatch(datesChanged(_fromDate, _throughDate));
}
};

Expand Down
1 change: 0 additions & 1 deletion src/reducers/filters/filtersSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ export function validatePer1000(state) {
: types.GEO_NORM_NONE;
}
export const {
companyReceivedDateUpdated,
dataNormalizationUpdated,
filterAdded,
filterRemoved,
Expand Down
60 changes: 39 additions & 21 deletions src/reducers/query/querySlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,36 @@ export const querySlice = createSlice({
};
},
},
datesChanged: {
// eslint-disable-next-line complexity
companyReceivedDateChanged: {
reducer: (state, action) => {
const { filterName } = action.payload;
let { maxDate, minDate } = action.payload;

const fields = [filterName + '_min', filterName + '_max'];

// // If maxDate AND minDate are falsy, early exit
// if (!maxDate && !minDate) {
// return state;
// }
minDate = dayjs(minDate).isValid()
? formatDate(dayjs(minDate).startOf('day'))
: null;

maxDate = dayjs(maxDate).isValid()
? formatDate(dayjs(maxDate).startOf('day'))
: null;
state.company_received_min = minDate;
state.company_received_max = maxDate;
},
prepare: (minDate, maxDate) => {
return {
payload: {
minDate,
maxDate,
},
meta: {
persist: PERSIST_SAVE_QUERY_STRING,
requery: REQUERY_ALWAYS,
},
};
},
},
datesChanged: {
reducer: (state, action) => {
let { maxDate, minDate } = action.payload;
minDate = dayjs(minDate).isValid()
? formatDate(dayjs(minDate).startOf('day'))
: null;
Expand All @@ -131,27 +148,24 @@ export const querySlice = createSlice({
: null;

const datesChanged =
state[fields[0]] !== minDate || state[fields[1]] !== maxDate;
state.date_received_min !== minDate ||
cdmh219 marked this conversation as resolved.
Show resolved Hide resolved
state.date_received_max !== maxDate;

const dateRange = calculateDateRange(minDate, maxDate);

// only modify dateRange when we use the date filter, not company filter
if (filterName === 'date_received') {
if (dateRange && datesChanged) {
state.dateRange = dateRange;
} else {
delete state.dateRange;
}
if (dateRange && datesChanged) {
state.dateRange = dateRange;
} else {
delete state.dateRange;
}

state[fields[0]] = minDate || state[fields[0]];
state[fields[1]] = maxDate || state[fields[1]];
state.date_received_min = minDate || state.date_received_min;
state.date_received_max = maxDate || state.date_received_max;
validateDateInterval(state);
},
prepare: (filterName, minDate, maxDate) => {
prepare: (minDate, maxDate) => {
return {
payload: {
filterName,
minDate,
maxDate,
},
Expand Down Expand Up @@ -287,6 +301,8 @@ export const querySlice = createSlice({
builder
.addCase('filters/filtersCleared', (state) => {
state.dateRange = 'All';
state.company_received_max = '';
state.company_received_min = '';
state.date_received_min = minDate;
state.date_received_max = maxDate;
})
Expand Down Expand Up @@ -337,6 +353,7 @@ export const querySlice = createSlice({
})
.addMatcher(
isAnyOf(
companyReceivedDateChanged,
datesChanged,
dateIntervalChanged,
dateRangeChanged,
Expand Down Expand Up @@ -608,6 +625,7 @@ export function clearPager(state) {
}

export const {
companyReceivedDateChanged,
datesChanged,
dateRangeChanged,
dateIntervalChanged,
Expand Down
30 changes: 24 additions & 6 deletions src/reducers/query/querySlice.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import target, {
searchFieldChanged,
dateIntervalChanged,
trendsDateWarningDismissed,
companyReceivedDateChanged,
} from './querySlice';
import * as types from '../../constants';
import dayjs from 'dayjs';
Expand Down Expand Up @@ -401,9 +402,28 @@ describe('reducer:query', () => {
});

describe('Dates', () => {
describe('companyReceivedDate actions', () => {
beforeEach(() => {
result = null;
});

it('adds the dates', () => {
const testState = { ...queryState };
expect(
target(
testState,
companyReceivedDateChanged('2011-12-20', '2014-10-09'),
),
).toEqual({
...testState,
company_received_min: '2011-12-20',
company_received_max: '2014-10-09',
});
});
});

describe('datesChanged actions', () => {
let result;
const filterName = 'date_received';
const minDate = new Date(2001, 0, 30);
const maxDate = new Date(2013, 1, 3);
beforeEach(() => {
Expand All @@ -413,9 +433,7 @@ describe('reducer:query', () => {
it('adds the dates', () => {
const testState = { ...queryState };
delete testState.dateRange;
expect(
target(testState, datesChanged(filterName, minDate, maxDate)),
).toEqual({
expect(target(testState, datesChanged(minDate, maxDate))).toEqual({
...testState,
breakPoints: {},
date_received_min: '2001-01-30',
Expand All @@ -434,7 +452,7 @@ describe('reducer:query', () => {
date_received_max: maxDate,
dateRange: '1y',
},
datesChanged(filterName, minDate, maxDate),
datesChanged(minDate, maxDate),
);
expect(result.dateRange).toBeFalsy();
});
Expand All @@ -444,7 +462,7 @@ describe('reducer:query', () => {
// today's date
const max = dayjs(startOfToday());
const min = new Date(dayjs(max).subtract(3, 'months'));
result = target({ ...queryState }, datesChanged(filterName, min, max));
result = target({ ...queryState }, datesChanged(min, max));
expect(result.dateRange).toEqual('3m');
});
});
Expand Down