Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6899 stevensc 1
import React, { useState, useEffect } from 'react'
2
import { axios } from '../../../utils'
3
import { useForm } from 'react-hook-form'
4
import { Button, Modal } from 'react-bootstrap'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import { useDispatch, useSelector } from 'react-redux'
7
import EditIcon from '@mui/icons-material/EditOutlined'
8
import IconButton from '@mui/material/IconButton'
9
 
10
import Spinner from '../../UI/Spinner'
11
import LoaderContainer from '../../UI/LoaderContainer'
12
import FormErrorFeedback from '../../UI/FormErrorFeedback'
13
 
14
const Industry = ({ groupId, industry: defaultValue, industries }) => {
15
  const [industryKey, setIndustryKey] = useState('')
16
  const [industry, setIndustry] = useState('')
17
  const [isModalOpen, setIsModalOpen] = useState(false)
18
  const [loading, setLoading] = useState(false)
19
  const labels = useSelector(({ intl }) => intl.labels)
20
  const dispatch = useDispatch()
21
 
22
  const { register, errors, handleSubmit, getValues, setError } = useForm()
23
 
24
  const handleModalOpen = () => {
25
    setIsModalOpen(!isModalOpen)
26
  }
27
 
28
  const onSubmitHandler = async (data) => {
29
    const formData = new FormData()
30
    Object.entries(data).map(([key, value]) => formData.append(key, value))
31
 
32
    await axios
33
      .post(`/group/my-groups/industry/${groupId}`, formData)
34
      .then((response) => {
35
        const { data, success } = response.data
36
 
37
        if (!success) {
38
          const resError = data
39
 
40
          if (resError.constructor.name !== 'Object') {
41
            dispatch(addNotification({ style: 'danger', msg: resError }))
42
            return
43
          }
44
 
45
          Object.entries(resError).map(([key, value]) => {
46
            if (key in getValues()) {
47
              setError(key, {
48
                type: 'manual',
49
                message: Array.isArray(value) ? value[0] : value,
50
              })
51
            }
52
          })
53
 
54
          return
55
        }
56
 
57
        setIndustry(data)
58
        handleModalOpen()
59
      })
60
    setLoading(false)
61
  }
62
 
63
  useEffect(() => {
64
    axios.get(`/group/my-groups/industry/${groupId}`).then((response) => {
65
      const { data, success } = response.data
66
      if (success && data <= 0) {
67
        return
68
      }
69
      setIndustryKey(data)
70
    })
71
  }, [industry])
72
 
73
  useEffect(() => {
74
    setIndustry(defaultValue)
75
  }, [defaultValue])
76
 
77
  return (
78
    <>
79
      <div className="experience__user-card">
80
        <div className="card__options-container">
81
          <h4>Industria</h4>
82
          <IconButton onClick={handleModalOpen}>
83
            <EditIcon />
84
          </IconButton>
85
        </div>
86
        <span id="overview-industry">{industry}</span>
87
      </div>
88
 
89
      <Modal show={isModalOpen} onHide={handleModalOpen}>
90
        <Modal.Header closeButton>
91
          <Modal.Title>Industria</Modal.Title>
92
        </Modal.Header>
93
        <form onSubmit={handleSubmit(onSubmitHandler)}>
94
          <Modal.Body>
95
            <select
96
              name="industry_id"
97
              id="industry_id"
98
              defaultValue={industryKey}
99
              ref={register({
100
                required: 'Por favor eliga una industria',
101
              })}
102
            >
103
              <option value="" hidden>
104
                Industria
105
              </option>
106
              {Object.entries(industries).map(([key, value]) => (
107
                <option value={key} key={key}>
108
                  {value}
109
                </option>
110
              ))}
111
            </select>
112
            {errors.industry_id && (
113
              <FormErrorFeedback>
114
                {errors.industry_id.message}
115
              </FormErrorFeedback>
116
            )}
117
          </Modal.Body>
118
          <Modal.Footer>
119
            <Button type="submit">{labels.accept}</Button>
120
            <Button variant="danger" onClick={handleModalOpen}>
121
              {labels.cancel}
122
            </Button>
123
          </Modal.Footer>
124
        </form>
125
        {loading && (
126
          <LoaderContainer>
127
            <Spinner />
128
          </LoaderContainer>
129
        )}
130
      </Modal>
131
    </>
132
  )
133
}
134
 
135
export default Industry