/*
 Rookeries - Router and initialization for client-side

 @copyright (c) Copyright 2013-2016 Dorian Pula
 @license AGPL v3
 @author Dorian Pula [dorian.pula@amber-penguin-software.ca]
 */

import React from "react";
import ReactDom from "react-dom";

import {Router, browserHistory} from "react-router";
import {syncHistoryWithStore} from "react-router-redux";
import {Provider} from "react-redux";

import {Routes} from "./routes";
import {appStore} from "../stores";
import {initializeApp, fetchPage} from "../actions";

export default function () {
    initializeApp(appStore);
    const history = syncHistoryWithStore(browserHistory, appStore);

    history.listen((location) => {
        appStore.dispatch(fetchPage(location.pathname.substring(1)))
    });

    const RookeriesWiredApp = (
        <Provider store={ appStore }>
            <Router history={ history } routes={ Routes }/>
        </Provider>
    );

    ReactDom.render(RookeriesWiredApp, document.getElementById("ui-target"));
}
