Skip to content

Commit b5eec09

Browse files
committed
Fix lint issues
1 parent cb6ad18 commit b5eec09

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

site/api.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@ export const login = async (email: string, password: string): Promise<LoginRespo
1414
}),
1515
})
1616

17-
return await readOrThrowResponse(response)
17+
const body = await response.json()
18+
if (!response.ok) {
19+
throw new Error(body.message)
20+
}
21+
22+
return body
1823
}
1924

2025
export const logout = async (): Promise<void> => {
2126
const response = await fetch("/api/v2/logout", {
2227
method: "POST",
2328
})
2429

25-
return await readOrThrowResponse(response)
26-
}
27-
28-
const readOrThrowResponse = async (response: Response): Promise<any> => {
29-
const body = await response.json()
3030
if (!response.ok) {
31+
const body = await response.json()
3132
throw new Error(body.message)
3233
}
3334

34-
return body
35+
return
3536
}

site/components/Navbar/UserDropdown.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import ListItemIcon from "@material-ui/core/ListItemIcon"
44
import ListItemText from "@material-ui/core/ListItemText"
55
import MenuItem from "@material-ui/core/MenuItem"
66
import { fade, makeStyles } from "@material-ui/core/styles"
7-
//import AccountIcon from "@material-ui/icons/AccountCircleOutlined"
87
import KeyboardArrowDown from "@material-ui/icons/KeyboardArrowDown"
98
import KeyboardArrowUp from "@material-ui/icons/KeyboardArrowUp"
109
import React, { useState } from "react"
@@ -15,8 +14,6 @@ import { UserProfileCard } from "../User/UserProfileCard"
1514
import { User } from "../../contexts/UserContext"
1615
import Divider from "@material-ui/core/Divider"
1716

18-
const navHeight = 56
19-
2017
export interface UserDropdownProps {
2118
user: User
2219
onSignOut: () => void
@@ -33,13 +30,10 @@ export const UserDropdown: React.FC<UserDropdownProps> = ({ user, onSignOut }: U
3330
setAnchorEl(undefined)
3431
}
3532

36-
// TODO: what does this do?
37-
const isSelected = false
38-
3933
return (
4034
<>
4135
<div>
42-
<MenuItem onClick={handleDropdownClick} selected={isSelected}>
36+
<MenuItem onClick={handleDropdownClick}>
4337
<div className={styles.inner}>
4438
{user && (
4539
<Badge overlap="circle">
@@ -73,7 +67,7 @@ export const UserDropdown: React.FC<UserDropdownProps> = ({ user, onSignOut }: U
7367
>
7468
{user && (
7569
<div className={styles.userInfo}>
76-
<UserProfileCard user={user} onAvatarClick={onPopoverClose} />
70+
<UserProfileCard user={user} />
7771

7872
<Divider className={styles.divider} />
7973

site/components/Navbar/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import React from "react"
22
import Button from "@material-ui/core/Button"
3-
import List from "@material-ui/core/List"
4-
import ListSubheader from "@material-ui/core/ListSubheader"
53
import { makeStyles } from "@material-ui/core/styles"
64
import Link from "next/link"
75

@@ -11,9 +9,10 @@ import { UserDropdown } from "./UserDropdown"
119

1210
export interface NavbarProps {
1311
user?: User
12+
onSignOut: () => void
1413
}
1514

16-
export const Navbar: React.FC<NavbarProps> = ({ user }) => {
15+
export const Navbar: React.FC<NavbarProps> = ({ user, onSignOut }) => {
1716
const styles = useStyles()
1817
return (
1918
<div className={styles.root}>
@@ -25,7 +24,7 @@ export const Navbar: React.FC<NavbarProps> = ({ user }) => {
2524
</Link>
2625
</div>
2726
<div className={styles.fullWidth} />
28-
<div className={styles.fixed}>{user && <UserDropdown user={user} onSignOut={() => alert("sign out")} />}</div>
27+
<div className={styles.fixed}>{user && <UserDropdown user={user} onSignOut={onSignOut} />}</div>
2928
</div>
3029
)
3130
}

site/contexts/UserContext.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useRouter } from "next/router"
22
import React, { useContext, useEffect } from "react"
33
import useSWR from "swr"
44

5+
import * as API from "../api"
6+
57
export interface User {
68
readonly id: string
79
readonly username: string
@@ -12,9 +14,14 @@ export interface User {
1214
export interface UserContext {
1315
readonly error?: Error
1416
readonly me?: User
17+
readonly signOut: () => Promise<void>
1518
}
1619

17-
const UserContext = React.createContext<UserContext>({})
20+
const UserContext = React.createContext<UserContext>({
21+
signOut: () => {
22+
return Promise.reject("Sign out API not available")
23+
},
24+
})
1825

1926
export const useUser = (redirectOnError = false): UserContext => {
2027
const ctx = useContext(UserContext)
@@ -36,13 +43,27 @@ export const useUser = (redirectOnError = false): UserContext => {
3643
}
3744

3845
export const UserProvider: React.FC = (props) => {
39-
const { data, error } = useSWR("/api/v2/user")
46+
const router = useRouter()
47+
const { data, error, mutate } = useSWR("/api/v2/user")
48+
49+
const signOut = async () => {
50+
await API.logout()
51+
// Tell SWR to invalidate the cache for the user endpoint
52+
mutate("/api/v2/user")
53+
router.push({
54+
pathname: "/login",
55+
query: {
56+
redirect: router.asPath,
57+
},
58+
})
59+
}
4060

4161
return (
4262
<UserContext.Provider
4363
value={{
4464
error: error,
4565
me: data,
66+
signOut: signOut,
4667
}}
4768
>
4869
{props.children}

site/pages/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { FullScreenLoader } from "../components/Loader/FullScreenLoader"
1212

1313
const WorkspacesPage: React.FC = () => {
1414
const styles = useStyles()
15-
const { me } = useUser(true)
15+
const { me, signOut } = useUser(true)
1616

1717
if (!me) {
1818
return <FullScreenLoader />
@@ -29,7 +29,7 @@ const WorkspacesPage: React.FC = () => {
2929

3030
return (
3131
<div className={styles.root}>
32-
<Navbar user={me} />
32+
<Navbar user={me} onSignOut={signOut} />
3333
<div className={styles.header}>
3434
<SplitButton<string>
3535
color="primary"

0 commit comments

Comments
 (0)