Proyectos de Subversion LeadersLinked - SPA

Rev

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