diff --git a/components/AppShell.jsx b/components/AppShell.jsx
index 2e87218..859257e 100644
--- a/components/AppShell.jsx
+++ b/components/AppShell.jsx
@@ -52,7 +52,7 @@ const CurrentPage = ({ page, pageProps = {} }) => {
};
const AppShell = ({ page, pageProps }) => {
- const [location, setLocation] = useLocation();
+ const [location] = useLocation();
const showMenu = Store.useState(selectors.getMenuOpen);
const showNotifications = Store.useState(selectors.getNotificationsOpen);
@@ -80,7 +80,7 @@ const AppShell = ({ page, pageProps }) => {
}
);
- console.log('Got location', location, '/settings' === location);
+ console.log('Got location', location);
// This is an example app layout. We've got a hidden menu that will be toggled
//
@@ -97,7 +97,6 @@ const AppShell = ({ page, pageProps }) => {
- {/**/}
{
selectedIcon={home}
title="Home"
href="/"
- selected={'/home' === location}
+ selected={'/' === location}
/>
(
);
-const ListDetail = ({ selected, list, listId, params }) => {
- const selectedList = Store.useState(selectors.getSelectedList);
+const ListDetail = ({ selected, listId, params }) => {
+ const lists = Store.useState(selectors.getLists);
+ const actualListId = listId ? listId : params?.listId || null;
+ const loadedList = lists.find(l => l.id === actualListId);
- const loadedList = list ? list : lists.find(l => l.id === params.listId);
-
- usePage({
- title: loadedList.title,
- });
+ usePage(
+ {
+ title: loadedList.name,
+ },
+ [loadedList]
+ );
return (
diff --git a/hooks/useLocation.js b/hooks/useLocation.js
index a396f0d..5385963 100644
--- a/hooks/useLocation.js
+++ b/hooks/useLocation.js
@@ -1,5 +1,29 @@
import { useEffect, useRef, useState, useCallback } from 'react';
+// useEffect(() => {
+// While History API does have `popstate` event, the only
+// proper way to listen to changes via `push/replaceState`
+// is to monkey-patch these methods.
+//
+// See https://stackoverflow.com/a/4585031
+if (typeof history !== 'undefined') {
+ for (const type of [eventPushState, eventReplaceState]) {
+ const original = history[type];
+
+ history[type] = function () {
+ const result = original.apply(this, arguments);
+ const event = new Event(type);
+ event.arguments = arguments;
+
+ dispatchEvent(event);
+ return result;
+ };
+ }
+} else {
+ console.log('No history API');
+}
+// });
+
/**
* History API docs @see https://developer.mozilla.org/en-US/docs/Web/API/History
*/
@@ -20,30 +44,6 @@ const useLocation = ({ base = '' } = {}) => {
const [path, update] = useState(() => getCurrentPathname(base)); // @see https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const prevPath = useRef(path);
- useEffect(() => {
- // While History API does have `popstate` event, the only
- // proper way to listen to changes via `push/replaceState`
- // is to monkey-patch these methods.
- //
- // See https://stackoverflow.com/a/4585031
- if (typeof history !== 'undefined') {
- for (const type of [eventPushState, eventReplaceState]) {
- const original = history[type];
-
- history[type] = function () {
- const result = original.apply(this, arguments);
- const event = new Event(type);
- event.arguments = arguments;
-
- dispatchEvent(event);
- return result;
- };
- }
- } else {
- console.log('No history API');
- }
- });
-
useEffect(() => {
// this function checks if the location has been changed since the
// last render and updates the state only when needed.
diff --git a/hooks/usePage.js b/hooks/usePage.js
index e86d87a..11dd438 100644
--- a/hooks/usePage.js
+++ b/hooks/usePage.js
@@ -1,12 +1,13 @@
import { useEffect } from 'react';
import Store from '../store';
-const usePage = fields => {
+const usePage = (fields, deps) => {
useEffect(() => {
+ console.log('Deps updating', fields, deps);
Store.update(s => {
s.currentPage = fields;
});
- }, [fields]);
+ }, [fields, deps]);
};
export default usePage;
diff --git a/pages/lists/[listId].js b/pages/lists/[listId].js
index 60c170e..5269337 100644
--- a/pages/lists/[listId].js
+++ b/pages/lists/[listId].js
@@ -1,9 +1,8 @@
import AppShell from '../../components/AppShell';
import ListDetailPage from '../../components/pages/ListDetail';
-import { lists } from '../../mock';
-export default function ListDetail({ list }) {
- return ;
+export default function ListDetail({ listId }) {
+ return ;
}
export const getServerSideProps = context => {
@@ -11,12 +10,9 @@ export const getServerSideProps = context => {
params: { listId },
} = context;
- const list = lists.find(l => l.id === listId);
- console.log('Loaded list', list);
-
return {
props: {
- list,
+ listId,
},
};
};
diff --git a/store/actions.js b/store/actions.js
index 9ba28fd..312affb 100644
--- a/store/actions.js
+++ b/store/actions.js
@@ -23,6 +23,7 @@ export const setSettings = settings => {
export const setDone = (list, item, done) => {
Store.update((s, o) => {
const listIndex = o.lists.findIndex(l => l === list);
+ console.log('Setting done', list, item, done, o.lists, listIndex);
const itemIndex = o.lists[listIndex].items.findIndex(i => i === item);
s.lists[listIndex].items[itemIndex].done = done;
if (list === o.selectedList) {
diff --git a/store/index.js b/store/index.js
index 9855774..fd5e08d 100644
--- a/store/index.js
+++ b/store/index.js
@@ -49,7 +49,6 @@ const Store = new PullStateStore({
currentPage: null,
homeItems,
lists,
- selectedList: null,
settings: {
enableNotifications: true,
},
diff --git a/store/selectors.js b/store/selectors.js
index 79fdcfd..ca77d16 100644
--- a/store/selectors.js
+++ b/store/selectors.js
@@ -9,5 +9,4 @@ export const getCurrentPage = createSelector(getState, state => state.currentPag
// App specific selectors
export const getHomeItems = createSelector(getState, state => state.homeItems);
export const getLists = createSelector(getState, state => state.lists);
-export const getSelectedList = createSelector(getState, state => state.selectedList);
export const getSettings = createSelector(getState, state => state.settings);