Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 686c706

Browse files
committed
Add user switcher.
1 parent 454038b commit 686c706

2 files changed

Lines changed: 58 additions & 14 deletions

File tree

apps/frontend/features/Tasks/index.tsx

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as O from "@effect-ts/core/Option"
22
import { Box, Hidden, Link, Typography } from "@material-ui/core"
33
import ArrowLeft from "@material-ui/icons/ArrowLeft"
44
import RouterLink from "next/link"
5-
import React from "react"
5+
import React, { useState } from "react"
66

7-
import { memo } from "@/data"
7+
import { memo, useEffect } from "@/data"
88
import { renderIf_ } from "@/utils"
99

1010
import FolderList from "./FolderList"
@@ -14,6 +14,21 @@ import { Ordery } from "./data"
1414

1515
import type { NonEmptyString, UUID } from "@effect-ts-demo/core/ext/Model"
1616

17+
const users = [
18+
{
19+
id: 0,
20+
name: "Patrick",
21+
},
22+
{
23+
id: 1,
24+
name: "Mike",
25+
},
26+
{
27+
id: 2,
28+
name: "Markus",
29+
},
30+
]
31+
1732
const TasksScreen = memo(function ({
1833
category,
1934
order,
@@ -23,20 +38,40 @@ const TasksScreen = memo(function ({
2338
order: O.Option<Ordery>
2439
taskId: O.Option<UUID>
2540
}) {
41+
const userId = useUserId()
2642
const view = O.isSome(taskId) ? "task" : O.isSome(category) ? "tasks" : "folders"
2743
return (
2844
<Box display="flex" flexDirection="column" height="100%">
29-
<Box sx={{ bgcolor: "primary.main", color: "primary.contrastText" }} p={2}>
30-
<Typography>
31-
To Do: An Effect-TS full-stack demo, clone of Microsoft To Do.{" "}
32-
<Link
33-
sx={{ color: "primary.contrastText" }}
34-
href="http://github.com/patroza/effect-ts-demo-todo"
35-
target="_blank"
36-
>
37-
<i>Fork Me</i>
38-
</Link>
39-
</Typography>
45+
<Box
46+
sx={{ bgcolor: "primary.main", color: "primary.contrastText" }}
47+
p={2}
48+
display="flex"
49+
>
50+
<Box flexGrow={1}>
51+
<Typography>
52+
To Do: An Effect-TS full-stack demo, clone of Microsoft To Do.{" "}
53+
<Link
54+
sx={{ color: "primary.contrastText" }}
55+
href="http://github.com/patroza/effect-ts-demo-todo"
56+
target="_blank"
57+
>
58+
<i>Fork Me</i>
59+
</Link>
60+
</Typography>
61+
</Box>
62+
<Box display="flex">
63+
Switch to
64+
{users
65+
.filter((u) => u.id.toString() !== userId)
66+
.map((u, idx) => (
67+
<React.Fragment key={u.id}>
68+
{idx ? "," : ":"}&nbsp;
69+
<Link href={`/set-user/${u.id}`} sx={{ color: "primary.contrastText" }}>
70+
{u.name}
71+
</Link>
72+
</React.Fragment>
73+
))}
74+
</Box>
4075
</Box>
4176
<Box display="flex" flexDirection={{ xs: "column", sm: "row" }} height="100%">
4277
<Hidden only={view === "folders" ? undefined : "xs"}>
@@ -109,4 +144,13 @@ const TasksScreen = memo(function ({
109144
)
110145
})
111146

147+
function useUserId() {
148+
const [userId, setUserId] = useState("0")
149+
useEffect(() => {
150+
setUserId(window.localStorage.getItem("userId") ?? "0")
151+
}, [])
152+
153+
return userId
154+
}
155+
112156
export default TasksScreen

apps/frontend/pages/set-user/[userId].tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const SetUser = () => {
77

88
useEffect(() => {
99
if (r.query.userId) {
10-
localStorage.setItem("user-id", r.query.userId as string)
10+
window.localStorage.setItem("user-id", r.query.userId as string)
1111
window.location.href = "/tasks"
1212
}
1313
}, [r.query.userId])

0 commit comments

Comments
 (0)