-
-
Save haykadamyan/e5ff1fe0e638d6ff87d61f8bee9d1383 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Countdown from '../../countdown'; | |
import * as FontAwesome from 'react-icons/lib/fa'; | |
import moment from 'moment'; | |
import classNames from 'classnames'; | |
import styles from './component.pcss'; | |
class ListItem extends React.Component { | |
state = { | |
editing: false, | |
title: this.props.item.title, | |
}; | |
onStart = () => { | |
this.props.onStart(this.props.item.slug); | |
}; | |
onDone = () => { | |
this.props.onDone(this.props.item.slug); | |
}; | |
onDelete = () => { | |
this.props.onDelete(this.props.item.slug); | |
}; | |
onEdit = () => { | |
const { editing, title } = this.state; | |
if (editing) { | |
this.props.onUpdate(title, this.props.item.slug).then(() => { | |
this.setState({ | |
editing: !editing, | |
}); | |
}); | |
} else { | |
this.setState({ | |
editing: !editing, | |
}); | |
} | |
}; | |
onChange = (e) => { | |
this.setState({ | |
[e.target.name]: e.target.value, | |
}); | |
}; | |
componentWillReceiveProps(nextProps) { | |
const { item } = nextProps; | |
this.setState({ | |
title: item.title | |
}) | |
} | |
render() { | |
const { item } = this.props; | |
let { status } = item; | |
const now = moment(); | |
const minutes = parseInt(moment.duration(item.startedAt.diff(now)).asMinutes()); | |
if (status === 'In Progress' && minutes <= 0) { | |
status = 'Fail'; | |
} | |
return ( | |
<li> | |
<a href='#'> | |
{ ( status !== "Done" && status !== "Fail" ) && <FontAwesome.FaCheck onClick={this.onDone} /> } | |
<FontAwesome.FaPencil onClick={this.onEdit} /> | |
<FontAwesome.FaClose onClick={this.onDelete} /> | |
</a> | |
{ | |
status === "Done" ? | |
<div> | |
<del> | |
{this.state.title} | |
</del> | |
</div> | |
: | |
<div className={classNames(styles.task, status === "Fail" && styles.failedTask)}> | |
{ | |
this.state.editing ? ( | |
<input | |
type="text" | |
name="title" | |
placeholder="Enter Task title" | |
className={styles.editTask} | |
value={this.state.title} | |
onChange={this.onChange} | |
/> | |
) : this.state.title | |
} | |
<div className={styles.taskRightSection}> | |
{ ( !this.state.editing && status === "In Progress" ) && <Countdown startedAt={item.startedAt}/> } | |
{ ( !this.state.editing && status === "Initial" ) && <span className={styles.taskAction} onClick={this.onStart}>Start</span> } | |
{ this.state.editing && <span className={styles.taskAction} onClick={this.onEdit}>Save</span> } | |
</div> | |
</div> | |
} | |
</li> | |
); | |
} | |
} | |
export default ListItem; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment