Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2801 | | Comparar con el anterior | Ultima modificación | Ver Log |

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