Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1643 | Rev 1645 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1644 stevensc 1
import React, { useEffect } 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
 
1644 stevensc 78
  useEffect(() => {
79
    console.log(showModal)
80
  }, [showModal])
81
 
1638 stevensc 82
  return (
1639 stevensc 83
    <Modal
84
      show={showModal}
85
      onClose={closeModal}
86
      title={`Reportar ${type.toLowerCase()}`}
87
    >
1638 stevensc 88
      <Form onSubmit={onSubmit} autoComplete='off'>
89
        <SelectInput
90
          id='reason'
91
          name='reason'
92
          label='Motivo'
93
          options={reasons}
94
          control={control}
95
          error={errors.reason?.message}
96
          rules={{ required: true }}
97
        />
98
 
99
        <SelectInput
100
          id='block_user'
101
          name='block_user'
102
          label='Bloquear a este usuario'
103
          options={block_options}
104
          control={control}
105
          error={errors.block_user?.message}
106
          rules={{ required: true }}
107
        />
108
 
109
        <FormInputText
110
          name='comment'
111
          control={control}
112
          rules={{ required: false }}
113
        />
114
      </Form>
115
    </Modal>
116
  )
117
}