import React, { useEffect, useState } from "react";
import {
  BrowserRouter as Router,
  Route,
  Redirect,
  Switch,
} from "react-router-dom";
import { connect } from "react-redux";

import { Provider } from "react-redux";
import store from "./store";
import history from "./utils/history";

import "./styles/Reset.css";
import "./styles/App.css";

import RegisterForm from "./components/RegisterForm";
import LoginForm from "./components/LoginForm";
import Lib from "./components/Library";

const PrivateRoute = (props) => {
  console.log(props);
  const { children, ...rest } = props;

  return (
    <Route
      {...rest}
      render={({ location }) => {
        return localStorage.authToken ? (
          children
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: location },
            }}
          />
        );
      }}
    />
  );
};

export default () => {
  return (
    <Provider store={store}>
      <div className="App">
        <Router history={history}>
          <Switch>
            <Route exact path="/" component={RegisterForm} />
            <Route path="/login" component={LoginForm} />
            <PrivateRoute path="/private">
              <Lib />
            </PrivateRoute>
          </Switch>
        </Router>
      </div>
    </Provider>
  );
};
