import { Dispatch } from 'redux' ; import analytics from '@react-native-firebase/analytics' ; import { Todo } from '../domain/models' ; import * as TodosRepository from '../domain/repositories/todos' ; import { AppState } from '../modules' ; import { add , remove , toggle , update } from '../modules/todos' ; export function addAndSync ( userId , newValues ) { return async function ( dispatch ) { const newTodo = Todo . factory ( newValues ) ; dispatch ( add ( newTodo ) ) ; TodosRepository . add ( userId , newTodo ) ; } ; } export function removeAndSync ( userId , id ) { return async function ( dispatch ) { dispatch ( remove ( id ) ) ; TodosRepository . remove ( userId , id ) ; } ; } export function toggleAndSync ( userId , id ) { return async function ( dispatch , getState ) { dispatch ( toggle ( id ) ) ; const { completedAt : newValue , title } = getState ( ) . todos [ id ] ; const eventName = newValue ? 'complete_todo' : 'uncomplete_todo' ; await analytics ( ) . logEvent ( eventName , { id : id , name : title , } ) ; TodosRepository . toggle ( userId , id , newValue ) ; } ; } export function editAndSync ( userId , id , newValues ) { return async function ( dispatch ) { dispatch ( update ( id , newValues ) ) ; TodosRepository . change ( userId , id , newValues ) ; } ; }