import React from "react";
import { Redirect, Route } from "react-router-dom";

// Route used when user starts the game or views summary of last gamerun
export default function PlayRoute({ component: Component, ...rest }) {
  // check if player has username
  const hasUsername = !!localStorage.getItem("username");

  return (
    <Route
      {...rest}
      render={(props) =>
        hasUsername ? ( // if user has username, render component
          <Component {...props} />
        ) : (
          // else redirect to page, where user has to enter username
          <Redirect
            to={{ pathname: "/username", state: { from: props.location } }}
          />
        )
      }
    />
  );
}
