Skip to content

Commit f442ffa

Browse files
authored
MPDX-8408 Filter Panel User Options (#1160)
* add useUserPreference() * Make PGA Report filters always show like angular
1 parent 44a98de commit f442ffa

File tree

6 files changed

+46
-16
lines changed

6 files changed

+46
-16
lines changed

pages/accountLists/[accountListId]/contacts/ContactsWrapper.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ContactFilterSetInput,
99
TaskFilterSetInput,
1010
} from 'src/graphql/types.generated';
11+
import { useUserPreference } from 'src/hooks/useUserPreference';
1112
import { sanitizeFilters } from 'src/lib/sanitizeFilters';
1213
import { getQueryParam } from 'src/utils/queryParam';
1314

@@ -76,7 +77,10 @@ export const ContactsWrapper: React.FC<Props> = ({
7677
const [searchTerm, setSearchTerm] = useState(
7778
getQueryParam(query, 'searchTerm') ?? '',
7879
);
79-
const [filterPanelOpen, setFilterPanelOpen] = useState(true);
80+
const [filterPanelOpen, setFilterPanelOpen] = useUserPreference({
81+
key: 'contact_filters_collapse',
82+
defaultValue: false,
83+
});
8084

8185
// Only allow the ids filter in map view, and remove the ids filter in other views
8286
const activeFilters = useMemo(() => {

pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.test.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ describe('partnerGivingAnalysis page', () => {
115115
});
116116

117117
it('renders filters panel', async () => {
118-
const { getByRole, findByRole } = render(<TestingComponent />);
118+
const { findByRole } = render(<TestingComponent />);
119119

120-
userEvent.click(getByRole('img', { name: 'Toggle Filter Panel' }));
121120
expect(await findByRole('heading', { name: 'Filter' })).toBeInTheDocument();
122121
});
123122

@@ -128,11 +127,11 @@ describe('partnerGivingAnalysis page', () => {
128127

129128
const leftPanel = getByTestId('SidePanelsLayoutLeftPanel');
130129

131-
userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
132-
expect(leftPanel).toHaveStyle('transform: none');
133-
134130
userEvent.click(await findByTestId('FilterPanelClose'));
135131
expect(leftPanel).toHaveStyle('transform: translate(-100%)');
132+
133+
userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
134+
expect(leftPanel).toHaveStyle('transform: none');
136135
});
137136

138137
it('changes the URL when a contact is selected', async () => {
@@ -162,7 +161,6 @@ describe('partnerGivingAnalysis page', () => {
162161
);
163162
const searchBar = getByPlaceholderText('Search Contacts');
164163
userEvent.type(searchBar, 'John');
165-
userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
166164
userEvent.click(
167165
await findByRole('combobox', { name: 'Designation Account' }),
168166
);

pages/accountLists/[accountListId]/reports/partnerGivingAnalysis/[[...contactId]].page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const PartnerGivingAnalysisReportPage: React.FC = () => {
4141
const { t } = useTranslation();
4242
const accountListId = useAccountListId();
4343
const { appName } = useGetAppSettings();
44-
const [panelOpen, setPanelOpen] = useState<Panel | null>(null);
44+
const [panelOpen, setPanelOpen] = useState<Panel | null>(Panel.Filters);
4545
const reportRef = useRef<PartnerGivingAnalysisReportRef>(null);
4646

4747
const router = useRouter();

pages/accountLists/[accountListId]/tasks/[[...contactId]].page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { useAccountListId } from 'src/hooks/useAccountListId';
2929
import useGetAppSettings from 'src/hooks/useGetAppSettings';
3030
import { useMassSelection } from 'src/hooks/useMassSelection';
3131
import useTaskModal from 'src/hooks/useTaskModal';
32+
import { useUserPreference } from 'src/hooks/useUserPreference';
3233
import theme from 'src/theme';
3334
import {
3435
TaskFilterTabsTypes,
@@ -114,7 +115,10 @@ const TasksPage: React.FC = () => {
114115
setContactId: setContactFocus,
115116
} = useTasksContactContext();
116117
const contactDetailsOpen = !!contactDetailsId;
117-
const [filterPanelOpen, setFilterPanelOpen] = useState(true);
118+
const [filterPanelOpen, setFilterPanelOpen] = useUserPreference({
119+
key: 'tasks_filters_collapse',
120+
defaultValue: false,
121+
});
118122

119123
//#region Filters
120124

src/hooks/useUserPreference.test.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const mutationSpy = jest.fn();
1818
interface WrapperProps {
1919
cached?: boolean;
2020
json?: boolean;
21+
invalidJson?: boolean;
2122
}
2223

2324
/**
@@ -26,7 +27,7 @@ interface WrapperProps {
2627
* The component returned from this function can be passed as the `wrapper` option to `renderHook`.
2728
**/
2829
const makeWrapper = (props: WrapperProps = {}) => {
29-
const { cached = true, json = false } = props;
30+
const { cached = true, json = false, invalidJson = false } = props;
3031

3132
const cache = createCache();
3233
if (cached) {
@@ -52,7 +53,11 @@ const makeWrapper = (props: WrapperProps = {}) => {
5253
UserOption: {
5354
userOption: {
5455
key,
55-
value: json ? '["initial"]' : 'initial',
56+
value: json
57+
? invalidJson
58+
? 'malformed JSON'
59+
: '["initial"]'
60+
: 'initial',
5661
},
5762
},
5863
UpdateUserOption: {
@@ -152,4 +157,19 @@ describe('useUserPreference', () => {
152157
value: '["changed"]',
153158
});
154159
});
160+
161+
it('uses the default value when json.parse errors', async () => {
162+
const { result, waitForNextUpdate } = renderHook(
163+
() => useUserPreference({ key, defaultValue: [defaultValue] }),
164+
{
165+
wrapper: makeWrapper({ cached: true, json: true, invalidJson: true }),
166+
},
167+
);
168+
169+
expect(result.current[0]).toEqual(['cached']);
170+
171+
await waitForNextUpdate();
172+
173+
expect(result.current[0]).toEqual([defaultValue]);
174+
});
155175
});

src/hooks/useUserPreference.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ export const useUserPreference = <T>({
4242
if (typeof defaultValue === 'string') {
4343
setValue((data.userOption.value ?? defaultValue) as T);
4444
} else {
45-
setValue(
46-
typeof data.userOption.value === 'string'
47-
? JSON.parse(data.userOption.value)
48-
: defaultValue,
49-
);
45+
try {
46+
setValue(
47+
typeof data.userOption.value === 'string'
48+
? JSON.parse(data.userOption.value)
49+
: defaultValue,
50+
);
51+
} catch {
52+
setValue(defaultValue);
53+
}
5054
}
5155
}, [data]);
5256

0 commit comments

Comments
 (0)