From e115a5a7ce029bce49de86b306f223288bdc5fca Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Wed, 30 Dec 2020 16:27:38 -0600 Subject: [PATCH] Routing --- components/AppShell.jsx | 12 +++++--- components/MenuContent.jsx | 11 ++++--- components/pages/ListDetail.jsx | 6 ++++ components/pages/Lists.jsx | 23 +++++++++------ components/pages/Settings.jsx | 5 ++++ components/ui/Tab.jsx | 51 +++++++++++++++++++-------------- store/actions.js | 12 -------- 7 files changed, 71 insertions(+), 49 deletions(-) diff --git a/components/AppShell.jsx b/components/AppShell.jsx index ce0347f..2c34939 100644 --- a/components/AppShell.jsx +++ b/components/AppShell.jsx @@ -60,6 +60,8 @@ const CurrentPage = ({ page }) => { }; const AppShell = ({ page }) => { + const [location, setLocation] = useLocation(); + const showMenu = Store.useState(selectors.getMenuOpen); const showNotifications = Store.useState(selectors.getNotificationsOpen); const currentPage = Store.useState(selectors.getCurrentPage); @@ -86,6 +88,8 @@ const AppShell = ({ page }) => { } ); + console.log('Got location', location, '/settings' === location); + // This is an example app layout. We've got a hidden menu that will be toggled // return ( @@ -109,21 +113,21 @@ const AppShell = ({ page }) => { selectedIcon={home} title="Home" href="/" - selected={'home' === currentPage?.id} + selected={'/home' === location} /> diff --git a/components/MenuContent.jsx b/components/MenuContent.jsx index 53559a0..8514844 100644 --- a/components/MenuContent.jsx +++ b/components/MenuContent.jsx @@ -1,3 +1,4 @@ +import useLocation from '../hooks/useLocation'; import Store from '../store'; import * as actions from '../store/actions'; import * as selectors from '../store/selectors'; @@ -14,9 +15,11 @@ const MenuItem = ({ children, ...props }) => ( ); const MenuContent = () => { + const [location, setLocation] = useLocation(); + const go = page => { - actions.setPage(page); actions.setMenuOpen(false); + setLocation(page); }; return ( @@ -25,9 +28,9 @@ const MenuContent = () => {

Menu

    - {}}>Home - {}}>Lists - {}}>Settings + go('/')}>Home + go('/lists')}>Lists + go('/settings')}>Settings
); diff --git a/components/pages/ListDetail.jsx b/components/pages/ListDetail.jsx index 02b3aca..c7ba18e 100644 --- a/components/pages/ListDetail.jsx +++ b/components/pages/ListDetail.jsx @@ -42,6 +42,10 @@ const ListItemEntry = ({ list, item }) => ( const ListDetail = ({ selected }) => { const selectedList = Store.useState(selectors.getSelectedList); + usePage({ + title: selectedList.title, + }); + return ( @@ -49,8 +53,10 @@ const ListDetail = ({ selected }) => { { + /* actions.setSelectedList(null); actions.setPageById('lists'); + */ }} /> )} diff --git a/components/pages/Lists.jsx b/components/pages/Lists.jsx index 465d61c..d87e042 100644 --- a/components/pages/Lists.jsx +++ b/components/pages/Lists.jsx @@ -1,3 +1,4 @@ +import usePage from '../../hooks/usePage'; import Store from '../../store'; import * as actions from '../../store/actions'; import * as selectors from '../../store/selectors'; @@ -31,17 +32,23 @@ const AllLists = ({ onSelect }) => { }; const Lists = ({ selected }) => { + usePage({ + title: 'Lists', + }); + return ( - {selected && ( - { - actions.setSelectedList(list); - actions.setPageById('list-detail'); - }} - /> - )} + {/*selected && (*/} + { + /* + actions.setSelectedList(list); + actions.setPageById('list-detail'); + */ + }} + /> + {/*)}*/} ); diff --git a/components/pages/Settings.jsx b/components/pages/Settings.jsx index 3e3643f..7877e33 100644 --- a/components/pages/Settings.jsx +++ b/components/pages/Settings.jsx @@ -6,8 +6,13 @@ import Content from '../ui/Content'; import List from '../ui/List'; import ListItem from '../ui/ListItem'; import Toggle from '../ui/Toggle'; +import usePage from '../../hooks/usePage'; const Settings = ({ selected }) => { + usePage({ + title: 'Settings', + }); + const enableNotifications = Store.useState(); const settings = Store.useState(selectors.getSettings); diff --git a/components/ui/Tab.jsx b/components/ui/Tab.jsx index 5775478..9b3ec60 100644 --- a/components/ui/Tab.jsx +++ b/components/ui/Tab.jsx @@ -1,27 +1,36 @@ import classNames from 'classnames'; import Icon from './Icon'; import { Link } from 'wouter'; +import { useEffect, useState } from 'react'; -const Tab = ({ title, href, icon, selected, selectedIcon, onClick }) => ( - - - {icon && ( - - )} - - - -); +const Tab = ({ title, href, icon, selected, selectedIcon, onClick }) => { + console.log('TAB', href, selected); + const [update, setUpdate] = useState(false); + useEffect(() => { + setUpdate(true); + }, [selected]); + + return ( + + + {icon && ( + + )} + + + + ); +}; export default Tab; diff --git a/store/actions.js b/store/actions.js index 5aa67dc..9ba28fd 100644 --- a/store/actions.js +++ b/store/actions.js @@ -1,17 +1,5 @@ import Store from '.'; -export const setPageById = id => { - Store.update((s, o) => { - s.currentPage = o.pages.find(p => p.id === id); - }); -}; - -export const setPage = page => { - Store.update((s, o) => { - s.currentPage = page; - }); -}; - export const setMenuOpen = open => { Store.update(s => { s.menuOpen = open;