import React from 'react';
import Layout from '../src/components/layout/Layout';
import Home from '../src/components/pages/home/Home';
import { ApiMethods, ApiRoutes } from '../src/model/api';
import { HomeProps } from '../src/model/components/home';
import { Ring } from '../src/model/rings';
import authenticateRequest from '../src/utils/api/authenticateRequest';
import withUserSSR from '../src/utils/ssr/withSSRUser';

const index: React.FC<HomeProps> = ({ rings, user }) => (
  <Layout title="Inicio" mainClassName="homePage">
    <Home rings={rings} user={user} />
  </Layout>
);

export interface Test {
  test: string;
}
export const getServerSideProps = withUserSSR<HomeProps>(async (_, user) => {
  const rings = await authenticateRequest<Ring[]>(
    ApiMethods.Get,
    ApiRoutes.GetRings,
  );
  return {
    props: {
      rings: rings.data.sort((ant, act) => ant.order - act.order),
      user,
    },
  };
});

export default index;
