Proyectos de Subversion LeadersLinked - SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3688 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
    () =>
21
      Object.entries(industries).find(
22
        ([name, value]) => value === industry
23
      )?.[0] || '',
24
    [industries, industry]
25
  )
26
  const industriesOptions = useMemo(
27
    () => Object.entries(industries).map(([value, name]) => ({ name, value })),
28
    [industries]
29
  )
30
  const dispatch = useDispatch()
31
 
32
  const {
33
    control,
34
    handleSubmit,
35
    formState: { errors, isSubmitting }
36
  } = useForm({
37
    defaultValues: {
38
      industry_id: ''
39
    },
40
    values: {
41
      industry_id: industryId
42
    }
43
  })
44
 
45
  const onSubmit = handleSubmit(async ({ industry_id }) => {
46
    const url = `/group/my-groups/industry/${groupId}`
47
    const formData = new FormData()
48
    formData.append('industry_id', industry_id)
49
 
50
    try {
51
      const response = await axios.post(url, formData)
52
      const { data, success } = response.data
53
 
54
      if (!success) {
55
        throw new Error('Error al editar el tipo de grupo')
56
      }
57
 
58
      onConfirm(data)
59
      onClose()
60
    } catch (error) {
61
      dispatch(addNotification({ style: 'danger', msg: error.message }))
62
    }
63
  })
64
 
65
  return (
66
    <Modal
67
      title='Industria'
68
      show={show}
69
      onClose={onClose}
70
      onAccept={onSubmit}
71
      loading={isSubmitting}
72
    >
73
      <Select
74
        name='industry_id'
75
        control={control}
76
        placeholder='Industria'
77
        rules={{
78
          required: 'Por favor eliga una industria'
79
        }}
80
        error={errors.industry_id?.message}
81
        options={industriesOptions}
82
      />
83
    </Modal>
84
  )
85
}