From 06c1a8f71d04112d8925cb7e35b48a2a228722cd Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Thu, 24 Dec 2020 15:56:08 -0600 Subject: [PATCH] Refactor state and make list detail a page --- README.md | 1 + components/MenuContent.jsx | 25 ++++++++------ components/pages/Home.jsx | 5 +-- components/pages/ListDetail.jsx | 60 ++++++++++++++++++++++++++++++++ components/pages/Lists.jsx | 61 +++++---------------------------- components/ui/Nav.jsx | 24 +++++-------- package-lock.json | 6 ++++ package.json | 3 +- pages/index.js | 47 +++++++++---------------- store/actions.js | 32 +++++++++++++++++ store/index.js | 18 ++++++++-- store/selectors.js | 15 ++++++++ 12 files changed, 182 insertions(+), 115 deletions(-) create mode 100644 components/pages/ListDetail.jsx create mode 100644 store/selectors.js diff --git a/README.md b/README.md index 34eee74..982a457 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ There are currently snippets for the following common mobile components: - [x] Content - [x] Tabs - [ ] Nav (in progress) +- [ ] Next.js router integration - [x] Icon - [x] Menu - [x] Modal diff --git a/components/MenuContent.jsx b/components/MenuContent.jsx index 050d216..08f06c6 100644 --- a/components/MenuContent.jsx +++ b/components/MenuContent.jsx @@ -1,4 +1,6 @@ import Store from '../store'; +import * as actions from '../store/actions'; +import * as selectors from '../store/selectors'; const MenuItem = ({ children, ...props }) => (
  • @@ -12,14 +14,11 @@ const MenuItem = ({ children, ...props }) => ( ); const MenuContent = () => { - const pages = Store.useState(s => s.pages); + const menuLinks = Store.useState(selectors.getMenuLinks); const go = page => { - Store.update(s => { - console.log('Going to', page); - s.currentPage = page; - s.showMenu = false; - }); + actions.setPage(page); + actions.setMenuOpen(false); }; return ( @@ -28,11 +27,15 @@ const MenuContent = () => {

    Menu

    ); diff --git a/components/pages/Home.jsx b/components/pages/Home.jsx index a0f2603..31d2749 100644 --- a/components/pages/Home.jsx +++ b/components/pages/Home.jsx @@ -1,8 +1,9 @@ -import { homeItems } from '../../data'; import Store from '../../store'; import Card from '../ui/Card'; import Content from '../ui/Content'; +import * as selectors from '../../store/selectors'; + const HomeCard = ({ title, type, text, author, image }) => (
    @@ -17,7 +18,7 @@ const HomeCard = ({ title, type, text, author, image }) => ( ); const Home = ({ selected }) => { - const homeItems = Store.useState(s => s.homeItems); + const homeItems = Store.useState(selectors.getHomeItems); return ( diff --git a/components/pages/ListDetail.jsx b/components/pages/ListDetail.jsx new file mode 100644 index 0000000..54e597b --- /dev/null +++ b/components/pages/ListDetail.jsx @@ -0,0 +1,60 @@ +import Store from '../../store'; +import * as actions from '../../store/actions'; +import * as selectors from '../../store/selectors'; + +import Content from '../ui/Content'; +import List from '../ui/List'; +import VirtualScroll from '../ui/VirtualScroll'; + +const ListItems = ({ list, onClose }) => { + return ( + <> + + } + /> + + ); +}; + +const ListItemEntry = ({ list, item }) => ( +
    + {item.name} + { + actions.setDone(list, item, !item.done); + }} + /> +
    +); + +const ListDetail = ({ selected }) => { + const selectedList = Store.useState(selectors.getSelectedList); + + return ( + + + {selected && ( + { + actions.setSelectedList(null); + actions.setPageById('lists'); + }} + /> + )} + + + ); +}; + +export default ListDetail; diff --git a/components/pages/Lists.jsx b/components/pages/Lists.jsx index 6f9237d..42d92fb 100644 --- a/components/pages/Lists.jsx +++ b/components/pages/Lists.jsx @@ -1,11 +1,9 @@ -import { useState } from 'react'; import Store from '../../store'; -import { setDone } from '../../store/actions'; -import Card from '../ui/Card'; +import * as actions from '../../store/actions'; +import * as selectors from '../../store/selectors'; import Content from '../ui/Content'; import List from '../ui/List'; -import ListItem from '../ui/ListItem'; import VirtualScroll from '../ui/VirtualScroll'; const ListEntry = ({ list, ...props }) => ( @@ -15,7 +13,7 @@ const ListEntry = ({ list, ...props }) => ( ); const AllLists = ({ onSelect }) => { - const lists = Store.useState(s => s.lists); + const lists = Store.useState(selectors.getLists); return ( { ); }; -const ListItems = ({ list, onClose }) => { - return ( - <> - - } - /> - - ); -}; - -const ListItemEntry = ({ list, item }) => ( -
    - {item.name} - { - setDone(list, item, !item.done); - }} - /> -
    -); - -const Feed = ({ selected }) => { - const selectedList = Store.useState(s => s.selectedList); - +const Lists = ({ selected }) => { return ( - {selected && selectedList ? ( - - Store.update(s => { - s.selectedList = null; - }) - } - /> - ) : ( + {selected && ( { - Store.update(s => { - s.selectedList = list; - }); + actions.setSelectedList(list); + actions.setPageById('list-detail'); }} /> )} @@ -89,4 +44,4 @@ const Feed = ({ selected }) => { ); }; -export default Feed; +export default Lists; diff --git a/components/ui/Nav.jsx b/components/ui/Nav.jsx index e43d155..d960f6c 100644 --- a/components/ui/Nav.jsx +++ b/components/ui/Nav.jsx @@ -1,10 +1,12 @@ import { useEffect, useState } from 'react'; import { Plugins } from '@capacitor/core'; -import Store from '../../store'; +import * as actions from '../../store/actions'; const Nav = ({ page }) => { - const [showMenu, setShowMenu] = useState(false); + const [showProfileMenu, setShowProfileMenu] = useState(false); + + const title = typeof page.title === 'function' ? page.title() : page.title; useEffect(() => { Plugins.StatusBar.setStyle({ @@ -23,11 +25,7 @@ const Nav = ({ page }) => {
    - Store.update(s => { - s.showMenu = true; - }) - } + onClick={() => actions.setMenuOpen(true)} > {/* Mobile menu button*/}
    -

    {page.title}

    +

    {title}

    @@ -116,11 +114,7 @@ const Nav = ({ page }) => {