Skip to content

Commit

Permalink
MPDX-8408 Filter Panel User Options (#1160)
Browse files Browse the repository at this point in the history
* add useUserPreference()

* Make PGA Report filters always show like angular
  • Loading branch information
caleballdrin authored Oct 29, 2024
1 parent 44a98de commit f442ffa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ContactFilterSetInput,
TaskFilterSetInput,
} from 'src/graphql/types.generated';
import { useUserPreference } from 'src/hooks/useUserPreference';
import { sanitizeFilters } from 'src/lib/sanitizeFilters';
import { getQueryParam } from 'src/utils/queryParam';

Expand Down Expand Up @@ -76,7 +77,10 @@ export const ContactsWrapper: React.FC<Props> = ({
const [searchTerm, setSearchTerm] = useState(
getQueryParam(query, 'searchTerm') ?? '',
);
const [filterPanelOpen, setFilterPanelOpen] = useState(true);
const [filterPanelOpen, setFilterPanelOpen] = useUserPreference({
key: 'contact_filters_collapse',
defaultValue: false,
});

// Only allow the ids filter in map view, and remove the ids filter in other views
const activeFilters = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ describe('partnerGivingAnalysis page', () => {
});

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

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

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

const leftPanel = getByTestId('SidePanelsLayoutLeftPanel');

userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
expect(leftPanel).toHaveStyle('transform: none');

userEvent.click(await findByTestId('FilterPanelClose'));
expect(leftPanel).toHaveStyle('transform: translate(-100%)');

userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
expect(leftPanel).toHaveStyle('transform: none');
});

it('changes the URL when a contact is selected', async () => {
Expand Down Expand Up @@ -162,7 +161,6 @@ describe('partnerGivingAnalysis page', () => {
);
const searchBar = getByPlaceholderText('Search Contacts');
userEvent.type(searchBar, 'John');
userEvent.click(getByRole('button', { name: 'Toggle Filter Panel' }));
userEvent.click(
await findByRole('combobox', { name: 'Designation Account' }),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const PartnerGivingAnalysisReportPage: React.FC = () => {
const { t } = useTranslation();
const accountListId = useAccountListId();
const { appName } = useGetAppSettings();
const [panelOpen, setPanelOpen] = useState<Panel | null>(null);
const [panelOpen, setPanelOpen] = useState<Panel | null>(Panel.Filters);
const reportRef = useRef<PartnerGivingAnalysisReportRef>(null);

const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useAccountListId } from 'src/hooks/useAccountListId';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { useMassSelection } from 'src/hooks/useMassSelection';
import useTaskModal from 'src/hooks/useTaskModal';
import { useUserPreference } from 'src/hooks/useUserPreference';
import theme from 'src/theme';
import {
TaskFilterTabsTypes,
Expand Down Expand Up @@ -114,7 +115,10 @@ const TasksPage: React.FC = () => {
setContactId: setContactFocus,
} = useTasksContactContext();
const contactDetailsOpen = !!contactDetailsId;
const [filterPanelOpen, setFilterPanelOpen] = useState(true);
const [filterPanelOpen, setFilterPanelOpen] = useUserPreference({
key: 'tasks_filters_collapse',
defaultValue: false,
});

//#region Filters

Expand Down
24 changes: 22 additions & 2 deletions src/hooks/useUserPreference.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const mutationSpy = jest.fn();
interface WrapperProps {
cached?: boolean;
json?: boolean;
invalidJson?: boolean;
}

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

const cache = createCache();
if (cached) {
Expand All @@ -52,7 +53,11 @@ const makeWrapper = (props: WrapperProps = {}) => {
UserOption: {
userOption: {
key,
value: json ? '["initial"]' : 'initial',
value: json
? invalidJson
? 'malformed JSON'
: '["initial"]'
: 'initial',
},
},
UpdateUserOption: {
Expand Down Expand Up @@ -152,4 +157,19 @@ describe('useUserPreference', () => {
value: '["changed"]',
});
});

it('uses the default value when json.parse errors', async () => {
const { result, waitForNextUpdate } = renderHook(
() => useUserPreference({ key, defaultValue: [defaultValue] }),
{
wrapper: makeWrapper({ cached: true, json: true, invalidJson: true }),
},
);

expect(result.current[0]).toEqual(['cached']);

await waitForNextUpdate();

expect(result.current[0]).toEqual([defaultValue]);
});
});
14 changes: 9 additions & 5 deletions src/hooks/useUserPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export const useUserPreference = <T>({
if (typeof defaultValue === 'string') {
setValue((data.userOption.value ?? defaultValue) as T);
} else {
setValue(
typeof data.userOption.value === 'string'
? JSON.parse(data.userOption.value)
: defaultValue,
);
try {
setValue(
typeof data.userOption.value === 'string'
? JSON.parse(data.userOption.value)
: defaultValue,
);
} catch {
setValue(defaultValue);
}
}
}, [data]);

Expand Down

0 comments on commit f442ffa

Please sign in to comment.