Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement switch list using useNavigate #48

Merged
merged 6 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AddItem, Home, Layout, List } from './views';
import { getItemData, streamListItems } from './api';
import { useStateWithStorage } from './utils';
import { generateToken } from '@the-collab-lab/shopping-list-utils';
import SwitchList from './views/SwitchList';

export function App() {
const [data, setData] = useState([]);
Expand All @@ -23,6 +24,10 @@ export function App() {
function setNewToken() {
setListToken(generateToken());
}

function clearListToken() {
setListToken(null);
}

useEffect(() => {
if (!listToken) return;
Expand Down Expand Up @@ -85,6 +90,7 @@ export function App() {
/>
}
/>
<Route path="/switch-list" element={<SwitchList clearListToken={clearListToken}/>} />
<Route
path="/list"
element={<List data={data} token={listToken} />}
Expand Down
7 changes: 2 additions & 5 deletions src/views/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ export function Layout({ token }) {
<nav className="Nav">
{token ? (
<>
<button
className="switch-btn Nav-link"
onClick={handleSwitchToken}
>
<NavLink to="/switch-list" className="Nav-link">
Switch List
</button>
</NavLink>
<NavLink to="/list" className="Nav-link">
List
</NavLink>
Expand Down
15 changes: 15 additions & 0 deletions src/views/SwitchList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';

export default function SwitchList({clearListToken}) {
const navigate = useNavigate();

React.useEffect(() => {
setTimeout(() => {
clearListToken()
navigate('/');
}, 1000);
}, [navigate]);

return <p style={{textAlign: "center"}}>Redirecting to home...</p>;
}