Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2105 | Rev 2107 | 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'
2104 stevensc 12
import Select from '../UI/inputs/Select'
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
 
2104 stevensc 20
  const { 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
    >
2104 stevensc 57
      <Select
1646 stevensc 58
        name='reason'
59
        label='Motivo'
60
        options={reasons}
2106 stevensc 61
        inputRef={register}
1646 stevensc 62
        error={errors.reason?.message}
1649 stevensc 63
        defaultValue={reasons[0].value}
1646 stevensc 64
      />
1638 stevensc 65
 
2104 stevensc 66
      <Select
1646 stevensc 67
        name='block_user'
68
        label='Bloquear a este usuario'
69
        options={block_options}
70
        error={errors.block_user?.message}
1649 stevensc 71
        defaultValue={block_options[1].value}
1646 stevensc 72
      />
1638 stevensc 73
 
2100 stevensc 74
      <Input
1646 stevensc 75
        name='comment'
76
        label='Comentario'
77
        placeholder='(Opcional)'
2100 stevensc 78
        inputRef={register}
79
        error={errors.comment?.message}
1646 stevensc 80
      />
1638 stevensc 81
    </Modal>
82
  )
83
}