Skip to content

Commit

Permalink
feat: handle routing with breadcrumbs
Browse files Browse the repository at this point in the history
- Dynamic creation of the breadcrumb based on datastore tab selectionand clicking the edit action button
  • Loading branch information
d-rita committed Dec 9, 2024
1 parent a629183 commit 66e0293
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
49 changes: 37 additions & 12 deletions src/components/pager/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link, useLocation, useMatches } from 'react-router-dom'
import classes from './Page.module.css'

function Breadcrumbs() {
const matches = useMatches()
const location = useLocation()

const finalMatch = matches.filter(
(match) => match.pathname === location.pathname
)
const { params: { store, namespace, key } } = finalMatch[0]

const storeLabels = {
dataStore: 'DataStore',
userDataStore: 'User DataStore',
}

return (
<ul className={classes.list}>
<Link to={'/dataStore'}>
<b>DataStore</b>
</Link>
/
<Link to={'/dataStore/edit/namespace'}>
<b>Key</b>
</Link>
/
<Link to={'/dataStore/edit/namespace/key'}>
<b>Value</b>
</Link>
{store && (
<>
<Link to={`/${store}`}>
<b>{storeLabels[store]}</b>
</Link>
<span>/</span>
</>
)}
{namespace && (
<>
<Link to={`/${store}/edit/${namespace}`}>
<b>{namespace}</b>
</Link>
<span>/</span>
</>
)}
{key && (
<>
<Link to={`/${store}/edit/${namespace}/${key}`}>
<b>{key}</b>
</Link>
</>
)}
</ul>
)
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/pager/CreateAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import i18n from '../../locales'
import CreateButton from './buttons/CreateButton'
import CreateModal from './CreateModal'

function CreateAction({ type }) {
type CreateActionProps = {
type: string
}

function CreateAction({ type }: CreateActionProps) {
const [openModal, setOpenModal] = useState(false)
const [values, setValues] = useState({})
const label = type === 'key' ? i18n.t('New Key') : i18n.t('New Namespace')
Expand Down
8 changes: 4 additions & 4 deletions src/components/pager/DataStoreTabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import i18n from '../../locales'

type DataStoreProps = {
activeTab: string
setActiveTab: (string) => void
switchTab: (string) => void
}

export default function DataStoreTabBar({
activeTab,
setActiveTab,
switchTab,
}: DataStoreProps) {
return (
<TabBar>
<Tab
onClick={() => {
setActiveTab('dataStore')
switchTab('dataStore')
}}
selected={activeTab === 'dataStore'}
>
{i18n.t('DataStore')}
</Tab>
<Tab
onClick={() => {
setActiveTab('userDataStore')
switchTab('userDataStore')
}}
selected={activeTab === 'userDataStore'}
>
Expand Down
9 changes: 8 additions & 1 deletion src/components/pager/pages/Namespaces.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import i18n from '../../../locales'
import CreateAction from '../CreateAction'
import DataStoreTabBar from '../DataStoreTabBar'
Expand All @@ -7,6 +8,8 @@ import SearchField from '../SearchField'
import ItemsTable from '../table/Table'

const NamespacesPage = () => {
const navigate = useNavigate()

const data = {
results: ['tea', 'coffee', 'chocolate', 'rose'],
}
Expand All @@ -16,6 +19,10 @@ const NamespacesPage = () => {

const [activeTab, setActiveTab] = useState('dataStore')

const handleSwitchTab = (selectedTab) => {
setActiveTab(selectedTab)
navigate(`/${selectedTab}`)
}
// question: fetch data outside? filter it inside? handling search

const RenderMidSection = ({ data }: { data: { results: string[] } }) => {
Expand All @@ -36,7 +43,7 @@ const NamespacesPage = () => {
<div>
<DataStoreTabBar
activeTab={activeTab}
setActiveTab={setActiveTab}
switchTab={handleSwitchTab}
/>
</div>
{activeTab === 'dataStore' && <RenderMidSection data={data} />}
Expand Down
4 changes: 3 additions & 1 deletion src/components/pager/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const ItemsTable = ({ data, label }: TableProps) => {
{item}
</DataTableCell>
<DataTableCell bordered width="12%">
<TableActions />
<TableActions
item={item}
/>
</DataTableCell>
</DataTableRow>
)
Expand Down
10 changes: 7 additions & 3 deletions src/components/pager/table/actions/TableActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import classes from '../../Page.module.css'
import DeleteButton from './DeleteButton'
import DeleteModal from './DeleteModal'

const TableActions = () => {
type TableActionProps = {
item: string
}

const TableActions = ({ item }: TableActionProps) => {
const navigate = useNavigate()
const { namespace } = useParams()

Expand All @@ -17,9 +21,9 @@ const TableActions = () => {
<EditButton
handleClick={() => {
if (namespace) {
navigate(`key`)
navigate(`${item}`)
} else {
navigate(`edit/namespace`)
navigate(`edit/${item}`)
}
}}
/>
Expand Down

0 comments on commit 66e0293

Please sign in to comment.