Skip to content

Commit aa6cbb7

Browse files
committed
Simplify and fix mobile compatibility
1 parent ab876df commit aa6cbb7

6 files changed

Lines changed: 31 additions & 20 deletions

File tree

components/JottingsControls/jottingsControl.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ export default function JottingsControl(props) {
1515
const [tasks, setTasks] = useState(null);
1616
const [labels, setLabels] = useState(null);
1717

18-
const { width } = useWindowSize();
19-
const [activeList, setActiveList] = useState('labels');
20-
2118
const router = useRouter();
19+
const [matches, setMatches] = useState(
20+
window.matchMedia("(min-width: 800px)").matches
21+
)
2222

23-
useEffect(() => {
24-
if (width >= 800) {
25-
setActiveList('all');
26-
} else {
27-
setActiveList('labels');
28-
}
29-
}, [width]);
23+
useEffect(() => {
3024

31-
useEffect(() => {
32-
setActiveList(router.query.list || 'labels');
25+
const handleResize = e => {
26+
setMatches(e.matches);
27+
};
3328

34-
}, [router.query]);
29+
window
30+
.matchMedia("(min-width: 800px)")
31+
.addEventListener('change', handleResize);
32+
33+
return () => { removeEventListener('change', handleResize); };
34+
}, []);
3535

3636
const getJotsToShow = (response, jotType) => {
3737
if (
@@ -109,13 +109,16 @@ export default function JottingsControl(props) {
109109
useEffect(() => {
110110
makeLabelsRequest();
111111
makeJottingsRequests();
112+
console.log(router.asPath);
112113
}, []);
113114

115+
const showList = list => router.asPath.includes(`list=${list}`)
116+
114117
return (
115118
<>
116-
<LabelsControl active={['labels', 'all'].includes(activeList)} labelsState={[labels, setLabels]} />
117-
<NotesControl active={['notes', 'all'].includes(activeList)} notesState={[notes, setNotes]} />
118-
<TasksControl active={['tasks', 'all'].includes(activeList)} tasksState={[tasks, setTasks]} />
119+
<LabelsControl active={router.asPath.includes(`list=labels`) || !router.asPath.includes('list=')} labelsState={[labels, setLabels]} />
120+
<NotesControl active={showList('notes')} notesState={[notes, setNotes]} />
121+
<TasksControl active={showList('tasks')} tasksState={[tasks, setTasks]} />
119122

120123
<NoteControl notes={notes} />
121124
<TaskControl tasks={tasks} />

components/JottingsControls/jottingsControl.module.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
}
44

55
.labelList, .ownNoteList, .ownTaskList {
6-
display: none;
76
grid-row: 2;
87
grid-column: 2 / -1;
8+
display: none;
99
}
1010

1111
.labelList h1, .ownNoteList h1, .ownTaskList h1 {
@@ -18,6 +18,10 @@
1818
}
1919

2020
@media screen and (min-width: 800px) {
21+
.labelList h1, .ownNoteList h1, .ownTaskList h1, .labelList, .ownNoteList, .ownTaskList {
22+
display: block;
23+
}
24+
2125
.ownNoteList {
2226
grid-column: 2 / 5;
2327
}

components/JottingsControls/labelsControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function LabelsControl({labelsState, active}) {
1717
const [labels, setLabels] = labelsState;
1818

1919
return (
20-
<article style={{display: active ? "block" : "none"}} className={jottingsControl.labelList}>
20+
<article className={`${active ? "active" : ""} ${jottingsControl.labelList}`}>
2121
<h1>Labels</h1>
2222
{labels ? <LabelList labels={labels} /> : <ProgressSpinner />}
2323
{/* <CreateNoteButton jots={notes} /> */}

components/JottingsControls/notesControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function NotesControl({notesState, active}) {
1616
const [notes, setNotes] = notesState;
1717

1818
return (
19-
<article style={{display: active ? 'block' : 'none'}} className={jottingsControl.ownNoteList}>
19+
<article className={`${active ? "active" : ""} ${jottingsControl.ownNoteList}`}>
2020
<h1>Notes</h1>
2121
{notes ? <NoteList notes={notes} /> : <ProgressSpinner />}
2222
<CreateNoteButton jots={notes} />

components/JottingsControls/tasksControl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function TasksControl({ tasksState, active }) {
1515
const [tasks, setTasks] = tasksState;
1616

1717
return (
18-
<article style={{display: active ? 'block' : 'none'}} className={jottingsControl.ownTaskList}>
18+
<article className={`${active ? "active" : ""} ${jottingsControl.ownTaskList}`}>
1919
<h1>Tasks</h1>
2020
{tasks ? <TaskList tasks={tasks} /> : <ProgressSpinner />}
2121
<CreateTaskButton jots={tasks} />

styles/globals.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ button:hover {
5959
opacity: 0;
6060
}
6161
}
62+
63+
article.active {
64+
display: block;
65+
}

0 commit comments

Comments
 (0)