Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
4638 stevensc 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import { Button, Modal } from "react-bootstrap";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
6
import { axios } from "../../../../../../../utils";
7
import FormErrorFeedback from "../../../../../../../shared/form-error-feedback/FormErrorFeedback";
8
import Spinner from "../../../../../../../shared/loading-spinner/Spinner";
9
import EditIcon from '@mui/icons-material/EditOutlined'
10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const Privacy = (props) => {
25
  // props destructuring
26
  const { groupId, addNotification, privacies, settedPrivacy, setSettedPrivacy, setSettedAccesibility } = props;
27
  // react hook form
28
  const {
29
    register,
30
    errors,
31
    handleSubmit,
32
    setValue,
33
    clearErrors,
34
    getValues,
35
    setError,
36
  } = useForm();
37
 
38
  // states
39
  const [isModalOpen, setIsModalOpen] = useState(false);
40
  const [loading, setLoading] = useState(false);
41
  const [settedPrivacyKey, setSettedPrivacyKey] = useState("");
42
 
43
  const handleModalOpen = (event) => {
44
    event && event.preventDefault();
45
    setIsModalOpen(!isModalOpen);
46
  };
47
 
48
  useEffect(() => {
49
    axios.get(`/group/my-groups/privacy/${groupId}`).then((response) => {
50
      const resData = response.data;
51
      (resData);
52
      if (resData.success) {
53
        if (resData.data) setSettedPrivacyKey(resData.data);
54
      }
55
    });
56
  }, [settedPrivacy]);
57
 
58
  const onSubmitHandler = async (data) => {
59
    // profile/my-profiles/extended', [ 'id' => $user_profile_id_encrypted]
60
    // https://leaderslinked.com/profile/my-profiles/extended/MzU4NDg3ODcg
61
    setLoading(true);
62
    const formData = new FormData();
63
    Object.entries(data).map(([key, value]) => formData.append(key, value))
64
    await axios.post(`/group/my-groups/privacy/${groupId}`, formData)
65
      .then(async ({ data }) => {
66
        if (data.success) {
67
          setSettedPrivacy(data.data);
68
          handleModalOpen();
69
          if (data.data === 'Privado') {
70
            const accessibilityData = new FormData()
71
            accessibilityData.append('accessibility', 'aa')
72
            const { data } = await axios.post(`/group/my-groups/accessibility/${groupId}`, accessibilityData)
73
            data.success && setSettedAccesibility(data.data)
74
          }
75
        } else {
76
          const resError = data.data;
77
          if (resError.constructor.name === "Object") {
78
            Object.entries(resError).map(([key, value]) => {
79
              if (key in getValues()) {
80
                setError(key, {
81
                  type: "manual",
82
                  message: Array.isArray(value) ? value[0] : value,
83
                });
84
              }
85
            });
86
          } else {
87
            addNotification({
88
              style: "danger",
89
              msg: resError,
90
            });
91
          }
92
        }
93
      });
94
    setLoading(false);
95
  };
96
 
97
  return (
98
    <React.Fragment>
4640 stevensc 99
      <div className="experience__user-card">
4638 stevensc 100
        <div className="card__options-container">
101
          <h4>
102
            Privacidad
103
          </h4>
104
          <button className='button-icon' onClick={handleModalOpen}>
105
            <EditIcon />
106
          </button>
107
        </div>
108
        <p>{settedPrivacy}</p>
109
      </div>
110
 
111
      {/* modal */}
112
      <Modal
113
        show={isModalOpen}
114
        onHide={handleModalOpen}
115
        style={{ overflowY: "scroll" }}
116
      >
117
        <Modal.Header closeButton>
118
          <Modal.Title>Privacidad</Modal.Title>
119
        </Modal.Header>
120
        <form onSubmit={handleSubmit(onSubmitHandler)}>
121
          <Modal.Body>
122
            <select
123
              name="privacy"
124
              id="privacy"
125
              defaultValue={settedPrivacyKey ? settedPrivacyKey : ""}
126
              ref={register({
127
                required: "Por favor eliga una privacidad",
128
              })}
129
            >
130
              <option value="" hidden>
131
                Privacidad
132
              </option>
133
              {Object.entries(privacies).map(([key, value]) => (
134
                <option value={key} key={key}>
135
                  {value}
136
                </option>
137
              ))}
138
            </select>
139
            {errors.privacy && (
140
              <FormErrorFeedback>{errors.privacy.message}</FormErrorFeedback>
141
            )}
142
          </Modal.Body>
143
          <Modal.Footer>
144
            <Button type="submit">Enviar</Button>
145
            <Button variant="danger" onClick={handleModalOpen}>
146
              Cancelar
147
            </Button>
148
          </Modal.Footer>
149
        </form>
150
        {loading && (
151
          <StyledSpinnerContainer>
152
            <Spinner />
153
          </StyledSpinnerContainer>
154
        )}
155
      </Modal>
156
    </React.Fragment>
157
  );
158
};
159
 
160
export default Privacy;