Skip to content

Commit

Permalink
feat: handle un-doing a todo
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed May 18, 2020
1 parent 5bd59eb commit ab52f67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
17 changes: 12 additions & 5 deletions client/src/components/Todos.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ const translateAction = action =>
prepare_notes: 'Prepare notes from',
}[action])

export const SingleTodo = ({ todo, withRemoveButtons }) => {
export const SingleTodo = ({ todo, withRemoveButtons, shouldUpdateAll }) => {
const dispatch = useDispatch()
const dueMoment = moment(todo.due_date)
const isDisabled = todo.isBeingUpdated

const handleComplete = () =>
const toggleComplete = isCompleted => () =>
dispatch(
todosActions.updateTodo({
id: todo.id,
updateData: { is_completed: true },
updateData: {
is_completed: isCompleted,
...(!isCompleted && { completed_on: null }),
},
shouldUpdateAll,
})
)

Expand Down Expand Up @@ -61,8 +65,11 @@ export const SingleTodo = ({ todo, withRemoveButtons }) => {
? `Completed on ${moment(todo.completed_on).format(DATE_FORMAT)}`
: dueMoment.fromNow()}
</div>
<Button disabled={isDisabled} onClick={handleComplete}>
done
<Button
disabled={isDisabled}
onClick={toggleComplete(!todo.is_completed)}
>
{todo.is_completed ? 'undo' : 'done'}
</Button>
{withRemoveButtons && (
<Button disabled={isDisabled} className='ml2' onClick={handleDelete}>
Expand Down
8 changes: 6 additions & 2 deletions client/src/utils/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function* deleteTodo(id: $PropertyType<Todo, 'id'>) {
yield put(todosActions.setTodos({ todos: todos.filter(v => v.id !== id) }))
}

function* updateTodo({ id, updateData }) {
function* updateTodo({ id, updateData, shouldUpdateAll }) {
const todo = yield call(request, {
url: getTodosURL(id),
method: 'PATCH',
Expand All @@ -90,7 +90,11 @@ function* updateTodo({ id, updateData }) {
: update(updatedItemIndex, todo, todos),
})
)
yield put(todosActions.fetchTodos())
// An update to a Todo might create Todos, so let's fetch all
const updateAction = shouldUpdateAll
? todosActions.fetchAllTodos()
: todosActions.fetchTodos()
yield put(updateAction)
}

function* getUserData(_) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/AllTodos.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AllTodos = ({ bookId }) => {
const renderSingle = todo => (
<tr key={todo.id}>
<td>
<SingleTodo todo={todo} withRemoveButtons />
<SingleTodo todo={todo} withRemoveButtons shouldUpdateAll />
</td>
</tr>
)
Expand Down

0 comments on commit ab52f67

Please sign in to comment.