﻿import 'babel-polyfill';

// React
import React from 'react';
import { render } from 'react-dom';

// Store
import { store } from './store';

// Actions
import { checkToken } from './actions/loginActions';

// NumeralJS
import numeral from 'numeral';
import language from 'numeral/languages/nl-nl';

numeral.language('nl-nl', language);
numeral.language('nl-nl');

// Moment
import moment from 'moment';
moment.locale('nl');

// Routes
import { routes } from './routes.jsx';

/**
 * Methode om te controleren of de huidige gebruiker wel is ingelogd
 */
const requireAuth = (nextState, replace) => {
  let state = store.getState();
  let auth = state.auth;

  if (!auth.get('isLoggedIn')) {
    replace({
      pathname: '/login',
      state: {
        nextPathname: nextState.location.pathname
      }
    });
  }
  else {
    store.dispatch(checkToken());
  }
};

// Rendering
render(routes(store, requireAuth), document.getElementById('app'));
