import React, { useEffect, useState } from 'react'; import type { ReactElement } from 'react'; import Head from 'next/head'; import BaseButton from '../components/BaseButton'; import CardBox from '../components/CardBox'; import BaseIcon from '../components/BaseIcon'; import { mdiInformation } from '@mdi/js'; import SectionFullScreen from '../components/SectionFullScreen'; import LayoutGuest from '../layouts/Guest'; import { Field, Form, Formik } from 'formik'; import FormField from '../components/FormField'; import FormCheckRadio from '../components/FormCheckRadio'; import BaseDivider from '../components/BaseDivider'; import BaseButtons from '../components/BaseButtons'; import { useRouter } from 'next/router'; import { getPageTitle } from '../config'; import { findMe, loginUser, resetAction } from '../stores/authSlice'; import { useAppDispatch, useAppSelector } from '../stores/hooks'; import Link from 'next/link'; import { toast, ToastContainer } from 'react-toastify'; import { getPexelsImage, getPexelsVideo } from '../helpers/pexels'; export default function Login() { const router = useRouter(); const dispatch = useAppDispatch(); const textColor = useAppSelector((state) => state.style.linkColor); const iconsColor = useAppSelector((state) => state.style.iconsColor); const notify = (type, msg) => toast(msg, { type }); const [illustrationImage, setIllustrationImage] = useState({ src: undefined, photographer: undefined, photographer_url: undefined, }); const [illustrationVideo, setIllustrationVideo] = useState({ video_files: [], }); const [contentType, setContentType] = useState('video'); const [contentPosition, setContentPosition] = useState('right'); const { currentUser, isFetching, errorMessage, token, notify: notifyState, } = useAppSelector((state) => state.auth); const [initialValues, setInitialValues] = React.useState({ email: 'admin@flatlogic.com', password: 'password', remember: true, }); const title = 'test3'; // Fetch Pexels image/video useEffect(() => { async function fetchData() { const image = await getPexelsImage(); const video = await getPexelsVideo(); setIllustrationImage(image); setIllustrationVideo(video); } fetchData(); }, []); // Fetch user data useEffect(() => { if (token) { dispatch(findMe()); } }, [token, dispatch]); // Redirect to dashboard if user is logged in useEffect(() => { if (currentUser?.id) { router.push('/dashboard'); } }, [currentUser?.id, router]); // Show error message if there is one useEffect(() => { if (errorMessage) { notify('error', errorMessage); } }, [errorMessage]); // Show notification if there is one useEffect(() => { if (notifyState?.showNotification) { notify('success', notifyState?.textNotification); dispatch(resetAction()); } }, [notifyState?.showNotification]); const handleSubmit = async (value) => { const { remember, ...rest } = value; await dispatch(loginUser(rest)); }; const setLogin = (target) => { const email = target?.innerText; setInitialValues((prev) => { return { ...prev, email, password: 'password' }; }); }; const imageBlock = (image) => (
Photo by {image?.photographer} on Pexels
); const videoBlock = (video) => { if (video?.video_files?.length > 0) { return (
Video by {video.user.name} on Pexels
); } }; return (
{getPageTitle('Login')}
{contentType === 'image' && contentPosition !== 'background' ? imageBlock(illustrationImage) : null} {contentType === 'video' && contentPosition !== 'background' ? videoBlock(illustrationVideo) : null}

Welcome!

handleSubmit(values)} >

Don’t have account yet?{' '} New Account

© 2024 {title}. All rights reserved

Privacy Policy
); } Login.getLayout = function getLayout(page: ReactElement) { return {page}; };