Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKomara committed Nov 27, 2024
1 parent bce3301 commit 9b0d3b4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 50 deletions.
68 changes: 25 additions & 43 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,38 @@
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
import { Outlet, NavLink } from 'react-router-dom';
import classNames from 'classnames';

// const tabs = [
// { id: 'tab-1', title: 'Tab 1', content: 'Some text 1' },
// { id: 'tab-2', title: 'Tab 2', content: 'Some text 2' },
// { id: 'tab-3', title: 'Tab 3', content: 'Some text 3' },
// ];
const getLinkClass = ({ isActive }: { isActive: boolean }) =>
classNames('navbar-item', {
'is-active': isActive,
});

export const App = () => (
<>
{/* Also requires <html class="has-navbar-fixed-top"> */}
<nav
className="navbar is-light is-fixed-top is-mobile has-shadow"
data-cy="Nav"
>
<div className="container">
<div className="navbar-brand">
<a href="/" className="navbar-item is-active">
Home
</a>
<a href="/tabs" className="navbar-item">
Tabs
</a>
</div>
</div>
</nav>

<div className="section">
<div className="container">
<h1 className="title">Home page</h1>
<h1 className="title">Tabs page</h1>
<h1 className="title">Page not found</h1>

<div className="tabs is-boxed">
<ul>
<li data-cy="Tab" className="is-active">
<a href="#/">Tab 1</a>
</li>
<li data-cy="Tab">
<a href="#/">Tab 2</a>
</li>
<li data-cy="Tab">
<a href="#/">Tab 3</a>
</li>
</ul>
<html className="has-navbar-fixed-top">
<nav
className="navbar is-light is-fixed-top is-mobile has-shadow"
data-cy="Nav"
>
<div className="container">
<div className="navbar-brand">
<NavLink to="/" className={getLinkClass}>
Home
</NavLink>
<NavLink to="/tabs" className={getLinkClass}>
Tabs
</NavLink>
</div>
</div>
</nav>

<div className="block" data-cy="TabContent">
Please select a tab
<div className="section">
<div className="container">
<Outlet />
</div>
</div>
</div>
</html>
</>
);
25 changes: 25 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HashRouter, Navigate } from 'react-router-dom';
import { App } from './App';
import { Route, Routes } from 'react-router-dom';
import { Tabs } from './pages/Tabs';
import { Home } from './pages/Home';
import { NotFound } from './pages/NotFound';

export const Root = () => {
return (
<HashRouter>
<Routes>
<Route path="/" element={<App />}>
<Route index element={<Home />} />
<Route path="/home" element={<Navigate to="/" replace />} />

<Route path="tabs">
<Route path=":tabId?" element={<Tabs />} />
</Route>

<Route path="*" element={<NotFound />} />
</Route>
</Routes>
</HashRouter>
);
};
5 changes: 5 additions & 0 deletions src/constants/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const tabs = [
{ id: 'tab-1', title: 'Tab 1', content: 'Some text 1' },
{ id: 'tab-2', title: 'Tab 2', content: 'Some text 2' },
{ id: 'tab-3', title: 'Tab 3', content: 'Some text 3' },
];
11 changes: 4 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { createRoot } from 'react-dom/client';
import { HashRouter } from 'react-router-dom';
import { App } from './App';
import { Root } from './Root';

createRoot(document.getElementById('root') as HTMLElement).render(
<HashRouter>
<App />
</HashRouter>,
);
const container = document.getElementById('root') as HTMLElement;

createRoot(container).render(<Root />);
5 changes: 5 additions & 0 deletions src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const Home = () => (
<div className="container">
<h1 className="title">Home page</h1>
</div>
);
5 changes: 5 additions & 0 deletions src/pages/NotFound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const NotFound = () => (
<div className="container">
<h1 className="title">Page not found</h1>
</div>
);
30 changes: 30 additions & 0 deletions src/pages/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Link, useParams } from 'react-router-dom';
import { tabs } from '../constants/tabs';

export const Tabs = () => {
const { tabId } = useParams();
const selectedTab = tabs.find(tab => tab.id === tabId);

return (
<div className="container">
<h1 className="title">Tabs page</h1>
<div className="tabs is-boxed">
<ul>
{tabs.map(tab => (
<li
data-cy="Tab"
className={tabId === tab.id ? 'is-active' : ''}
key={tab.id}
>
<Link to={`/tabs/${tab.id}`}>{tab.title}</Link>
</li>
))}
</ul>
</div>

<div className="block" data-cy="TabContent">
{selectedTab ? selectedTab.content : 'Please select a tab'}
</div>
</div>
);
};

0 comments on commit 9b0d3b4

Please sign in to comment.