mentorenwahl/docker/frontend/pages/index.tsx

59 lines
992 B
TypeScript
Raw Normal View History

2022-02-02 14:38:36 +00:00
import Cookies from "js-cookie";
import Router from "next/router";
import { useEffect } from "react";
2022-02-02 20:54:14 +00:00
import { gql, useLazyQuery } from "@apollo/client";
2022-02-02 14:38:36 +00:00
import * as cookieNames from "../lib/cookieNames";
2022-02-02 20:54:14 +00:00
import Dashboard from "../components/dashboard";
import Loading from "../components/loading";
const ME_QUERY = gql`
query Me {
me {
id
firstname
email
role
teacher {
id
}
student {
id
}
}
}
`;
const token = Cookies.get(cookieNames.TOKEN);
2022-02-02 14:38:36 +00:00
function Home(): JSX.Element {
useEffect(() => {
if (!token) {
Router.push("/login");
}
2022-02-02 20:54:14 +00:00
});
const [loadMe, { called, loading, data }] = useLazyQuery(ME_QUERY, {
variables: { token },
});
2022-02-02 14:38:36 +00:00
if (!token) {
return <></>;
}
2022-02-02 20:54:14 +00:00
if (called && loading) {
return <Loading />;
} else if (!called) {
loadMe();
return <Loading />;
}
return (
<div>
<Dashboard user={data.me} />
</div>
);
2022-02-02 14:38:36 +00:00
}
2022-01-29 15:40:39 +00:00
export default Home;