Skip to content
Open
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
56 changes: 56 additions & 0 deletions components/Todos/AddTodo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { Component } from 'react'
import { func, string, bool } from 'prop-types'
import { connect } from 'react-redux'
import { Actions, Selectors } from 'redux-enterprise'
import { Button, Container, Form, Input } from './styled'

@connect((state) => ({
text: state.todoEditor.text,
isEditing: state.todoEditor.isEditing,
editingId: state.todoEditor.editingId
}))
export default class AddTodo extends Component {
static propTypes = {
dispatch: func.isRequired,
isEditing: bool.isRequired,
text: string,
editingId: string
}

onSubmit = (e) => {
e.preventDefault()
const { dispatch, text } = this.props

if (!text.trim()) {
return
}

dispatch(Actions.todos.create({ id: '4', text }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either add state Field for tracking last used number and increment it, or just gen a uuid

dispatch(Actions.todoEditor.text.unset())
}

onInput = (e) => {
const { dispatch } = this.props;
dispatch(Actions.todoEditor.text.set(e.target.value))
}

onFocus = () => {
const { dispatch } = this.props
dispatch(Actions.todoEditor.isEditing.toggle())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this component is showing at all you are probably already isEditing

}

render() {
const { text = '', isEditing } = this.props

return (
<Container
isEditing={isEditing}>
<Form onSubmit={this.onSubmit}>
<Input onFocus={this.onFocus} onInput={this.onInput} />
<Button>Add</Button>
</Form>
</Container>
)
}
}

31 changes: 31 additions & 0 deletions components/Todos/AddTodo/styled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import styled from 'styled-components'

export const Container = styled.div`
border-bottom: 1px solid black;
padding: 5px 10px;
${({ isSelected }) => isSelected && 'background: green;'}
`

export const Form = styled.form`
`

export const Input = styled.input.attrs({
type: 'text',
value: props => props.text
})`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
padding: ${props => props.padding}
`

export const Button = styled.button.attrs({
type: 'submit',
value: 'Submit'
})`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
`;
6 changes: 4 additions & 2 deletions components/Todos/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { Component } from 'react'
import { connect } from 'react-redux'
import { arrayOf, string } from 'prop-types'
import { Actions, Selectors } from 'redux-enterprise'
import { Container } from './styled'
import { Button, Container } from './styled'
import Todo from './Todo'
import AddTodo from './AddTodo'

@connect((state) => ({
todoIds: Selectors.todos.ids(state)
Expand All @@ -17,8 +18,9 @@ export default class Todos extends Component {
return (
<Container>
{this.props.todoIds.map((id) =>
<Todo id={id} />
<Todo id={id} key={id} />
)}
<AddTodo />
</Container>
)
}
Expand Down
1 change: 1 addition & 0 deletions state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { reducers } = defineState({
selected: Index,
todoEditor: {
isEditing: Flag,
text: Field,
editingId: Field,
},
})
Expand Down