Skip to content

Commit

Permalink
feat(Router): ✨ dr type preserved in URL
Browse files Browse the repository at this point in the history
UPDATE #34
  • Loading branch information
jalezi committed Jan 17, 2022
1 parent 15064e1 commit e81b9e8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/Filters/ToggleDoctorType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import { useFilter } from 'context/filterContext';
import { useTranslation } from 'react-i18next';
import { useEffect, useState } from 'react';
import { useLocation } from 'react-router';

import { useDoctorTypeExactPath } from 'hooks';

import { IconToggleButton } from './Shared';

function withToggleGroup(Component) {
return function ToggleDoctorType(props) {
const { state } = useLocation();
const { t } = useTranslation();

const { drType: drTypeFromPath, ageGroup: ageGroupFromPath } = useDoctorTypeExactPath();

const { type: stateType, ageGroup: stateAgeGroup } = state ?? {
type: 'gp',
ageGroup: 'adults',
type: drTypeFromPath ?? 'gp',
ageGroup: ageGroupFromPath ?? 'adults',
};

const { doctorType, setDoctorType } = useFilter();
Expand All @@ -34,6 +39,7 @@ function withToggleGroup(Component) {
};

useEffect(() => {
// TODO refactor; maybe is not needed at all; we have age groups only for dentists
const typeTranslate = {
gp_adults: 'gp',
gp_youth: 'gp',
Expand Down
7 changes: 6 additions & 1 deletion src/context/filterContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createContext, useCallback, useContext, useState, useEffect, useMemo } from 'react';

import { useDoctorTypeExactPath } from 'hooks';

import { filterBySearchValueInMapBounds } from '../utils';
import { useDoctors } from './doctorsContext';
import { useLeafletContext } from './leafletContext';
Expand All @@ -12,7 +15,9 @@ const FilterProvider = function FilterProvider({ children }) {
const { map } = useLeafletContext();
const { doctors: _doctors } = useDoctors();

const [doctorType, setDoctorType] = useState('gp');
const { type } = useDoctorTypeExactPath();

const [doctorType, setDoctorType] = useState(type ?? 'gp');
const [accept, setAccept] = useState('vsi');
const [searchValue, setSearchValue] = useState('');

Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as useTimeout } from './useTimeout';
export { default as useDebounce } from './useDebounce';
export { default as useGeoLocation } from './useGeoLocation';
export { default as useEventListener } from './useEventListener';
export { default as useDoctorTypeExactPath } from './useDoctorTypeExactPath';
34 changes: 34 additions & 0 deletions src/hooks/useDoctorTypeExactPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useTranslation } from 'react-i18next';
import { useLocation, matchPath } from 'react-router-dom';

import { DOCTORS } from 'const';
import { useState } from 'react';

const AGE_GROUP_TRANLATE = {
gp: 'adults',
ped: 'youth',
gyn: 'adults',
den: 'adults',
'den-y': 'youth',
'den-s': 'students',
};

export default function useDoctorExactPath() {
const {
i18n: { languages },
} = useTranslation();
const location = useLocation();

const typePathObj = languages
.map(lang => matchPath(`/${lang}/:type`, location.pathname))
.filter(paramKey => Boolean(paramKey))?.[0];

const type = typePathObj?.params?.type;

const isValidPath = DOCTORS.TYPES.includes(type);
const [path] = useState(isValidPath ? type : null);
const [drType] = useState(isValidPath ? type.split('-')[0] : null);
const [ageGroup] = useState(isValidPath ? AGE_GROUP_TRANLATE[path] : null);

return { pathObj: typePathObj, type: path, drType, ageGroup, isValidPath };
}
15 changes: 15 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,30 @@ const Router = function Router() {
/>
));

const doctorTypeRoutes = languages.map(lang => (
<Route
key={`${lang}-dr-type-page`}
exact
path={`/${lang}/:type`}
element={
<Suspense fallback={<Loader.Center />}>
<Home />
</Suspense>
}
/>
));

return (
<HelmetProvider>
<Routes>
<Route exact path="/" element={<Navigate to={`/${language}/`} />} />
<Route exact path="/faq" element={<Navigate to={`/${language}/faq`} />} />
<Route exact path="/about" element={<Navigate to={`/${language}/about`} />} />

{homeRoutes}
{faqRoutes}
{aboutRoutes}
{doctorTypeRoutes}
{doctorPageRoutes}
{notFoundRoutes}

Expand Down

0 comments on commit e81b9e8

Please sign in to comment.