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

Use setState updater to access state #32

Merged
merged 2 commits into from
Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions client/src/auth/Provider.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mount } from 'enzyme'
import React, { SFC } from 'react'
import context, { Status } from './context'
import { Status } from '~interfaces'
import context from './context'
import Provider from './Provider'

describe('<Provider />', () => {
Expand Down Expand Up @@ -61,16 +62,13 @@ describe('<Provider />', () => {

expectInitialValue(value)

value
.signIn(email, password)
.then(() => callback(wrapper))
value.signIn(email, password).then(() => callback(wrapper))

value = getValue(wrapper)

expect(value.signedIn).toBe(false)
expect(value.user).toBeNull()
expect(value.status).toBe(Status.Pending)

}

it('when signIn() succeeds', done => {
Expand Down
27 changes: 15 additions & 12 deletions client/src/auth/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react'
import context, { defaultValue, SignIn, SignOut, Status, Value } from './context'
import { Status } from '~interfaces'
import context, { defaultValue, SignIn, SignOut, Value } from './context'
import { signIn } from './service'

export default class Provider extends Component<{}, ProviderState> {
Expand All @@ -21,17 +22,19 @@ export default class Provider extends Component<{}, ProviderState> {
)
}

private signIn: SignIn = async (email, password) => {
this.setState({
status: Status.Pending
})

const maybeUser = await signIn(email, password)

this.setState({
signedIn: Boolean(maybeUser),
status: maybeUser ? Status.Success : Status.Failure,
user: maybeUser
private signIn: SignIn = (email, password) => {
return new Promise(resolve => {
this.setState({
status: Status.Pending
}, async () => {
const maybeUser = await signIn(email, password)

this.setState({
signedIn: Boolean(maybeUser),
status: maybeUser ? Status.Success : Status.Failure,
user: maybeUser
}, () => resolve())
})
})
}

Expand Down
8 changes: 1 addition & 7 deletions client/src/auth/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, createContext } from 'react'
import { User } from '~interfaces'
import { Status, User } from '~interfaces'

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

export enum Status {
Success = 'success',
Failure = 'failure',
Pending = 'pending'
}

export interface Value {
signedIn: boolean,
status: Status | undefined,
Expand Down
3 changes: 1 addition & 2 deletions client/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Status } from './context'
import Provider from './Provider'
import withAuth, { WithAuth } from './withAuth'

export { Provider, withAuth, Status, WithAuth }
export { Provider, withAuth, WithAuth }
4 changes: 1 addition & 3 deletions client/src/generic/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ class Header extends Component<HeaderProps, HeaderState> {
}

private closeMenu: () => void = () => {
this.setState({
anchorElement: undefined
})
this.setState({ anchorElement: undefined })
}

private onSignOut: () => void = () => {
Expand Down
4 changes: 2 additions & 2 deletions client/src/generic/Header/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ const styles = createStyles({
}
})

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

interface SearchBarProps extends WithStyles <typeof styles> {}
export default withStyles(styles)(SearchBar)
40 changes: 20 additions & 20 deletions client/src/generic/SelectCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@ import { createStyles, StyleRules, withStyles, WithStyles } from '@material-ui/c
import React, { SFC } from 'react'
import { Category, CategoryList } from '~interfaces'

const styles: StyleRules = createStyles({
container: {
height: '100%',
overflow: 'auto'
},
item: {
display: 'block',
textAlign: 'center'
},
list: {
height: 0
}
})

export interface SelectCategoryProps extends WithStyles<typeof styles> {
categories: CategoryList,
onClick: (id: Category['id']) => void,
value: Category['id']
}

const SelectCategory: SFC<SelectCategoryProps> = ({
categories,
classes: { container, item, list },
Expand All @@ -49,4 +29,24 @@ const SelectCategory: SFC<SelectCategoryProps> = ({
</div>
)

const styles: StyleRules = createStyles({
container: {
height: '100%',
overflow: 'auto'
},
item: {
display: 'block',
textAlign: 'center'
},
list: {
height: 0
}
})

export interface SelectCategoryProps extends WithStyles<typeof styles> {
categories: CategoryList,
onClick: (id: Category['id']) => void,
value: Category['id']
}

export default withStyles(styles)(SelectCategory)
36 changes: 18 additions & 18 deletions client/src/generic/SelectTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ import { Chip } from '@material-ui/core'
import { createStyles, StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles'
import React, { SFC } from 'react'

const SelectTags: SFC<SelectTagsProps> = ({
tags,
classes: { container, item, selected, clickable },
onClick
}) => (
<div className={container}>
{tags.map((tag, i) => (
<Chip
clickable
key={i}
label={tag.name}
className={`${item} ${clickable} ${tag.selected ? selected : ''}`}
onClick={onClick.bind(null, tag.name)}
morganfree marked this conversation as resolved.
Show resolved Hide resolved
/>
))}
</div>
)

const styles: StyleRulesCallback = ({
spacing,
palette: { primary: { light } }
Expand Down Expand Up @@ -38,22 +56,4 @@ export interface SelectTagsProps extends WithStyles<typeof styles> {
onClick: (name: Tag['name']) => void
}

const SelectTags: SFC<SelectTagsProps> = ({
tags,
classes: { container, item, selected, clickable },
onClick
}) => (
<div className={container}>
{tags.map((tag, i) => (
<Chip
clickable
key={i}
label={tag.name}
className={`${item} ${clickable} ${tag.selected ? selected : ''}`}
onClick={onClick.bind(null, tag.name)}
/>
))}
</div>
)

export default withStyles(styles)(SelectTags)
38 changes: 17 additions & 21 deletions client/src/generic/TaskList/TaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,11 @@ import React from 'react'
import { Task } from '~interfaces'
import TaskListElement from './TaskListElement'

const styles: StyleRulesCallback = () => createStyles({
container: {
height: '100%',
margin: 0,
overflow: 'auto'
},
grid: {
height: 0,
margin: 0,
width: '100%'
},
gridItem: {
// height: '250px'
}
})

interface TaskListProps extends WithStyles<typeof styles> {
tasks: Task[]
}

const TaskList = ({ classes, tasks }: TaskListProps) => (
<div className={classes.container}>
<Grid classes={{ container: classes.grid }} spacing={8} container>
{tasks.map((task, i) => (
<Grid
className={classes.gridItem}
item
xs={12}
sm={6}
Expand All @@ -42,4 +21,21 @@ const TaskList = ({ classes, tasks }: TaskListProps) => (
</div>
)

const styles: StyleRulesCallback = () => createStyles({
container: {
height: '100%',
margin: 0,
overflow: 'auto'
},
grid: {
height: 0,
margin: 0,
width: '100%'
}
})

interface TaskListProps extends WithStyles<typeof styles> {
tasks: Task[]
}

export default withStyles(styles)(TaskList)
7 changes: 7 additions & 0 deletions client/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ interface User {
email: string
}

enum Status {
Success = 'success',
Failure = 'failure',
Pending = 'pending'
}

export {
Category,
CategoryList,
Status,
Task,
TagList,
User
Expand Down
68 changes: 34 additions & 34 deletions client/src/pages/AddTask/components/AddTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,6 @@ import { SelectCategory, SelectCategoryProps, SelectTags, SelectTagsProps } from
import CapturePhoto, { CapturePhotoProps } from './CapturePhoto'
import Description, { DescriptionProps } from './Description'

const styles: StyleRulesCallback = () => createStyles({
addTaskContainer: {
display: 'grid',
gridTemplateRows: 'auto max-content'
},
container: {
display: 'grid',
gridTemplateRows: 'max-content auto'
},
content: {
display: 'grid',
gridTemplateRows: '50% 50%'
},
item: {
justifyContent: 'center',
maxWidth: '100%'
}
})

export interface AddTaskProps extends WithStyles<typeof styles> {
categories: SelectCategoryProps['categories'],
onClickCategory: SelectCategoryProps['onClick'],
onClickTag: SelectTagsProps['onClick'],
onSubmit: MouseEventHandler,
onCapturePhoto: CapturePhotoProps['onCapture'],
onChangeDescription: DescriptionProps['onChange'],
onChangeTab: (e: ChangeEvent<{}>, value: number) => void,
categoryValue: SelectCategoryProps['value'],
descriptionValue: DescriptionProps['value'],
image: CapturePhotoProps['imgSrc'],
tags: SelectTagsProps['tags'],
tabValue: boolean | number
}

const AddTask: SFC<AddTaskProps> = ({
classes: { addTaskContainer, container, content, item },
categories, onClickCategory, onChangeDescription, onChangeTab, onCapturePhoto,
Expand Down Expand Up @@ -74,4 +40,38 @@ const AddTask: SFC<AddTaskProps> = ({
</div>
)

const styles: StyleRulesCallback = () => createStyles({
addTaskContainer: {
display: 'grid',
gridTemplateRows: 'auto max-content'
},
container: {
display: 'grid',
gridTemplateRows: 'max-content auto'
},
content: {
display: 'grid',
gridTemplateRows: '50% 50%'
},
item: {
justifyContent: 'center',
maxWidth: '100%'
}
})

export interface AddTaskProps extends WithStyles<typeof styles> {
categories: SelectCategoryProps['categories'],
onClickCategory: SelectCategoryProps['onClick'],
onClickTag: SelectTagsProps['onClick'],
onSubmit: MouseEventHandler,
onCapturePhoto: CapturePhotoProps['onCapture'],
onChangeDescription: DescriptionProps['onChange'],
onChangeTab: (e: ChangeEvent<{}>, value: number) => void,
categoryValue: SelectCategoryProps['value'],
descriptionValue: DescriptionProps['value'],
image: CapturePhotoProps['imgSrc'],
tags: SelectTagsProps['tags'],
tabValue: boolean | number
}

export default withStyles(styles)(AddTask)
Loading