import { useState } from 'react';
import BooksList from './BooksList';
import keycloak from './keycloak';

const App: React.FC = () => {
  const [token, setToken] = useState(keycloak.token);

  const refreshToken = async () => {
    if (keycloak.isTokenExpired(30)) {
      await keycloak.updateToken(60);
      setToken(keycloak.token);
    }
  };

  console.log(keycloak.token);

  return (
    <div style={{ padding: 20 }}>
      <h1>Willkommen zur Bücherliste</h1>

      <p>Angemeldet als: {keycloak.tokenParsed?.preferred_username}</p>

      <button onClick={() => keycloak.logout()}>Logout</button>

      <hr />

      <BooksList token={token} refreshToken={refreshToken} />
    </div>
  );
};

export default App;

