Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1979 | Rev 2093 | 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 { connect, 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,
  addNotification // redux action
}) => {
  const labels = useSelector(({ intl }) => intl.labels)

  const { register, handleSubmit, errors } = useForm()

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

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

        if (!success) {
          const errorMessage =
            typeof data === 'string'
              ? data
              : 'Ha ocurrido un error, por favor intente más tarde'
          addNotification({ style: 'success', msg: errorMessage })
          return
        }

        getProfiles()
        addNotification({ style: 'success', msg: data })
        onHide()
      })
      .catch((err) => {
        addNotification({ style: 'danger', msg: err.message })
      })
  })

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

const mapDispatchToProps = {
  addNotification: (notification) => addNotification(notification)
}

export default connect(null, mapDispatchToProps)(AddProfileModal)