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

Fix/discover link break #2537

Merged
Merged
26 changes: 26 additions & 0 deletions site/gatsby-site/cypress/e2e/integration/discover.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,32 @@ describe('The Discover app', () => {
}
);

it('Should update display types', () => {
cy.visit(url + '?display=list');

cy.get('[data-cy="display-mode-list"]').should('have.class', 'selected');

cy.waitForStableDOM();

cy.get('[data-cy="display-mode-compact"]').click();

cy.waitForStableDOM();

cy.location('search', { timeout: 8000 }).should('contain', 'display=compact');

cy.get('[data-cy="display-mode-compact"]').should('have.class', 'selected');

cy.waitForStableDOM();

cy.get('[data-cy="display-mode-details"]').click();

cy.waitForStableDOM();

cy.location('search', { timeout: 8000 }).should('contain', 'display=details');

cy.get('[data-cy="display-mode-details"]').should('have.class', 'selected');
});

conditionalIt(
!Cypress.env('isEmptyEnvironment'),
'Search using the classifications filter',
Expand Down
18 changes: 16 additions & 2 deletions site/gatsby-site/src/components/discover/Discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,20 @@ export default function Discover() {
}, 1000)
).current;

const [display, setDisplay] = useState('');

useEffect(() => {
const queryString = window.location.search;

const urlParams = new URLSearchParams(queryString);

const display = urlParams.get('display');

setDisplay((prev) => {
if (display && display !== prev) {
return display;
}
});
window.addEventListener('resize', handleWindowSizeChange);

handleWindowSizeChange();
Expand Down Expand Up @@ -72,8 +85,9 @@ export default function Discover() {
return window.location;
},
parseURL: ({ location }) => parseURL({ location, indexName, queryConfig, taxa }),
createURL: ({ routeState }) =>
createURL({ indexName, locale, queryConfig, routeState, taxa }),
createURL: ({ routeState }) => {
return createURL({ indexName, locale, queryConfig, routeState, taxa, display });
},
push: (url) => {
navigate(`?${url}`);
},
Expand Down
30 changes: 28 additions & 2 deletions site/gatsby-site/src/components/discover/DisplayModeSwitch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { useQueryParam } from 'use-query-params';
import { DisplayModeEnumParam } from './queryParams';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTh, faThList, faInfo } from '@fortawesome/free-solid-svg-icons';
import { Button } from 'flowbite-react';
import { useInstantSearch } from 'react-instantsearch';

const modes = {
details: {
Expand All @@ -20,8 +21,30 @@ const modes = {
export default function DisplayModeSwitch() {
const [display, setDisplay] = useQueryParam('display', DisplayModeEnumParam);

const { indexUiState, setIndexUiState } = useInstantSearch();

const [, setConfigure] = useState({ ...indexUiState.configure });

useEffect(() => {
setConfigure((configure) => ({ ...configure, ...indexUiState.configure }));
}, [indexUiState]);

const onChange = (key) => {
setDisplay(key);
setIndexUiState((previousState) => {
return {
...previousState,
refinementList: {
...previousState.refinementList,
display: [key],
},
configure: {
...previousState.configure,
},
};
});

setConfigure((configure) => ({ ...configure }));
};

return (
Expand All @@ -32,8 +55,11 @@ export default function DisplayModeSwitch() {
onClick={() => onChange(key)}
size="sm"
className={`${
key == display ? 'bg-gray-700 hover:bg-gray-800' : 'bg-gray-500 hover:bg-gray-700'
key == display
? 'bg-gray-700 hover:bg-gray-800 selected'
: 'bg-gray-500 hover:bg-gray-700'
} text-white w-8`}
data-cy={`display-mode-${key}`}
>
<FontAwesomeIcon icon={modes[key].icon} />
</Button>
Expand Down
8 changes: 6 additions & 2 deletions site/gatsby-site/src/components/discover/createURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ const getQueryFromState = ({ state, locale, taxa }) => {
return query;
};

export default function ({ routeState, indexName, locale, queryConfig, taxa }) {
export default function ({ routeState, indexName, locale, queryConfig, taxa, display }) {
const state = routeState[indexName] || {};

const query = getQueryFromState({ state, locale, taxa });

const encoded = encodeQueryParams(queryConfig, query);

const stringified = stringify(encoded);
let stringified = stringify(encoded);

if (display) {
stringified += `&display=${display}`;
}

return stringified;
}
2 changes: 1 addition & 1 deletion site/gatsby-site/src/components/discover/hitTypes/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TranslationBadge from 'components/i18n/TranslationBadge';
import { VIEW_TYPES } from 'utils/discover';
import ReportCard from 'components/reports/ReportCard';

export default function Details({ item, toggleFilterByIncidentId, viewType }) {
export default function List({ item, toggleFilterByIncidentId, viewType }) {
const actions = <Actions toggleFilterByIncidentId={toggleFilterByIncidentId} item={item} />;

item.title = viewType === VIEW_TYPES.INCIDENTS ? item.incident_title : item.title;
Expand Down
2 changes: 1 addition & 1 deletion site/gatsby-site/src/components/reports/ReportCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ReportCard = ({
incidentId = null,
readOnly = false,
}) => {
item.incident_id = incidentId;
item.incident_id = incidentId || item.incident_id;

const { t } = useTranslation();

Expand Down
Loading