import * as TodosApi from '../../api' ; import { actions as todosActions } from '../slices/todos' ; import { AppDispatch , AppThunk } from '..' ; export const fetchTodos = ( ) : AppThunk => async ( dispatch ) => { try { const todos = await TodosApi . fetchTodos ( ) ; dispatch ( todosActions . setTodos ( todos ) ) ; } catch ( error ) { throw error ; } } ; export const addTodo = ( todo ) : AppThunk => async ( dispatch ) => { try { const id = await TodosApi . addTodo ( todo ) ; const finalTodo = { id , completed : false , ... todo , } ; dispatch ( todosActions . addTodo ( finalTodo ) ) ; } catch ( error ) { throw error ; } } ; export const removeTodo = ( id ) : AppThunk => async ( dispatch ) => { try { await TodosApi . removeTodo ( id ) ; dispatch ( todosActions . removeTodo ( id ) ) ; } catch ( error ) { throw error ; } } ; export const updateTodo = ( id , changes ) => async ( dispatch ) => { try { await TodosApi . updateTodo ( id , changes ) ; dispatch ( todosActions . updateTodo ( { id , changes } ) ) ; } catch ( error ) { throw error ; } } ; export const toggleTodo = ( id ) => async ( dispatch ) => { try { await TodosApi . toggleTodo ( id ) ; dispatch ( todosActions . toggleTodo ( id ) ) ; } catch ( error ) { throw error ; } } ;