Skip to content

Commit 446e53e

Browse files
committed
WEB-4460 use atomic state setting for tideDashboardFilters
1 parent cdf6af3 commit 446e53e

12 files changed

Lines changed: 74 additions & 50 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* global describe */
2+
/* global it */
3+
/* global expect */
4+
5+
import reducer from '@app/pages/clinicworkspace/TideDashboardV2/tideDashboardFiltersSlice';
6+
7+
describe('tideDashboardFiltersSlice', () => {
8+
const defaultFilters = { lastData: 7, patientTags: [], clinicSites: [], summaryPeriod: '14d' };
9+
const appliedFilters = { lastData: 30, patientTags: ['tag1'], clinicSites: ['site1'], summaryPeriod: '30d' };
10+
11+
describe('SELECT_CLINIC_SUCCESS', () => {
12+
it('should hydrate the filters carried on the clinic selection', () => {
13+
const persistedFilters = { lastData: 14, patientTags: ['tag2'], clinicSites: [], summaryPeriod: '7d' };
14+
15+
const state = reducer(appliedFilters, {
16+
type: 'SELECT_CLINIC_SUCCESS',
17+
payload: { clinicId: 'clinic123', tideDashboardFilters: persistedFilters },
18+
});
19+
20+
expect(state).toStrictEqual(persistedFilters);
21+
});
22+
23+
it('should reset to the default filters when the clinic selection carries none', () => {
24+
// Selecting a clinic with nothing persisted
25+
let state = reducer(appliedFilters, {
26+
type: 'SELECT_CLINIC_SUCCESS',
27+
payload: { clinicId: 'clinic123', tideDashboardFilters: undefined },
28+
});
29+
30+
expect(state).toStrictEqual(defaultFilters);
31+
32+
// Unsetting the clinic
33+
state = reducer(appliedFilters, {
34+
type: 'SELECT_CLINIC_SUCCESS',
35+
payload: { clinicId: null },
36+
});
37+
38+
expect(state).toStrictEqual(defaultFilters);
39+
});
40+
});
41+
});

__tests__/unit/app/redux/actions/async.test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ describe('Actions', () => {
130130
};
131131

132132
let expectedActions = [
133-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId } },
134-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: { lastData: 7, patientTags: ['load1'] } }, // loads from localState
133+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId, tideDashboardFilters: { lastData: 7, patientTags: ['load1'] } } }, // filters load from localState
135134
{ type: 'FETCH_CLINIC_PATIENT_COUNTS_REQUEST' },
136135
{ type: 'FETCH_CLINIC_PATIENT_COUNT_SETTINGS_REQUEST' },
137136
{ type: 'FETCH_CLINIC_PATIENT_COUNTS_SUCCESS', payload: { clinicId, patientCounts: countResults } },
@@ -169,8 +168,7 @@ describe('Actions', () => {
169168
};
170169

171170
let expectedActions = [
172-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId } },
173-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
171+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId, tideDashboardFilters: undefined } },
174172
{ type: 'SET_CLINIC_UI_DETAILS', payload: { clinicId, uiDetails: expectedUIDetails } },
175173
];
176174

@@ -207,8 +205,7 @@ describe('Actions', () => {
207205
settingsErr.status = 500;
208206

209207
let expectedActions = [
210-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId } },
211-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
208+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId, tideDashboardFilters: undefined } },
212209
{ type: 'FETCH_CLINIC_PATIENT_COUNTS_REQUEST' },
213210
{ type: 'FETCH_CLINIC_PATIENT_COUNT_SETTINGS_REQUEST' },
214211
{

app/pages/clinicworkspace/TideDashboardV2/tideDashboardFiltersSlice.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSlice } from '@reduxjs/toolkit';
2+
import { SELECT_CLINIC_SUCCESS } from '../../../redux/constants/actionTypes';
23

34
const getInitialState = () => ({
45
lastData: 7,
@@ -26,6 +27,11 @@ const tideDashboardFiltersSlice = createSlice({
2627
},
2728
resetTideDashboardFilters: () => getInitialState(),
2829
},
30+
extraReducers: (builder) => {
31+
builder.addCase(SELECT_CLINIC_SUCCESS, (_state, action) => {
32+
return action.payload?.tideDashboardFilters || getInitialState();
33+
});
34+
},
2935
});
3036

