import React, { Dispatch, useEffect } from "react"; import { Page } from "@/pages/base"; import { Button, CircularProgress, Container, Typography } from "@material-ui/core"; import { Action, StudentActions, useDispatch } from "@/state/actions"; import { Route, Switch, useHistory, useLocation, useRouteMatch } from "react-router-dom"; import { route } from "@/routing"; import { useVerticalSpacing } from "@/styles"; import { AppState } from "@/state/reducer"; import api from "@/api"; import { UserActions } from "@/state/actions/user"; import { getAuthorizeUrl } from "@/api/user"; import { useTranslation } from "react-i18next"; import { Loading } from "@/components/loading"; const authorizeUser = (code?: string) => async (dispatch: Dispatch, getState: () => AppState): Promise => { const token = await api.user.login(code); dispatch({ type: UserActions.Login, token, }) const student = await api.student.current(); dispatch({ type: StudentActions.Set, student: student, }) } export const UserLoginPage = () => { const dispatch = useDispatch(); const history = useHistory(); const match = useRouteMatch(); const location = useLocation(); const query = new URLSearchParams(useLocation().search); const { t } = useTranslation(); const handleSampleLogin = async () => { await dispatch(authorizeUser()); history.push(route("home")); } const handlePgLogin = async () => { history.push(route("user_login") + "/pg"); } const classes = useVerticalSpacing(3); useEffect(() => { (async function() { if (location.pathname === `${match.path}/check/pg`) { await dispatch(authorizeUser(query.get("code") as string)); history.push("/"); } })(); }, [ match.path ]); const inProgress = return Zaloguj się { window.location.href = getAuthorizeUrl() return inProgress } } /> { inProgress } ; }