Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useMemo } from 'react';
2
import { useDispatch } from 'react-redux';
3
import { useForm } from 'react-hook-form';
4
 
5
import { axios } from '@utils';
6
import { addNotification } from '@store/notification/notification.actions';
7
 
8
import Modal from '@components/UI/modal/Modal';
9
import Select from '@components/UI/inputs/Select';
10
 
11
export default function IndustryModal({
12
  show = false,
13
  industries = {},
14
  industry = '',
15
  groupId,
16
  onClose = () => {},
17
  onConfirm = () => {}
18
}) {
19
  const industryId = useMemo(
20
    () => Object.values(industries).find((value) => value === industry) || '',
21
    [industries, industry]
22
  );
23
  const industriesOptions = useMemo(
24
    () => Object.entries(industries).map(([value, name]) => ({ name, value })),
25
    [industries]
26
  );
27
  const dispatch = useDispatch();
28
 
29
  const {
30
    control,
31
    handleSubmit,
32
    formState: { errors, isSubmitting }
33
  } = useForm({
34
    defaultValues: {
35
      industry_id: ''
36
    },
37
    values: {
38
      industry_id: industryId
39
    }
40
  });
41
 
42
  const onSubmit = handleSubmit(async ({ industry_id }) => {
43
    const url = `/group/my-groups/industry/${groupId}`;
44
    const formData = new FormData();
45
    formData.append('industry_id', industry_id);
46
 
47
    try {
48
      const response = await axios.post(url, formData);
49
      const { data, success } = response.data;
50
 
51
      if (!success) {
52
        throw new Error('Error al editar el tipo de grupo');
53
      }
54
 
55
      onConfirm(data);
56
      onClose();
57
    } catch (error) {
58
      dispatch(addNotification({ style: 'danger', msg: error.message }));
59
    }
60
  });
61
 
62
  return (
63
    <Modal
64
      title='Industria'
65
      show={show}
66
      onClose={onClose}
67
      onAccept={onSubmit}
68
      loading={isSubmitting}
69
    >
70
      <Select
71
        name='industry_id'
72
        control={control}
73
        placeholder='Industria'
74
        rules={{
75
          required: 'Por favor eliga una industria'
76
        }}
77
        error={errors.industry_id?.message}
78
        options={industriesOptions}
79
      />
80
    </Modal>
81
  );
82
}