Skip to content

Commit dcafe44

Browse files
refactor: move dispatch calls into useTodoAPI calls
1 parent ef0991a commit dcafe44

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

web/src/features/todos/todo-list/TodoItem.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ import { Card, Typography } from '@equinor/eds-core-react'
22
import { done, remove_outlined, undo } from '@equinor/eds-icons'
33
import type { AddTodoResponse } from '../../../api/generated'
44
import IconButton from '../../../common/components/IconButton'
5-
import { useTodos } from '../../../contexts/TodoContext'
65
import { useTodoAPI } from '../../../hooks/useTodoAPI'
76
import { StyledTodoItemTitle } from './TodoItem.styled'
87

98
const TodoItem = ({ todo }: { todo: AddTodoResponse }) => {
109
const { toggleTodoItem, removeTodoItem } = useTodoAPI()
11-
const { dispatch } = useTodos()
1210

1311
async function toggle() {
1412
await toggleTodoItem(todo)
15-
dispatch({ type: 'TOGGLE_TODO', payload: todo })
1613
}
1714

1815
async function remove() {
1916
await removeTodoItem(todo)
20-
dispatch({ type: 'REMOVE_TODO', payload: todo })
2117
}
2218

2319
return (

web/src/features/todos/todo-list/TodoList.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import { StyledInput, StyledTodoList } from './TodoList.styled'
88

99
const AddItem = () => {
1010
const { addTodoItem } = useTodoAPI()
11-
const { dispatch } = useTodos()
1211
const [value, setValue] = useState('')
1312

1413
const add: FormEventHandler = (event) => {
1514
event.preventDefault()
1615
if (value) {
17-
addTodoItem(value).then((todo) => dispatch({ type: 'ADD_TODO', payload: todo }))
16+
addTodoItem(value)
1817
}
1918
setValue('')
2019
}
@@ -43,7 +42,7 @@ const TodoList = () => {
4342
const { state, dispatch } = useTodos()
4443

4544
useEffect(() => {
46-
getAllTodoItems().then((todos) => dispatch({ type: 'INITIALIZE', payload: todos }))
45+
getAllTodoItems()
4746
}, [dispatch, getAllTodoItems])
4847

4948
return (

web/src/hooks/useTodoAPI.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback } from 'react'
22
import { type AddTodoResponse, ApiError, type ErrorResponse, TodoService } from '../api/generated'
3+
import { useTodos } from '../contexts/TodoContext'
34

45
function handleError(error: unknown): void {
56
if (error instanceof ApiError) {
@@ -9,36 +10,44 @@ function handleError(error: unknown): void {
910
}
1011

1112
export function useTodoAPI() {
13+
const { dispatch } = useTodos()
14+
1215
const addTodoItem = useCallback(async (title: string) => {
1316
try {
14-
return await TodoService.create({ title })
17+
const todoItem = await TodoService.create({ title })
18+
dispatch({ type: 'ADD_TODO', payload: todoItem })
19+
return todoItem
1520
} catch (error) {
1621
handleError(error)
1722
}
1823
}, [])
1924

2025
const getAllTodoItems = useCallback(async () => {
2126
try {
22-
return await TodoService.getAll()
27+
const todoItems = await TodoService.getAll()
28+
dispatch({ type: 'INITIALIZE', payload: todoItems })
29+
return todoItems
2330
} catch (error) {
2431
handleError(error)
2532
}
2633
}, [])
2734

28-
const toggleTodoItem = useCallback(async (todo: AddTodoResponse) => {
35+
const toggleTodoItem = useCallback(async (todoItem: AddTodoResponse) => {
2936
try {
30-
return await TodoService.updateById(todo.id, {
31-
is_completed: !todo.is_completed,
32-
title: todo.title,
37+
await TodoService.updateById(todoItem.id, {
38+
is_completed: !todoItem.is_completed,
39+
title: todoItem.title,
3340
})
41+
dispatch({ type: 'TOGGLE_TODO', payload: todoItem })
3442
} catch (error) {
3543
handleError(error)
3644
}
3745
}, [])
3846

39-
const removeTodoItem = useCallback(async (todo: AddTodoResponse) => {
47+
const removeTodoItem = useCallback(async (todoItem: AddTodoResponse) => {
4048
try {
41-
return await TodoService.deleteById(todo.id)
49+
await TodoService.deleteById(todoItem.id)
50+
dispatch({ type: 'REMOVE_TODO', payload: todoItem })
4251
} catch (error) {
4352
handleError(error)
4453
}

0 commit comments

Comments
 (0)