Proyectos de Subversion LeadersLinked - SPA

Rev

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

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

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

import Modal from '../UI/modal/Modal'
import Input from '../UI/inputs/Input'

const AddProfileModal = ({
  show = '',
  onHide = () => null,
  getProfiles = () => null
}) => {
  const labels = useSelector(({ intl }) => intl.labels)
  const dispatch = useDispatch()

  const {
    control,
    handleSubmit,
    formState: { errors }
  } = useForm()

  const onSubmitHandler = handleSubmit(async (data) => {
    const formData = new FormData()
    Object.entries(data).map(([key, value]) => formData.append(key, value))

    try {
      const response = await axios.post('/profile/my-profiles/add', formData)
      const { data, success } = response.data

      if (!success) throw new Error('Error al crear el perfil')

      addNotification({ style: 'success', msg: data })
      getProfiles()
      onHide()
    } catch (error) {
      dispatch(addNotification({ style: 'danger', msg: error.message }))
    }
  })

  return (
    <Modal
      title={labels.new_profile}
      show={show}
      onHide={onHide}
      onAccept={onSubmitHandler}
      onClose={onHide}
    >
      <Input
        name='name'
        placeholder={labels.create_profile}
        control={control}
        rules={{ required: 'Este campo es requerido' }}
        error={errors.name?.message}
      />
    </Modal>
  )
}

export default AddProfileModal