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

Commit 9899082

Browse files
authored
Use setState updater to access state (#32)
1 parent 03d7edb commit 9899082

File tree

17 files changed

+276
-259
lines changed

17 files changed

+276
-259
lines changed

client/src/auth/Provider.spec.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mount } from 'enzyme'
22
import React, { SFC } from 'react'
3-
import context, { Status } from './context'
3+
import { Status } from '~interfaces'
4+
import context from './context'
45
import Provider from './Provider'
56

67
describe('<Provider />', () => {
@@ -61,16 +62,13 @@ describe('<Provider />', () => {
6162

6263
expectInitialValue(value)
6364

64-
value
65-
.signIn(email, password)
66-
.then(() => callback(wrapper))
65+
value.signIn(email, password).then(() => callback(wrapper))
6766

6867
value = getValue(wrapper)
6968

7069
expect(value.signedIn).toBe(false)
7170
expect(value.user).toBeNull()
7271
expect(value.status).toBe(Status.Pending)
73-
7472
}
7573

7674
it('when signIn() succeeds', done => {

client/src/auth/Provider.tsx

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react'
2-
import context, { defaultValue, SignIn, SignOut, Status, Value } from './context'
2+
import { Status } from '~interfaces'
3+
import context, { defaultValue, SignIn, SignOut, Value } from './context'
34
import { signIn } from './service'
45

56
export default class Provider extends Component<{}, ProviderState> {
@@ -21,17 +22,19 @@ export default class Provider extends Component<{}, ProviderState> {
2122
)
2223
}
2324

24-
private signIn: SignIn = async (email, password) => {
25-
this.setState({
26-
status: Status.Pending
27-
})
28-
29-
const maybeUser = await signIn(email, password)
30-
31-
this.setState({
32-
signedIn: Boolean(maybeUser),
33-
status: maybeUser ? Status.Success : Status.Failure,
34-
user: maybeUser
25+
private signIn: SignIn = (email, password) => {
26+
return new Promise(resolve => {
27+
this.setState({
28+
status: Status.Pending
29+
}, async () => {
30+
const maybeUser = await signIn(email, password)
31+
32+
this.setState({
33+
signedIn: Boolean(maybeUser),
34+
status: maybeUser ? Status.Success : Status.Failure,
35+
user: maybeUser
36+
}, () => resolve())
37+
})
3538
})
3639
}
3740

client/src/auth/context.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Context, createContext } from 'react'
2-
import { User } from '~interfaces'
2+
import { Status, User } from '~interfaces'
33

44
export const defaultValue: Value = {
55
signIn: () => new Promise<void>(resolve => resolve()),
@@ -17,12 +17,6 @@ export type SignIn = (email: string, password: string) => Promise<void>
1717
export type SignOut = () => void
1818
export type MaybeUser = User | null
1919

20-
export enum Status {
21-
Success = 'success',
22-
Failure = 'failure',
23-
Pending = 'pending'
24-
}
25-
2620
export interface Value {
2721
signedIn: boolean,
2822
status: Status | undefined,

client/src/auth/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Status } from './context'
21
import Provider from './Provider'
32
import withAuth, { WithAuth } from './withAuth'
43

5-
export { Provider, withAuth, Status, WithAuth }
4+
export { Provider, withAuth, WithAuth }

client/src/generic/Header/Header.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ class Header extends Component<HeaderProps, HeaderState> {
100100
}
101101

102102
private closeMenu: () => void = () => {
103-
this.setState({
104-
anchorElement: undefined
105-
})
103+
this.setState({ anchorElement: undefined })
106104
}
107105

108106
private onSignOut: () => void = () => {

client/src/generic/Header/SearchBar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ const styles = createStyles({
1414
}
1515
})
1616

17-
export default withStyles(styles)(SearchBar)
17+
interface SearchBarProps extends WithStyles<typeof styles> {}
1818

19-
interface SearchBarProps extends WithStyles <typeof styles> {}
19+
export default withStyles(styles)(SearchBar)

client/src/generic/SelectCategory.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,6 @@ import { createStyles, StyleRules, withStyles, WithStyles } from '@material-ui/c
33
import React, { SFC } from 'react'
44
import { Category, CategoryList } from '~interfaces'
55

6-
const styles: StyleRules = createStyles({
7-
container: {
8-
height: '100%',
9-
overflow: 'auto'
10-
},
11-
item: {
12-
display: 'block',
13-
textAlign: 'center'
14-
},
15-
list: {
16-
height: 0
17-
}
18-
})
19-
20-
export interface SelectCategoryProps extends WithStyles<typeof styles> {
21-
categories: CategoryList,
22-
onClick: (id: Category['id']) => void,
23-
value: Category['id']
24-
}
25-
266
const SelectCategory: SFC<SelectCategoryProps> = ({
277
categories,
288
classes: { container, item, list },
@@ -49,4 +29,24 @@ const SelectCategory: SFC<SelectCategoryProps> = ({
4929
</div>
5030
)
5131

32+
const styles: StyleRules = createStyles({
33+
container: {
34+
height: '100%',
35+
overflow: 'auto'
36+
},
37+
item: {
38+
display: 'block',
39+
textAlign: 'center'
40+
},
41+
list: {
42+
height: 0
43+
}
44+
})
45+
46+
export interface SelectCategoryProps extends WithStyles<typeof styles> {
47+
categories: CategoryList,
48+
onClick: (id: Category['id']) => void,
49+
value: Category['id']
50+
}
51+
5252
export default withStyles(styles)(SelectCategory)

client/src/generic/SelectTags.tsx

+18-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import { Chip } from '@material-ui/core'
22
import { createStyles, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles'
33
import React, { SFC } from 'react'
44

5+
const SelectTags: SFC<SelectTagsProps> = ({
6+
tags,
7+
classes: { container, item, selected, clickable },
8+
onClick
9+
}) => (
10+
<div className={container}>
11+
{tags.map((tag, i) => (
12+
<Chip
13+
clickable
14+
key={i}
15+
label={tag.name}
16+
className={`${item} ${clickable} ${tag.selected ? selected : ''}`}
17+
onClick={onClick.bind(null, tag.name)}
18+
/>
19+
))}
20+
</div>
21+
)
22+
523
const styles: StyleRulesCallback = ({
624
spacing,
725
palette: { primary: { light } }
@@ -38,22 +56,4 @@ export interface SelectTagsProps extends WithStyles<typeof styles> {
3856
onClick: (name: Tag['name']) => void
3957
}
4058

41-
const SelectTags: SFC<SelectTagsProps> = ({
42-
tags,
43-
classes: { container, item, selected, clickable },
44-
onClick
45-
}) => (
46-
<div className={container}>
47-
{tags.map((tag, i) => (
48-
<Chip
49-
clickable
50-
key={i}
51-
label={tag.name}
52-
className={`${item} ${clickable} ${tag.selected ? selected : ''}`}
53-
onClick={onClick.bind(null, tag.name)}
54-
/>
55-
))}
56-
</div>
57-
)
58-
5959
export default withStyles(styles)(SelectTags)

client/src/generic/TaskList/TaskList.tsx

+17-21
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,11 @@ import React from 'react'
33
import { Task } from '~interfaces'
44
import TaskListElement from './TaskListElement'
55

6-
const styles: StyleRulesCallback = () => createStyles({
7-
container: {
8-
height: '100%',
9-
margin: 0,
10-
overflow: 'auto'
11-
},
12-
grid: {
13-
height: 0,
14-
margin: 0,
15-
width: '100%'
16-
},
17-
gridItem: {
18-
// height: '250px'
19-
}
20-
})
21-
22-
interface TaskListProps extends WithStyles<typeof styles> {
23-
tasks: Task[]
24-
}
25-
266
const TaskList = ({ classes, tasks }: TaskListProps) => (
277
<div className={classes.container}>
288
<Grid classes={{ container: classes.grid }} spacing={8} container>
299
{tasks.map((task, i) => (
3010
<Grid
31-
className={classes.gridItem}
3211
item
3312
xs={12}
3413
sm={6}
@@ -42,4 +21,21 @@ const TaskList = ({ classes, tasks }: TaskListProps) => (
4221
</div>
4322
)
4423

24+
const styles: StyleRulesCallback = () => createStyles({
25+
container: {
26+
height: '100%',
27+
margin: 0,
28+
overflow: 'auto'
29+
},
30+
grid: {
31+
height: 0,
32+
margin: 0,
33+
width: '100%'
34+
}
35+
})
36+
37+
interface TaskListProps extends WithStyles<typeof styles> {
38+
tasks: Task[]
39+
}
40+
4541
export default withStyles(styles)(TaskList)

client/src/interfaces/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ interface User {
2020
email: string
2121
}
2222

23+
enum Status {
24+
Success = 'success',
25+
Failure = 'failure',
26+
Pending = 'pending'
27+
}
28+
2329
export {
2430
Category,
2531
CategoryList,
32+
Status,
2633
Task,
2734
TagList,
2835
User

client/src/pages/AddTask/components/AddTask.tsx

+34-34
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,6 @@ import { SelectCategory, SelectCategoryProps, SelectTags, SelectTagsProps } from
55
import CapturePhoto, { CapturePhotoProps } from './CapturePhoto'
66
import Description, { DescriptionProps } from './Description'
77

8-
const styles: StyleRulesCallback = () => createStyles({
9-
addTaskContainer: {
10-
display: 'grid',
11-
gridTemplateRows: 'auto max-content'
12-
},
13-
container: {
14-
display: 'grid',
15-
gridTemplateRows: 'max-content auto'
16-
},
17-
content: {
18-
display: 'grid',
19-
gridTemplateRows: '50% 50%'
20-
},
21-
item: {
22-
justifyContent: 'center',
23-
maxWidth: '100%'
24-
}
25-
})
26-
27-
export interface AddTaskProps extends WithStyles<typeof styles> {
28-
categories: SelectCategoryProps['categories'],
29-
onClickCategory: SelectCategoryProps['onClick'],
30-
onClickTag: SelectTagsProps['onClick'],
31-
onSubmit: MouseEventHandler,
32-
onCapturePhoto: CapturePhotoProps['onCapture'],
33-
onChangeDescription: DescriptionProps['onChange'],
34-
onChangeTab: (e: ChangeEvent<{}>, value: number) => void,
35-
categoryValue: SelectCategoryProps['value'],
36-
descriptionValue: DescriptionProps['value'],
37-
image: CapturePhotoProps['imgSrc'],
38-
tags: SelectTagsProps['tags'],
39-
tabValue: boolean | number
40-
}
41-
428
const AddTask: SFC<AddTaskProps> = ({
439
classes: { addTaskContainer, container, content, item },
4410
categories, onClickCategory, onChangeDescription, onChangeTab, onCapturePhoto,
@@ -74,4 +40,38 @@ const AddTask: SFC<AddTaskProps> = ({
7440
</div>
7541
)
7642

43+
const styles: StyleRulesCallback = () => createStyles({
44+
addTaskContainer: {
45+
display: 'grid',
46+
gridTemplateRows: 'auto max-content'
47+
},
48+
container: {
49+
display: 'grid',
50+
gridTemplateRows: 'max-content auto'
51+
},
52+
content: {
53+
display: 'grid',
54+
gridTemplateRows: '50% 50%'
55+
},
56+
item: {
57+
justifyContent: 'center',
58+
maxWidth: '100%'
59+
}
60+
})
61+
62+
export interface AddTaskProps extends WithStyles<typeof styles> {
63+
categories: SelectCategoryProps['categories'],
64+
onClickCategory: SelectCategoryProps['onClick'],
65+
onClickTag: SelectTagsProps['onClick'],
66+
onSubmit: MouseEventHandler,
67+
onCapturePhoto: CapturePhotoProps['onCapture'],
68+
onChangeDescription: DescriptionProps['onChange'],
69+
onChangeTab: (e: ChangeEvent<{}>, value: number) => void,
70+
categoryValue: SelectCategoryProps['value'],
71+
descriptionValue: DescriptionProps['value'],
72+
image: CapturePhotoProps['imgSrc'],
73+
tags: SelectTagsProps['tags'],
74+
tabValue: boolean | number
75+
}
76+
7777
export default withStyles(styles)(AddTask)

0 commit comments

Comments
 (0)