Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1664 | Rev 1760 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1645 stevensc 1
import React from 'react'
1664 stevensc 2
import { useForm } from 'react-hook-form'
1638 stevensc 3
import { useDispatch, useSelector } from 'react-redux'
4
 
1664 stevensc 5
import { block_options, reasons } from '@app/constants/report'
1638 stevensc 6
import { addNotification } from '@app/redux/notification/notification.actions'
1643 stevensc 7
import { closeReportModal } from '@app/redux/report/report.actions'
1664 stevensc 8
import { sendReport } from '@app/services/reports'
1638 stevensc 9
 
1664 stevensc 10
import FormInputText from '../UI/form/FormInputText'
11
import SelectInput from '../UI/form/SelectField'
1646 stevensc 12
import Modal from '../UI/modal/Modal'
1638 stevensc 13
 
14
export default function ReportModal() {
1639 stevensc 15
  const { showModal, type, reportUrl, onComplete } = useSelector(
16
    (state) => state.report
17
  )
1638 stevensc 18
  const dispatch = useDispatch()
19
 
1664 stevensc 20
  const { control, errors, getValues, handleSubmit } = useForm()
1638 stevensc 21
 
1664 stevensc 22
  const onSubmit = handleSubmit(async () => {
1638 stevensc 23
    try {
1646 stevensc 24
      const formData = getValues()
1639 stevensc 25
      const { success, data } = await sendReport(reportUrl, formData)
1638 stevensc 26
 
27
      if (!success) {
28
        throw new Error(data)
29
      }
30
 
1647 stevensc 31
      if (onComplete) {
32
        onComplete()
33
      }
34
 
35
      dispatch(addNotification({ style: 'success', msg: data.message }))
1639 stevensc 36
      dispatch(closeReportModal())
1638 stevensc 37
    } catch (error) {
38
      dispatch(addNotification({ style: 'danger', msg: error.message }))
39
    }
1664 stevensc 40
  })
1638 stevensc 41
 
1643 stevensc 42
  const closeModal = () => dispatch(closeReportModal())
1638 stevensc 43
 
44
  return (
1639 stevensc 45
    <Modal
46
      show={showModal}
47
      onClose={closeModal}
48
      title={`Reportar ${type.toLowerCase()}`}
1646 stevensc 49
      onAccept={onSubmit}
1639 stevensc 50
    >
1646 stevensc 51
      <SelectInput
52
        id='reason'
53
        name='reason'
54
        label='Motivo'
55
        options={reasons}
56
        control={control}
57
        error={errors.reason?.message}
1665 stevensc 58
        rules={{ required: 'Este campo es requerido' }}
1649 stevensc 59
        defaultValue={reasons[0].value}
1646 stevensc 60
      />
1638 stevensc 61
 
1646 stevensc 62
      <SelectInput
63
        id='block_user'
64
        name='block_user'
65
        label='Bloquear a este usuario'
66
        options={block_options}
67
        control={control}
68
        error={errors.block_user?.message}
1665 stevensc 69
        rules={{ required: 'Este campo es requerido' }}
1649 stevensc 70
        defaultValue={block_options[1].value}
1646 stevensc 71
      />
1638 stevensc 72
 
1646 stevensc 73
      <FormInputText
74
        name='comment'
75
        control={control}
1665 stevensc 76
        rules={{ required: 'Este campo es requerido' }}
1650 stevensc 77
        error={errors.comment?.message}
1646 stevensc 78
        label='Comentario'
79
        placeholder='(Opcional)'
80
      />
1638 stevensc 81
    </Modal>
82
  )
83
}