import accounting from 'accounting' ; import localForage from 'localforage' ; import React , { createContext , useContext , useState , useEffect } from 'react' ; import { useTranslation } from 'react-i18next' ; import { AppContext , AppState , AppActions , AppViews , AppHelpers , AppContextResponseArray , AppContextResponseObject , } from 'common/types' ; import { INIT_CONTEXT , INIT_STATE } from 'common/assets' ; import { I18nContext } from 'common/contexts' ; import { IS_DEMO_MODE } from 'config' ; import * as A from '../creators/actions' ; import { createClosedOrdersViews , createTranslationHelper } from '../creators' ; import { getDemoData } from 'common/utils/demo' ; import { CurrencyPosition } from 'common/enums' ; const store = localForage . createInstance ( { driver : localForage . INDEXEDDB , name : 'AsatelitPOS' , } ) ; const readContextFromLocalStorage = ( initState ) : Promise < AppState > => { return store . getItem ( 'state' ) . then ( ( state ) => ( state ? { ... initState , ... JSON . parse ( state as string ) } : initState ) ) . catch ( ( err ) => { throw new Error ( `Cannot read storage data. Error: ${ err }` ) ; } ) ; } ; export const appContext = createContext < AppContext > ( INIT_CONTEXT ) ; export const AppContextProvider = ( { children } ) => { const { dateDnsLocales , supportedLocales } = useContext ( I18nContext ) ; const { i18n , t } = useTranslation ( ) ; const [ state , setState ] = useState < AppState > ( INIT_STATE ) ; useEffect ( ( ) => { ( async function updateState ( ) { let demoData = { } ; if ( IS_DEMO_MODE ) demoData = getDemoData ( ) ; const state = await readContextFromLocalStorage ( INIT_STATE ) ; if ( state . settings . lang !== 'default' ) i18n . changeLanguage ( state . settings . lang ) ; accounting . settings = { currency : { symbol : state . settings . currency , format : state . settings . currencyPosition === CurrencyPosition . Left ? '%s %v' : '%v %s' , decimal : '.' , thousand : ',' , precision : 2 , } , number : { precision : 2 , thousand : ',' , decimal : '.' , } , } ; setState ( { ... state , ... demoData , isLoading : false } ) ; } ) ( ) ; } , [ i18n ] ) ; const updateContext = ( value ) => { try { const newState = { ... state , ... value } ; setState ( newState ) ; store . setItem ( 'state' , JSON . stringify ( newState ) ) ; } catch ( error ) { throw new Error ( 'Error writing data to offline storage.' ) ; } } ; const actions = { category : A . createCategoryActions ( state , updateContext ) , orders : A . createOrdersActions ( state , updateContext ) , item : A . createItemActions ( state , updateContext ) , settings : A . createSettingsActions ( state , updateContext ) , taxes : A . createTaxActions ( state , updateContext ) , } ; const views = { closedOrders : createClosedOrdersViews ( state ) , } ; const helpers = { translation : createTranslationHelper ( state , { i18n , t , dateDnsLocales , supportedLocales } ) , } ; const responseArray = [ state , actions , views , helpers ] ; const responseObject = { state , actions , views , helpers } ; return < appContext . Provider value = { Object . assign ( responseArray , responseObject ) } > { children } < /appContext.Provider>; } ;