Proyectos de Subversion LeadersLinked - SPA

Rev

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

import React from 'react'
import { useDispatch } from 'react-redux'
import { useForm } from 'react-hook-form'

import { axios } from '../../utils'
import { addNotification } from '../../redux/notification/notification.actions'

import Modal from 'components/UI/modal/Modal'
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'

const CreateGroupModal = ({ isOpen, onClose }) => {
  const { register, handleSubmit, errors } = useForm()
  const dispatch = useDispatch()

  const onSubmitHandler = handleSubmit(async (data) => {
    const formData = new FormData()
    Object.entries(data).map(([key, value]) => formData.append(key, value))
    axios
      .post('/chat/create-group', formData)
      .then(({ data: response }) => {
        const { success } = response

        if (!success) {
          dispatch(
            addNotification({
              style: 'danger',
              message: 'Ha ocurrido un error, por favor intente más tarde.'
            })
          )
          return
        }

        onClose()
      })
      .catch((err) => {
        dispatch(addNotification({ style: 'danger', message: err.message }))
      })
  })

  return (
    <Modal
      title='Crear grupo'
      show={isOpen}
      onClose={onClose}
      onReject={onClose}
      onAccept={onSubmitHandler}
    >
      <label className='mb-1' htmlFor='name'>
        Nombre del grupo
      </label>
      <input
        type='text'
        name='name'
        id='name'
        ref={register({ required: 'Este campo es requerido' })}
      />
      {errors.name && (
        <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
      )}
    </Modal>
  )
}

export default CreateGroupModal