Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3230 | Rev 3263 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useRef, useState, useEffect } from 'react'
import { Link, useNavigate } from 'react-router-dom'
import { useForm } from 'react-hook-form'
import { useDispatch, useSelector } from 'react-redux'
import { Button, styled, Typography } from '@mui/material'
import { Mail, Lock } from '@mui/icons-material'
import Recaptcha from 'react-recaptcha'

import { axios } from '@utils'
import { useMobile } from '@hooks'
import { asyncLogin } from '@store/auth/auth.actions'
import { addNotification } from '@store/notification/notification.actions'
import CryptoJSAesJson from '@utils/crypto-js/cryptojs-aes-format'

import FacebookIcon from '@components/UI/icons/FacebookIcon'
import XIcon from '@components/UI/icons/XIcon'
import GoogleIcon from '@components/UI/icons/GoogleIcon'
import Form from '@components/common/form'
import Input from '@components/UI/inputs/Input'
import CheckboxInput from '@components/UI/inputs/Checkbox'
import LoadingWrapper from '@components/common/loading-wrapper'

const SocialButton = styled(Button)`
  background-color: white;
  border: 1px solid black;
  padding: 0.5rem 1rem;
  font-size: 1rem;
  color: #383838;
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 10px;
  svg {
    width: 16px;
    height: 16px;
  }
`

const Login = ({
  facebookOauth = '/signin/facebook',
  twitterOauth = '/signin/twitter',
  googleOauth = '/signin/google'
}) => {
  const [isVerified, setIsVerified] = useState(false)
  const reCaptchaInstance = useRef(null)
  const reCaptchaToken = useRef('')
  const navigate = useNavigate()
  const dispatch = useDispatch()

  const { site_key, aes, access_usign_social_networks } = useSelector(
    ({ auth }) => auth
  )

  const socialNetworksAccessAllowed = access_usign_social_networks === 'y'
  const isMobile = useMobile()

  const rrssOptions = [
    { label: 'Facebook', icon: FacebookIcon, href: facebookOauth },
    { label: 'Twitter', icon: XIcon, href: twitterOauth },
    { label: 'Google', icon: GoogleIcon, href: googleOauth }
  ]

  const {
    formState: { errors, isSubmitting },
    control,
    handleSubmit,
    reset
  } = useForm({ mode: 'all' })

  const onSubmitHandler = handleSubmit(
    async ({ email, password, remember }) => {
      try {
        const response = await dispatch(
          asyncLogin({
            email: CryptoJSAesJson.encrypt(email, aes),
            password: CryptoJSAesJson.encrypt(password, aes),
            remember: remember ? 1 : 0,
            captcha: reCaptchaToken.current
          })
        )
        reset()

        console.log(response)

        isMobile ? navigate('/apps-navigation') : navigate('/dashboard')
      } catch (error) {
        dispatch(addNotification({ style: 'danger', msg: error.message }))
      } finally {
        reCaptchaInstance.current.reset()
        loginExpiredCallbackHandler()
      }
    }
  )

  const loginVerifyCallbackHandler = (response) => {
    reCaptchaToken.current = response
    setIsVerified(true)
  }

  const loginExpiredCallbackHandler = () => {
    reCaptchaToken.current = ''
    setIsVerified(false)
  }

  const handleOnRecaptchaLoad = () => {
    reCaptchaToken.current = ''
  }

  const getRedirectUrl = async (endpoint) => {
    try {
      const res = await axios.get(endpoint)
      if (res.data && res.data.data) {
        window.location.href = res.data.data
      }
    } catch (error) {
      console.log('>>: error > ', error)
    }
  }

  useEffect(() => {
    reCaptchaInstance.current?.reset()
  }, [])

  return (
    <Form onSubmit={onSubmitHandler}>
      <LoadingWrapper loading={isSubmitting} displayChildren></LoadingWrapper>

      <Typography variant='h3'>Entrar</Typography>

      <Input
        type='email'
        name='email'
        icon={<Mail />}
        placeholder='Correo electrónico'
        rules={{
          required: 'Este campo es requerido',
          pattern: {
            value: /^[\w-.]+@([\w-]+\.)+[\w-]{2,}$/i,
            message: 'Debe ser una dirección de correo electrónico valida'
          },
          maxLength: {
            value: 64,
            message: 'Debe ser menor a 64 caracteres'
          }
        }}
        control={control}
        error={errors.email?.message}
      />

      <Input
        type='password'
        name='password'
        icon={<Lock />}
        placeholder='Clave'
        control={control}
        rules={{
          required: 'Este campo es requerido',
          pattern: {
            value:
              /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$^x%x*-]).{6,16}$/i,
            message:
              'Debe contener entre 6 y 16 caracteres, incluida una letra mayúscula, un número y un carácter especial #?!@$^%*-'
          }
        }}
        error={errors.password?.message}
      />

      <CheckboxInput control={control} name='remember' label='Recuerdame' />

      {site_key && (
        <Recaptcha
          sitekey={site_key}
          verifyCallback={loginVerifyCallbackHandler}
          verifyCallbackName='loginVerifyCallbackHandler'
          expiredCallback={loginExpiredCallbackHandler}
          expiredCallbackName='loginExpiredCallbackHandler'
          ref={reCaptchaInstance}
          render='explicit'
          onloadCallback={handleOnRecaptchaLoad}
          hl='es'
        />
      )}

      <div className='links'>
        <Link to='/forgot-password'>¿Has olvidado tu contraseña?</Link>
        <Link to='/signup'>¿No tienes cuenta?</Link>
      </div>

      <Button color='secondary' type='submit' disabled={!isVerified}>
        Entrar
      </Button>

      {socialNetworksAccessAllowed && (
        <>
          <h4>Entrar usando su red social</h4>
          <ul>
            {rrssOptions.map(({ label, icon: Icon, href }) => (
              <li key={label}>
                <SocialButton onClick={() => getRedirectUrl(href)}>
                  <Icon />
                  {`Iniciar sesión con ${label}`}
                </SocialButton>
              </li>
            ))}
          </ul>
        </>
      )}
    </Form>
  )
}

export default Login