Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3688 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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