/* eslint-disable react/jsx-curly-newline */ /* eslint-disable sonarjs/no-small-switch */ import TelegramIcon from '../public/assets/images/telegram.svg'; import styles from './Form.module.scss'; import { publicRuntimeConfig } from '@/config/runtime'; import { FormStateContext } from '@/context/form-state'; import { useSocket } from '@/hooks/socket'; import type { TelegramUrlResponse } from '@/types/error'; import type { LdapUser } from '@/types/user'; import axios, { isAxiosError } from 'axios'; import Image from 'next/image'; import type { PropsWithChildren } from 'react'; import { useContext, useEffect } from 'react'; import { useForm } from 'react-hook-form'; const ERROR_INVALID_CREDENTIALS = 'Неверный логин или пароль'; const ERROR_SERVER = 'Не удалось войти. Повторите попытку позже'; const { APP_BASE_PATH, TELEGRAM_BOT_URL } = publicRuntimeConfig; type FormData = { readonly login: string; readonly password: string; }; type FormProps = { readonly onSubmit: (data: FormData) => void; }; function redirect() { const redirectUrl = (window.location.pathname.replace(APP_BASE_PATH, '') || '/') + (window.location.search || ''); window.location.replace(redirectUrl); } function BaseForm({ children, onSubmit }: FormProps & PropsWithChildren) { const { handleSubmit, register } = useForm(); const { state: { error, step }, } = useContext(FormStateContext); return (
{step === 'telegram-login' ? ( Открыть чат с ботом ) : null} {error ? {error} : null} {children}
); } export const Form = { Default() { const { dispatch, state: { step, user }, } = useContext(FormStateContext); function handleRefreshToken() { axios .get('/refresh-token') .then(() => redirect()) .catch(() => dispatch({ payload: { error: ERROR_SERVER }, type: 'set-error', }) ); } if (step === 'login' && user) { return ( ); } function handleLogin(data: FormData) { return axios .post('/login', data) .then(() => redirect()) .catch(() => dispatch({ payload: { error: ERROR_INVALID_CREDENTIALS }, type: 'set-error', }) ); } return ( handleLogin(data)}> ); }, Telegram() { const { dispatch, state: { step, user }, } = useContext(FormStateContext); const { socket } = useSocket(); useEffect(() => { if (step === 'telegram-login') { socket.open(); socket.on('connect', () => {}); socket.on('auth-allow', () => { socket.off('connect'); axios .get('/login-confirm') .then(() => redirect()) .catch(() => dispatch({ payload: { error: ERROR_SERVER }, type: 'set-error', }) ); }); } return () => { socket.off('connect'); }; }, [dispatch, socket, step]); function handleLogin(data: FormData) { axios .post('/login', data) .then((res) => { dispatch({ payload: { step: 'telegram', user: res.data, }, type: 'set-step', }); }) .catch(() => dispatch({ payload: { error: ERROR_INVALID_CREDENTIALS }, type: 'set-error', }) ); } function handleTelegramLogin() { axios .post('/login-telegram') .then(() => { dispatch({ payload: { step: 'telegram-login', }, type: 'set-step', }); }) .catch((error_) => { let error = ERROR_SERVER; if (isAxiosError(error_) && error_.response?.data?.message) { error = error_.response?.data?.message; } return dispatch({ payload: { error }, type: 'set-error', }); }); } // eslint-disable-next-line sonarjs/no-identical-functions function handleRefreshToken() { axios .get('/refresh-token') .then(() => redirect()) .catch(() => dispatch({ payload: { error: ERROR_SERVER }, type: 'set-error', }) ); } if (step === 'login' && user) { return ( ); } if (step === 'telegram') { return ( handleTelegramLogin()}> ); } if (step === 'telegram-login') { return ( {}}> ); } return ( handleLogin(data)}> ); }, };