Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1761 | Rev 2104 | 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
 
2100 stevensc 10
import Modal from '../UI/modal/Modal'
11
import Input from '../UI/inputs/Input'
1664 stevensc 12
import SelectInput from '../UI/form/SelectField'
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
 
2100 stevensc 20
  const { control, register, errors, getValues, handleSubmit } = useForm({
1760 stevensc 21
    defaultValues: {
22
      reason: reasons[0].value,
23
      block_user: block_options[1].value,
24
      comment: ''
25
    }
26
  })
1638 stevensc 27
 
1664 stevensc 28
  const onSubmit = handleSubmit(async () => {
1638 stevensc 29
    try {
1646 stevensc 30
      const formData = getValues()
1639 stevensc 31
      const { success, data } = await sendReport(reportUrl, formData)
1638 stevensc 32
 
33
      if (!success) {
34
        throw new Error(data)
35
      }
36
 
1647 stevensc 37
      if (onComplete) {
38
        onComplete()
39
      }
40
 
41
      dispatch(addNotification({ style: 'success', msg: data.message }))
1639 stevensc 42
      dispatch(closeReportModal())
1638 stevensc 43
    } catch (error) {
44
      dispatch(addNotification({ style: 'danger', msg: error.message }))
45
    }
1664 stevensc 46
  })
1638 stevensc 47
 
1643 stevensc 48
  const closeModal = () => dispatch(closeReportModal())
1638 stevensc 49
 
50
  return (
1639 stevensc 51
    <Modal
52
      show={showModal}
53
      onClose={closeModal}
54
      title={`Reportar ${type.toLowerCase()}`}
1646 stevensc 55
      onAccept={onSubmit}
1639 stevensc 56
    >
1646 stevensc 57
      <SelectInput
58
        id='reason'
59
        name='reason'
60
        label='Motivo'
61
        options={reasons}
62
        control={control}
63
        error={errors.reason?.message}
1665 stevensc 64
        rules={{ required: 'Este campo es requerido' }}
1649 stevensc 65
        defaultValue={reasons[0].value}
1646 stevensc 66
      />
1638 stevensc 67
 
1646 stevensc 68
      <SelectInput
69
        id='block_user'
70
        name='block_user'
71
        label='Bloquear a este usuario'
72
        options={block_options}
73
        control={control}
74
        error={errors.block_user?.message}
1665 stevensc 75
        rules={{ required: 'Este campo es requerido' }}
1649 stevensc 76
        defaultValue={block_options[1].value}
1646 stevensc 77
      />
1638 stevensc 78
 
2100 stevensc 79
      <Input
1646 stevensc 80
        name='comment'
81
        label='Comentario'
82
        placeholder='(Opcional)'
2100 stevensc 83
        inputRef={register}
84
        error={errors.comment?.message}
1646 stevensc 85
      />
1638 stevensc 86
    </Modal>
87
  )
88
}