Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1644 | Rev 1646 | 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'
1638 stevensc 8
 
9
import Form from '../UI/form/Form'
10
import FormInputText from '../UI/form/FormInputText'
11
import SelectInput from '../UI/form/SelectField'
12
import Modal from '../UI/modal/Modal'
13
 
14
const reasons = [
15
  {
16
    name: 'Contenido sexual',
17
    value: 's'
18
  },
19
  {
20
    name: 'Contenido ofensivo',
21
    value: 'o'
22
  },
23
  {
24
    name: 'Discriminación',
25
    value: 'd'
26
  },
27
  {
28
    name: 'Adicciones',
29
    value: 'a'
30
  },
31
  {
32
    name: 'Terrorismo',
33
    value: 't'
34
  },
35
  {
36
    name: 'Otro',
37
    value: 'ot'
38
  }
39
]
40
 
41
const block_options = [
42
  {
43
    name: 'Sí',
44
    value: 'y'
45
  },
46
  {
47
    name: 'No',
48
    value: 'n'
49
  }
50
]
51
 
52
export default function ReportModal() {
1639 stevensc 53
  const { showModal, type, reportUrl, onComplete } = useSelector(
54
    (state) => state.report
55
  )
1638 stevensc 56
  const dispatch = useDispatch()
57
 
58
  const { control, errors, handleSubmit } = useForm()
59
 
1639 stevensc 60
  const onSubmit = handleSubmit(async (formData) => {
1638 stevensc 61
    try {
1639 stevensc 62
      const { success, data } = await sendReport(reportUrl, formData)
1638 stevensc 63
 
64
      if (!success) {
65
        throw new Error(data)
66
      }
67
 
1639 stevensc 68
      onComplete()
1638 stevensc 69
      dispatch(addNotification({ style: 'success', msg: data }))
1639 stevensc 70
      dispatch(closeReportModal())
1638 stevensc 71
    } catch (error) {
72
      dispatch(addNotification({ style: 'danger', msg: error.message }))
73
    }
74
  })
75
 
1643 stevensc 76
  const closeModal = () => dispatch(closeReportModal())
1638 stevensc 77
 
78
  return (
1639 stevensc 79
    <Modal
80
      show={showModal}
81
      onClose={closeModal}
82
      title={`Reportar ${type.toLowerCase()}`}
83
    >
1638 stevensc 84
      <Form onSubmit={onSubmit} autoComplete='off'>
85
        <SelectInput
86
          id='reason'
87
          name='reason'
88
          label='Motivo'
89
          options={reasons}
90
          control={control}
91
          error={errors.reason?.message}
92
          rules={{ required: true }}
93
        />
94
 
95
        <SelectInput
96
          id='block_user'
97
          name='block_user'
98
          label='Bloquear a este usuario'
99
          options={block_options}
100
          control={control}
101
          error={errors.block_user?.message}
102
          rules={{ required: true }}
103
        />
104
 
105
        <FormInputText
106
          name='comment'
107
          control={control}
108
          rules={{ required: false }}
109
        />
110
      </Form>
111
    </Modal>
112
  )
113
}