import * as React from "react";
import {useSearchParams} from "react-router-dom";
import {Loading} from "../../../../components/core/Loading";
import {api} from "../../../../lib/clients/api";
import {requests} from "../../../../requests";
import {useAuth} from "../../../../hooks/useAuth";

export function TelegramCode() {
    const [searchParams] = useSearchParams();
    const {checkSession} = useAuth();

    let code = searchParams.get("code");
    const request = {
        byLogin: false,
        code: code
    }
    api().post(requests.auth.login, request)
        .then(async (response) => {
            if (response.data.error) {
                return;
            }
            localStorage.setItem("token", response.data.token);
            await checkSession?.();
            window.location.reload();
        })

    return (
        <Loading loading={true}/>
    )
}