Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1645 | Rev 1647 | 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'
1638 stevensc 2
import { useDispatch, useSelector } from 'react-redux'
3
import { useForm } from 'react-hook-form'
4
 
5
import { sendReport } from '@app/services/reports'
6
import { addNotification } from '@app/redux/notification/notification.actions'
1643 stevensc 7
import { closeReportModal } from '@app/redux/report/report.actions'
1646 stevensc 8
import { block_options, reasons } from '@app/constants/report'
1638 stevensc 9
 
1646 stevensc 10
import Modal from '../UI/modal/Modal'
11
import SelectInput from '../UI/form/SelectField'
1638 stevensc 12
import FormInputText from '../UI/form/FormInputText'
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
 
1646 stevensc 20
  const { control, errors, getValues } = useForm()
1638 stevensc 21
 
1646 stevensc 22
  const onSubmit = 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
 
1639 stevensc 31
      onComplete()
1638 stevensc 32
      dispatch(addNotification({ style: 'success', msg: data }))
1639 stevensc 33
      dispatch(closeReportModal())
1638 stevensc 34
    } catch (error) {
35
      dispatch(addNotification({ style: 'danger', msg: error.message }))
36
    }
1646 stevensc 37
  }
1638 stevensc 38
 
1643 stevensc 39
  const closeModal = () => dispatch(closeReportModal())
1638 stevensc 40
 
41
  return (
1639 stevensc 42
    <Modal
43
      show={showModal}
44
      onClose={closeModal}
45
      title={`Reportar ${type.toLowerCase()}`}
1646 stevensc 46
      onAccept={onSubmit}
1639 stevensc 47
    >
1646 stevensc 48
      <SelectInput
49
        id='reason'
50
        name='reason'
51
        label='Motivo'
52
        options={reasons}
53
        control={control}
54
        error={errors.reason?.message}
55
        rules={{ required: true }}
56
      />
1638 stevensc 57
 
1646 stevensc 58
      <SelectInput
59
        id='block_user'
60
        name='block_user'
61
        label='Bloquear a este usuario'
62
        options={block_options}
63
        control={control}
64
        error={errors.block_user?.message}
65
        rules={{ required: true }}
66
      />
1638 stevensc 67
 
1646 stevensc 68
      <FormInputText
69
        name='comment'
70
        control={control}
71
        rules={{ required: false }}
72
        label='Comentario'
73
        placeholder='(Opcional)'
74
      />
1638 stevensc 75
    </Modal>
76
  )
77
}