-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[University] Extract tabs to a new hook
- Loading branch information
1 parent
c26728d
commit 597f1f0
Showing
4 changed files
with
57 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import qs from 'query-string' | ||
import { useEffect, useState } from 'react' | ||
import { useHistory, useLocation } from 'react-router-dom' | ||
|
||
export const useTabs = (totalTabs: number) => { | ||
const history = useHistory() | ||
const location = useLocation() | ||
|
||
const id = 'tab' | ||
const initialTab = 0 | ||
|
||
const normalizeTab = (tab: unknown) => { | ||
const tabIndex = parseInt(tab as string, 10) | ||
return Number.isNaN(tabIndex) || tabIndex < 0 || tabIndex >= totalTabs ? initialTab : tabIndex | ||
} | ||
|
||
const [tab, setTab] = useState<number>(() => { | ||
const params = qs.parse(location.search) | ||
return normalizeTab(params[id]) | ||
}) | ||
|
||
const pushToUrl = (newTab: number) => { | ||
history.replace({ | ||
pathname: location.pathname, | ||
search: qs.stringify({ ...qs.parse(location.search), [id]: newTab }), | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
const params = qs.parse(location.search) | ||
const queryTab = normalizeTab(params[id]) | ||
if (queryTab !== tab) { | ||
setTab(queryTab) | ||
} | ||
}, [id, location.search]) | ||
|
||
useEffect(() => { | ||
pushToUrl(tab) | ||
}, [tab]) | ||
|
||
const handleChange = (_event: React.SyntheticEvent, { activeIndex }: { activeIndex: number }) => { | ||
setTab(activeIndex) | ||
} | ||
|
||
return [tab, handleChange] as const | ||
} |
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 |
---|---|---|
@@ -1,42 +1,29 @@ | ||
import { Container, Tab, Tabs } from '@mui/material' | ||
import { useEffect, useState } from 'react' | ||
import { useHistory, useLocation } from 'react-router-dom' | ||
|
||
import { useTitle } from '@/common/hooks' | ||
import { PageTitle } from '@/components/material/PageTitle' | ||
import { useTabs } from '@/hooks/tabs' | ||
import { FacultyGraduationsTab } from './FacultyGraduationsTab' | ||
import { FacultyProgressTab } from './FacultyProgressTab' | ||
|
||
export const University = () => { | ||
useTitle('University') | ||
|
||
const history = useHistory() | ||
const location = useLocation() | ||
|
||
const query = new URLSearchParams(location.search) | ||
const tabFromQuery = parseInt(query.get('tab') ?? '') | ||
const initialTab = [0, 1].includes(tabFromQuery) ? tabFromQuery : 0 | ||
const [activeTab, setActiveTab] = useState(initialTab) | ||
|
||
const handleTabChange = (_event, newValue: number) => { | ||
setActiveTab(newValue) | ||
query.set('tab', newValue.toString()) | ||
history.push({ search: query.toString() }) | ||
} | ||
|
||
useEffect(() => { | ||
setActiveTab(initialTab) | ||
}, [initialTab]) | ||
const [tab, handleTabChange] = useTabs(2) | ||
|
||
return ( | ||
<Container maxWidth="lg"> | ||
<PageTitle title="University" /> | ||
<Tabs onChange={handleTabChange} sx={{ marginBottom: 2 }} value={activeTab}> | ||
<Tabs | ||
onChange={(event, newValue) => handleTabChange(event, { activeIndex: newValue })} | ||
sx={{ marginBottom: 2 }} | ||
value={tab} | ||
> | ||
<Tab data-cy="FacultyProgressTab" label="Faculty progress" /> | ||
<Tab data-cy="FacultyGraduationsTab" label="Faculty graduations" /> | ||
</Tabs> | ||
{activeTab === 0 && <FacultyProgressTab />} | ||
{activeTab === 1 && <FacultyGraduationsTab />} | ||
{tab === 0 && <FacultyProgressTab />} | ||
{tab === 1 && <FacultyGraduationsTab />} | ||
</Container> | ||
) | ||
} |