import React , { createContext , useReducer , useState } from "react" ; import { mockData } from "../helpers/transactions" ; import { ITransaction } from "../models/ITransaction" ; import AppReducer from "./AppReducer" ; const ENABLE_MOCK_DATA = true ; const mocksData = mockData ; const initialState = { transactions : new Map < number , ITransaction > ( ENABLE_MOCK_DATA ? mocksData : null ) , defaultTransaction : new Map < number , ITransaction > ( ) , deleteTransaction : ( State ) => { } , addTransaction : ( State ) => { } , editTransaction : ( State ) => { } , discardTransaction : ( ) => { } , defaultPresent : false , setDefaultPresent : ( State ) => { } , } ; export const GlobalContext = createContext ( initialState ) ; export const GlobalProvider = ( { children } ) => { const [ state , dispatch ] = useReducer ( AppReducer , initialState ) ; const [ defaultPresent , setDefaultPresent ] = useState ( false ) ; const deleteTransaction = ( id ) => { dispatch ( { type : "DELETE_TRANSACTION" , payload : id , } ) ; } ; const addTransaction = ( transaction ) => { dispatch ( { type : "ADD_TRANSACTION" , payload : transaction , } ) ; } ; const editTransaction = ( id ) => { dispatch ( { type : "EDIT_TRANSACTION" , payload : id , } ) ; } ; const discardTransaction = ( ) => { dispatch ( { type : "DISCARD_TRANSACTION" , } ) ; } ; return ( < GlobalContext . Provider value = { { transactions : state . transactions , defaultTransaction : state . defaultTransaction , deleteTransaction , addTransaction , editTransaction , defaultPresent , setDefaultPresent , discardTransaction , } } > { children } < /GlobalContext.Provider>); } ;