import * as React from 'react' ; import * as ReactDOM from 'react-dom' ; import * as ReactRedux from 'react-redux' ; import * as Redux from 'redux' ; import { changeInstallStateAction , changeTabStateAction , setMessageAction , setValueAction , updateAdminOptionsAction , updateSyncStatusAction , } from './actions' ; import MainContainer from './containers/MainContainer' ; import { DisqusApi } from './DisqusApi' ; import adminApp from './reducers/adminApp' ; import { IAdminOptions } from './reducers/AdminOptions' ; import AdminState , { InstallationState } from './reducers/AdminState' ; import { ISyncStatus } from './reducers/SyncStatus' ; import { IRestResponse , WordPressRestApi } from './WordPressRestApi' ; const store = Redux . createStore < AdminState > ( adminApp ) ; const element = document . getElementById ( 'disqus-admin' ) ; const onClearMessage = ( event ) : void => { store . dispatch ( setMessageAction ( null ) ) ; } ; const onHashChange = ( event ) : void => { if ( event ) event . preventDefault ( ) ; store . dispatch ( changeTabStateAction ( window . location . hash . replace ( '#' , '' ) ) ) ; } ; window . addEventListener ( 'hashchange' , onHashChange ) ; if ( window . location . hash ) onHashChange ( null ) ; ReactDOM . render ( < ReactRedux . Provider store = { store } > < MainContainer /> < /ReactRedux.Provider>, element, () => { const checkResponse = ( response ) : boolean => { if ( ! response ) return false ; if ( response . code !== 'OK' ) { store . dispatch ( setMessageAction ( { onDismiss : onClearMessage , text : response . message , type : 'error' , } ) ) ; return false ; } return true ; } ; store . dispatch ( setValueAction ( 'isFetchingAdminOptions' , true ) ) ; store . dispatch ( setValueAction ( 'isFetchingSyncStatus' , true ) ) ; WordPressRestApi . instance . pluginRestGet ( 'settings' , ( response ) => { store . dispatch ( setValueAction ( 'isFetchingAdminOptions' , false ) ) ; if ( ! checkResponse ( response ) ) return ; const data = response . data ; DisqusApi . instance . configure ( data . disqus_public_key , data . disqus_admin_access_token , data . disqus_forum_url ) ; store . dispatch ( updateAdminOptionsAction ( response . data ) ) ; if ( response . data . disqus_installed ) store . dispatch ( changeInstallStateAction ( InstallationState . installed ) ) ; } ) ; WordPressRestApi . instance . pluginRestGet ( 'sync/status' , ( response ) => { store . dispatch ( setValueAction ( 'isFetchingSyncStatus' , false ) ) ; if ( ! checkResponse ( response ) ) return ; store . dispatch ( updateSyncStatusAction ( response . data ) ) ; } ) ; } ) ;