Skip to content

Commit 11bff33

Browse files
committed
Ported most of the JS modules to Typescript
1 parent 2377622 commit 11bff33

33 files changed

Lines changed: 462 additions & 261 deletions

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"react/prop-types": "off",
1111
"comma-dangle": "off",
1212
"no-use-before-define": "off",
13-
"@typescript-eslint/no-use-before-define": ["error"]
13+
"@typescript-eslint/no-use-before-define": ["error"],
14+
"no-unused-vars": "off",
15+
"@typescript-eslint/no-unused-vars": ["error"]
1416
}
1517
}

components/AddRule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const fields = [
2727
}
2828
]
2929

30-
const AddRule = ({ onAddRule }) => {
30+
const AddRule = () => {
3131
const formRef = useRef()
3232
const [error, setError] = useState(null)
3333
const router = useRouter()
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as React from 'react'
12
import { Flex, Box, Link } from 'ooni-components'
23
import styled from 'styled-components'
34

@@ -16,7 +17,12 @@ const FooterColumn = styled(Flex).attrs({
1617
mx: 3
1718
})``
1819

19-
const FooterItem = ({ label, link }) => (
20+
type FooterItemProps = {
21+
label: React.ReactNode,
22+
link: string
23+
}
24+
25+
const FooterItem: React.FunctionComponent<FooterItemProps> = ({ label, link }) => (
2026
<Link my={1} mx={3} color='gray2' href={link}>{label}</Link>
2127
)
2228

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import * as React from 'react'
22
import NextHead from 'next/head'
33
import styled from 'styled-components'
44
import { Container, Flex } from 'ooni-components'
@@ -11,14 +11,18 @@ const PageWrapper = styled(Flex)`
1111
min-height: 100vh;
1212
`
1313

14-
const Layout = ({ title = '', children }) => {
14+
type LayoutProps = {
15+
title: string,
16+
children: React.ReactNode
17+
}
18+
const Layout: React.FunctionComponent<LayoutProps> = ({ title = '', children }) => {
1519
return (
1620
<>
1721
<NextHead>
1822
<title>{title}</title>
1923
</NextHead>
2024
<PageWrapper>
21-
<NavBar title={title} />
25+
<NavBar />
2226
<Container sx={{ flex: 1, width: '100%' }}>
2327
{children}
2428
</Container>

components/List.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const Button = styled.button`
120120
// Dynamic button
121121
// * Starts editing a row
122122
// * Switches to a two button component to confirm or cancel a row edit operation.
123-
const EditButton = ({ resetRow, onRowUpdate, row: { index, values, state: { isEditing, dirty }, setState } }) => {
123+
const EditButton = ({ resetRow, onRowUpdate, row: { index, values, state: { isEditing, /* dirty */ }, setState } }) => {
124124
const onEdit = useCallback(() => {
125125
// TODO: Don't edit if another row is still being edited
126126
setState(state => ({ ...state, isEditing: true }))

components/Loading.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import styled, { keyframes } from 'styled-components'
33

4-
const rotate = (dir) => keyframes`
4+
const rotate = (dir: number) => keyframes`
55
from {
66
transform: rotate(0deg);
77
}
@@ -19,12 +19,12 @@ const Spinner = styled.div<SpinnerProps>`
1919
animation: ${props => rotate(props.$dir)} ${props => 2 / props.speed}s linear infinite;
2020
`
2121

22-
interface LoadingProps {
23-
readonly size?: number;
24-
readonly speed?: number;
25-
readonly dir?: number;
22+
type LoadingProps = {
23+
size?: number;
24+
speed?: number;
25+
dir?: number;
2626
}
27-
const Loading: React.FC<LoadingProps> = ({ size = 64, speed = 1, dir = 1 }) => (
27+
const Loading: React.FunctionComponent<LoadingProps> = ({ size = 64, speed = 1, dir = 1 }) => (
2828
<Spinner speed={speed} $dir={dir}>
2929
<svg
3030
xmlns="http://www.w3.org/2000/svg"
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,39 @@ const StyledInputContainer = styled(Box).attrs({
2222
}
2323
`
2424

25-
export const LoginModal = ({ isShowing, hide, onLogin }) =>
26-
<Modal show={isShowing} onHideClick={hide}>
27-
<LoginForm onLogin={onLogin} />
28-
</Modal>
25+
type LoginFormData = {
26+
email_address: string,
27+
nickname: string
28+
}
2929

30-
export const LoginForm = ({ onLogin }) => {
31-
const [submitting, setSubmitting] = useState(false)
32-
const [loginError, setError] = useState(null)
30+
type LoginFormProps = {
31+
onLogin: Function
32+
}
3333

34-
const { handleSubmit, register, formState, reset } = useForm({
34+
export const LoginForm: React.FunctionComponent<LoginFormProps> = ({ onLogin }) => {
35+
const [submitting, setSubmitting] = useState(false)
36+
const [loginError, setError] = useState<string | null>(null)
37+
const { handleSubmit, register, formState, reset } = useForm<LoginFormData>({
3538
mode: 'onTouched',
3639
})
3740

3841
const { errors, isValid, isDirty } = formState
3942

40-
const onSubmit = useCallback((data) => {
43+
const onSubmit = useCallback((data: LoginFormData) => {
4144
const { email_address, nickname } = data
42-
const registerApi = async (email_address, nickname) => {
45+
const registerApi = async (email_address: string, nickname: string) => {
4346
try {
4447
await registerUser(email_address, nickname)
4548
if (typeof onLogin === 'function') {
4649
onLogin()
4750
}
4851
} catch (e) {
49-
setError(e.message)
5052
// Reset form to mark `isDirty` as false
5153
reset({}, { keepValues: true })
54+
console.error(e)
55+
if (e instanceof Error) {
56+
setError(e.message)
57+
}
5258
} finally {
5359
setSubmitting(false)
5460
}
@@ -119,4 +125,14 @@ export const LoginForm = ({ onLogin }) => {
119125
)
120126
}
121127

128+
type LoginModalProps = {
129+
isShowing: boolean,
130+
hide: Function,
131+
onLogin: Function
132+
}
133+
export const LoginModal: React.FunctionComponent<LoginModalProps> = ({ isShowing, hide, onLogin }) =>
134+
<Modal show={isShowing} onHideClick={hide}>
135+
<LoginForm onLogin={onLogin} />
136+
</Modal>
137+
122138
export default LoginForm

components/lib/api.js

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)