3137
export const {

app/redux/actions/async.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { clinicUIDetails } from '../../core/clinicUtils.js';
1919
import { getDismissedAltRangeBannerKey, isRangeWithNonStandardTarget } from '../../providers/AppBanner/appBannerHelpers.js';
2020
import { getGlycemicRangesPreset } from '../../core/glycemicRangesUtils.js';
2121
import { getTideDashboardFiltersKey, loadLocalState } from '../store/localStorage';
22-
import { setTideDashboardFilters } from '../../pages/clinicworkspace/TideDashboardV2/tideDashboardFiltersSlice';
2322

2423
// Exported as a mutable reference to allow location to be swapped in tests
2524
export const _win = { location: window.location };
@@ -3155,13 +3154,11 @@ export function fetchRpmReportPatients(api, clinicId, options) {
31553154
*/
31563155
export function selectClinic(api, clinicId) {
31573156
return (dispatch, getState) => {
3158-
dispatch(sync.selectClinicSuccess(clinicId));
3159-
31603157
const { blip: { clinics = {}, loggedInUserId } } = getState();
31613158

31623159
const tideDashboardFiltersKey = getTideDashboardFiltersKey(loggedInUserId, clinicId);
31633160
const tideDashboardFilters = loggedInUserId && clinicId ? loadLocalState(tideDashboardFiltersKey) : undefined;
3164-
dispatch(setTideDashboardFilters(tideDashboardFilters));
3161+
dispatch(sync.selectClinicSuccess(clinicId, tideDashboardFilters));
31653162

31663163
const clinic = clinics[clinicId];
31673164

app/redux/actions/sync.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,11 +2114,12 @@ export function getClinicsForClinicianFailure(error, apiError) {
21142114
};
21152115
}
21162116

2117-
export function selectClinicSuccess(clinicId) {
2117+
export function selectClinicSuccess(clinicId, tideDashboardFilters) {
21182118
return {
21192119
type: ActionTypes.SELECT_CLINIC_SUCCESS,
21202120
payload: {
2121-
clinicId
2121+
clinicId,
2122+
tideDashboardFilters,
21222123
},
21232124
};
21242125
}

test/unit/components/clinic/WorkspaceSwitcher.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,9 @@ describe('WorkspaceSwitcher', () => {
209209
type: 'SELECT_CLINIC_SUCCESS',
210210
payload: {
211211
clinicId: null,
212+
tideDashboardFilters: undefined,
212213
},
213214
},
214-
{
215-
type: 'tideDashboardFilters/setTideDashboardFilters',
216-
payload: undefined,
217-
},
218215
{
219216
type: '@@router/CALL_HISTORY_METHOD',
220217
payload: {
@@ -252,6 +249,7 @@ describe('WorkspaceSwitcher', () => {
252249
type: 'SELECT_CLINIC_SUCCESS',
253250
payload: {
254251
clinicId: 'clinicID456',
252+
tideDashboardFilters: undefined,
255253
},
256254
},
257255
{

test/unit/pages/clinicdetails.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ describe('ClinicDetails', () => {
474474
const expectedActions = [
475475
{ type: 'CREATE_CLINIC_REQUEST' },
476476
{ type: 'CREATE_CLINIC_SUCCESS', payload: { clinic: { id: 'newClinic123' } } },
477-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'newClinic123' } },
478-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
477+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'newClinic123', tideDashboardFilters: undefined } },
479478
{ type: 'GET_CLINICS_FOR_CLINICIAN_REQUEST' },
480479
{ type: 'GET_CLINICS_FOR_CLINICIAN_SUCCESS', payload: { clinics: [{ clinic: { id: 'newClinic123' } }], clinicianId: 'clinicianUserId123' } },
481480
{ type: 'FETCH_CLINIC_EHR_SETTINGS_REQUEST' },

test/unit/pages/workspaces/workspaces.test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ describe('Workspaces', () => {
310310
store.clearActions();
311311
fireEvent.click(button);
312312
expect(store.getActions()).to.eql([
313-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: null } },
314-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
313+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: null, tideDashboardFilters: undefined } },
315314
{
316315
type: '@@router/CALL_HISTORY_METHOD',
317316
payload: {
@@ -361,8 +360,7 @@ describe('Workspaces', () => {
361360
expect(store.getActions()).to.eql([
362361
{ type: 'SET_PATIENT_LIST_SEARCH_TEXT_INPUT', payload: { textInput: '' } },
363362
{ type: 'SET_IS_PATIENT_LIST_VISIBLE', payload: { isVisible: false } },
364-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicID456' } },
365-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
363+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicID456', tideDashboardFilters: undefined } },
366364
{ type: 'FETCH_CLINIC_PATIENT_COUNTS_REQUEST' },
367365
{ type: 'FETCH_CLINIC_PATIENT_COUNT_SETTINGS_REQUEST' },
368366
{

test/unit/redux/actions/async.test.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,8 +1202,7 @@ describe('Actions', () => {
12021202
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [{ inviteId: 'invite123' }] }},
12031203
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
12041204
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1205-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123' }},
1206-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1205+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123', tideDashboardFilters: undefined }},
12071206
{ type: 'LOGIN_SUCCESS', payload: { user } },
12081207
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: ['/some-dest', { selectedClinicId: 'clinic123' }] } }
12091208
];
@@ -1292,8 +1291,7 @@ describe('Actions', () => {
12921291
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
12931292
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
12941293
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1295-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicId123' } },
1296-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1294+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicId123', tideDashboardFilters: undefined } },
12971295
{ type: 'LOGIN_SUCCESS', payload: { user } },
12981296
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: [ '/clinic-details/migrate', { selectedClinicId: 'clinicId123' } ] } }
12991297
];
@@ -1335,8 +1333,7 @@ describe('Actions', () => {
13351333
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
13361334
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
13371335
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1338-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicId123' } },
1339-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1336+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinicId123', tideDashboardFilters: undefined } },
13401337
{ type: 'LOGIN_SUCCESS', payload: { user } },
13411338
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: [ '/clinic-details/migrate', { selectedClinicId: 'clinicId123' } ] } }
13421339
];
@@ -1471,8 +1468,7 @@ describe('Actions', () => {
14711468
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
14721469
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
14731470
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1474-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123' } },
1475-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1471+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123', tideDashboardFilters: undefined } },
14761472
{ type: 'LOGIN_SUCCESS', payload: { user } },
14771473
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: ['/clinic-workspace', { selectedClinicId: 'clinic123' }] } }
14781474
];
@@ -1527,8 +1523,7 @@ describe('Actions', () => {
15271523
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
15281524
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
15291525
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1530-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic456' } },
1531-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1526+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic456', tideDashboardFilters: undefined } },
15321527
{ type: 'LOGIN_SUCCESS', payload: { user } },
15331528
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: ['/clinic-workspace', { selectedClinicId: 'clinic456' }] } }
15341529
];
@@ -1580,8 +1575,7 @@ describe('Actions', () => {
15801575
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
15811576
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
15821577
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1583-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123' } },
1584-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1578+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic123', tideDashboardFilters: undefined } },
15851579
{ type: 'LOGIN_SUCCESS', payload: { user } },
15861580
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: ['/clinic-workspace', { selectedClinicId: 'clinic123' }] } }
15871581
];
@@ -1797,8 +1791,7 @@ describe('Actions', () => {
17971791
{ type: 'FETCH_CLINICIAN_INVITES_SUCCESS', payload: { invites: [] }},
17981792
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_REQUEST' },
17991793
{ type: 'FETCH_ASSOCIATED_ACCOUNTS_SUCCESS', payload: { patients: [] }},
1800-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic456' } },
1801-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
1794+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId: 'clinic456', tideDashboardFilters: undefined } },
18021795
{ type: 'LOGIN_SUCCESS', payload: { user } },
18031796
{ type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'push', args: ['/newDestination', { selectedClinicId: 'clinic456' }] } }
18041797
];
@@ -5423,8 +5416,7 @@ describe('Actions', () => {
54235416
let expectedActions = [
54245417
{ type: 'CREATE_CLINIC_REQUEST' },
54255418
{ type: 'CREATE_CLINIC_SUCCESS', payload: { clinic : clinicReturn } },
5426-
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId : 'new_clinic_id' } },
5427-
{ type: 'tideDashboardFilters/setTideDashboardFilters', payload: undefined },
5419+
{ type: 'SELECT_CLINIC_SUCCESS', payload: { clinicId : 'new_clinic_id', tideDashboardFilters: undefined } },
54285420
{ type: 'GET_CLINICS_FOR_CLINICIAN_REQUEST' },
54295421
{ type: 'GET_CLINICS_FOR_CLINICIAN_SUCCESS', payload: { clinicianId, clinics } },
54305422
{ type: 'FETCH_CLINIC_EHR_SETTINGS_REQUEST' },

test/unit/redux/actions/sync.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3576,9 +3576,11 @@ describe('Actions', () => {
35763576

35773577
it('type should equal SELECT_CLINIC_SUCCESS', () => {
35783578
let clinicId = 'clinicId'
3579-
let action = sync.selectClinicSuccess(clinicId);
3579+
let tideDashboardFilters = { lastData: 30, patientTags: ['tag1'] };
3580+
let action = sync.selectClinicSuccess(clinicId, tideDashboardFilters);
35803581
expect(action.type).to.equal('SELECT_CLINIC_SUCCESS');
35813582
expect(action.payload.clinicId).to.equal(clinicId);
3583+
expect(action.payload.tideDashboardFilters).to.eql(tideDashboardFilters);
35823584
});
35833585
});
35843586

0 commit comments

Comments
 (0)