-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Router): ✨ dr type preserved in URL
UPDATE #34
- Loading branch information
Showing
5 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters