Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6900 | 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 Privacy = ({
15
  groupId = '',
16
  privacies = [],
17
  privacy: defaultValue = '',
18
  onChange = () => null,
19
}) => {
20
  const [privacy, setPrivacy] = useState('')
21
  const [privacyKey, setPrivacyKey] = useState('')
22
  const [isModalOpen, setIsModalOpen] = useState(false)
23
  const [loading, setLoading] = useState(false)
24
  const labels = useSelector(({ intl }) => intl.labels)
25
  const dispatch = useDispatch()
26
 
27
  const { register, errors, handleSubmit, getValues, setError } = useForm()
28
 
29
  const handleModalOpen = () => {
30
    setIsModalOpen(!isModalOpen)
31
  }
32
 
33
  const onSubmitHandler = async (data) => {
34
    setLoading(true)
35
    const formData = new FormData()
36
    Object.entries(data).map(([key, value]) => formData.append(key, value))
37
 
38
    await axios
39
      .post(`/group/my-groups/privacy/${groupId}`, formData)
40
      .then((response) => {
41
        const { data, success } = response.data
42
 
43
        if (success) {
44
          const resError = data
45
 
46
          if (resError.constructor.name !== 'Object') {
47
            dispatch(addNotification({ style: 'danger', msg: resError }))
48
            return
49
          }
50
 
51
          Object.entries(resError).map(([key, value]) => {
52
            if (key in getValues()) {
53
              setError(key, {
54
                type: 'manual',
55
                message: Array.isArray(value) ? value[0] : value,
56
              })
57
            }
58
          })
59
 
60
          return
61
        }
62
 
63
        onChange(data)
64
        handleModalOpen()
65
      })
66
    setLoading(false)
67
  }
68
 
69
  useEffect(() => {
70
    axios.get(`/group/my-groups/privacy/${groupId}`).then((response) => {
71
      const { data, success } = response.data
72
      if (!success) {
73
        return
74
      }
75
 
76
      setPrivacyKey(data)
77
    })
78
  }, [privacy])
79
 
80
  useEffect(() => {
81
    setPrivacy(defaultValue)
82
  }, [defaultValue])
83
 
84
  return (
85
    <>
86
      <div className="experience__user-card">
87
        <div className="card__options-container">
88
          <h4>Privacidad</h4>
89
          <IconButton onClick={handleModalOpen}>
90
            <EditIcon />
91
          </IconButton>
92
        </div>
93
        <p>{privacy}</p>
94
      </div>
95
 
96
      <Modal show={isModalOpen} onHide={handleModalOpen}>
97
        <Modal.Header closeButton>
98
          <Modal.Title>Privacidad</Modal.Title>
99
        </Modal.Header>
100
        <form onSubmit={handleSubmit(onSubmitHandler)}>
101
          <Modal.Body>
102
            <select
103
              name="privacy"
104
              id="privacy"
105
              defaultValue={privacyKey}
106
              ref={register({
107
                required: 'Por favor eliga una privacidad',
108
              })}
109
            >
110
              <option value="" hidden>
111
                Privacidad
112
              </option>
113
              {Object.entries(privacies).map(([key, value]) => (
114
                <option value={key} key={key}>
115
                  {value}
116
                </option>
117
              ))}
118
            </select>
119
            {errors.privacy && (
120
              <FormErrorFeedback>{errors.privacy.message}</FormErrorFeedback>
121
            )}
122
          </Modal.Body>
123
          <Modal.Footer>
124
            <Button variant="primary" type="submit">
125
              {labels.accept}
126
            </Button>
127
            <Button variant="danger" onClick={handleModalOpen}>
128
              {labels.cancel}
129
            </Button>
130
          </Modal.Footer>
131
        </form>
132
        {loading && (
133
          <LoaderContainer>
134
            <Spinner />
135
          </LoaderContainer>
136
        )}
137
      </Modal>
138
    </>
139
  )
140
}
141
 
142
export default Privacy