Skip to content

Commit

Permalink
refactor: change TAB_LIST to enum and remove currentTab state
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Jul 9, 2024
1 parent 2764aa0 commit 3b2a6ad
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/library-authoring/LibraryAuthoringPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { StudioFooter } from '@edx/frontend-component-footer';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Expand All @@ -19,11 +19,11 @@ import LibraryHome from './LibraryHome';
import { useContentLibrary } from './data/apiHook';
import messages from './messages';

const TAB_LIST = {
home: '',
components: 'components',
collections: 'collections',
};
enum TabList {
home = '',
components = 'components',
collections = 'collections',
}

const SubHeaderTitle = ({ title }: { title: string }) => {
const intl = useIntl();
Expand All @@ -44,21 +44,14 @@ const LibraryAuthoringPage = () => {
const intl = useIntl();
const location = useLocation();
const navigate = useNavigate();
const [tabKey, setTabKey] = useState(TAB_LIST.home);
const [searchKeywords, setSearchKeywords] = useState('');

const { libraryId } = useParams();

const { data: libraryData, isLoading } = useContentLibrary(libraryId);

useEffect(() => {
const currentPath = location.pathname.split('/').pop();
if (currentPath && Object.values(TAB_LIST).includes(currentPath)) {
setTabKey(currentPath);
} else {
setTabKey(TAB_LIST.home);
}
}, [location]);
const currentPath = location.pathname.split('/').pop();
const activeKey = (currentPath && currentPath in TabList) ? TabList[currentPath] : TabList.home;

if (isLoading) {
return <Loading />;
Expand All @@ -69,7 +62,7 @@ const LibraryAuthoringPage = () => {
}

const handleTabChange = (key: string) => {
setTabKey(key);
// setTabKey(key);
navigate(key);
};

Expand All @@ -96,25 +89,25 @@ const LibraryAuthoringPage = () => {
/>
<Tabs
variant="tabs"
activeKey={tabKey}
activeKey={activeKey}
onSelect={handleTabChange}
className="my-3"
>
<Tab eventKey={TAB_LIST.home} title={intl.formatMessage(messages.homeTab)} />
<Tab eventKey={TAB_LIST.components} title={intl.formatMessage(messages.componentsTab)} />
<Tab eventKey={TAB_LIST.collections} title={intl.formatMessage(messages.collectionsTab)} />
<Tab eventKey={TabList.home} title={intl.formatMessage(messages.homeTab)} />
<Tab eventKey={TabList.components} title={intl.formatMessage(messages.componentsTab)} />
<Tab eventKey={TabList.collections} title={intl.formatMessage(messages.collectionsTab)} />
</Tabs>
<Routes>
<Route
path={TAB_LIST.home}
path={TabList.home}
element={<LibraryHome libraryId={libraryId} filter={{ searchKeywords }} />}
/>
<Route
path={TAB_LIST.components}
path={TabList.components}
element={<LibraryComponents libraryId={libraryId} filter={{ searchKeywords }} />}
/>
<Route
path={TAB_LIST.collections}
path={TabList.collections}
element={<LibraryCollections />}
/>
<Route
Expand Down

0 comments on commit 3b2a6ad

Please sign in to comment.