import { delay , push , RecoilTaskInterface , removeObj , updateObj } from 'recoil-toolkit' ; import { itemStatus , todoList } from './atoms' ; import { ItemStatus } from './types' ; import { Item , delRemoteTodoItem , postRemoteTodoItem , putRemoteTodoItem , getRemoteTodoList , } from '../server' ; export const getTodoListTask = ( { set } ) => async ( ) => { const items = ( await getRemoteTodoList ( ) ) as Item [ ] ; set ( todoList , items ) ; } ; export const addItemTask = ( { set } ) => async ( text ) => { const item = ( await postRemoteTodoItem ( text ) ) as Item ; set ( todoList , push ( item ) ) ; } ; export const removeItemTask = ( { set } ) => async ( id ) => { try { set ( itemStatus ( id ) , ItemStatus . Deleting ) ; await delRemoteTodoItem ( id ) ; set ( itemStatus ( id ) , ItemStatus . Deleted ) ; await delay ( 1000 ) ; set ( todoList , removeObj < Item > ( { id } ) ) ; } catch ( e ) { set ( itemStatus ( id ) , ItemStatus . Idle ) ; throw e ; } } ; export const editItemTask = ( { set } ) => async ( item ) => { try { set ( itemStatus ( item . id ) , ItemStatus . Saving ) ; await putRemoteTodoItem ( item ) ; set ( itemStatus ( item . id ) , ItemStatus . Idle ) ; set ( todoList , updateObj < Item > ( item , { id : item . id } ) ) ; } catch ( e ) { set ( itemStatus ( item . id ) , ItemStatus . Editing ) ; throw e ; } } ; export const editAndRemoveTask = ( cb ) => async ( item ) => { await editItemTask ( cb ) ( item ) ; await removeItemTask ( cb ) ( item . id ) ; } ;