Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2044 | Rev 3069 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 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";
2243 stevensc 6
import { axios } from "../../../../../utils";
7
import { CKEditor } from "ckeditor4-react";
1 www 8
import FormErrorFeedback from "../../../../../shared/form-error-feedback/FormErrorFeedback";
9
import Spinner from "../../../../../shared/loading-spinner/Spinner";
2243 stevensc 10
import { CKEDITOR_OPTIONS } from "../../../../../utils";
1 www 11
import parse from "html-react-parser";
12
 
13
const StyledSpinnerContainer = styled.div`
14
  position: absolute;
15
  left: 0;
16
  top: 0;
17
  width: 100%;
18
  height: 100%;
19
  background: rgba(255, 255, 255, 0.4);
20
  display: flex;
21
  justify-content: center;
22
  align-items: center;
23
  z-index: 300;
24
`;
25
 
26
const Overview = (props) => {
27
  // props destructuring
28
  const { groupId, overview, addNotification } = props;
29
  // react hook form
30
  const {
31
    register,
32
    errors,
33
    handleSubmit,
34
    setValue,
35
    clearErrors,
36
    getValues,
37
    setError,
38
  } = useForm();
39
 
233 steven 40
 
41
 
1 www 42
  // states
43
  const [isModalOpen, setIsModalOpen] = useState(false);
44
  const [loading, setLoading] = useState(false);
45
  const [settedOverview, setSettedOverview] = useState(overview);
46
 
47
  useEffect(() => {
48
    register("description", {
49
      required: "Este campo es requerido",
50
    });
51
    setValue("description", settedOverview);
52
  }, []);
53
 
54
  const handleModalOpen = (event) => {
55
    event && event.preventDefault();
56
    setIsModalOpen(!isModalOpen);
57
  };
58
 
59
  const onSubmitHandler = async (data) => {
60
    // profile/my-profiles/extended', [ 'id' => $user_profile_id_encrypted]
61
    // https://leaderslinked.com/profile/my-profiles/extended/MzU4NDg3ODcg
62
    setLoading(true);
63
    const formData = new FormData();
2243 stevensc 64
    (data);
1 www 65
    Object.entries(data).map(([key, value]) => {
66
      formData.append(key, value);
67
    });
68
    await axios
69
      .post(`/group/my-groups/extended/${groupId}`, formData)
70
      .then((response) => {
71
        const resData = response.data;
2243 stevensc 72
        (resData);
1 www 73
        if (resData.success) {
74
          setSettedOverview(resData.data);
75
          handleModalOpen();
76
        } else {
77
          const resError = resData.data;
78
          if (resError.constructor.name === "Object") {
79
            Object.entries(resError).map(([key, value]) => {
80
              if (key in getValues()) {
81
                setError(key, {
82
                  type: "manual",
83
                  message: Array.isArray(value) ? value[0] : value,
84
                });
85
              }
86
            });
87
          } else {
88
            addNotification({
89
              style: "danger",
90
              msg: resError,
91
            });
92
          }
93
        }
94
      });
95
    setLoading(false);
96
  };
97
 
98
  return (
99
    <React.Fragment>
100
      <div className="user-profile-extended-ov">
101
        <h3>
230 steven 102
          Visión general
1 www 103
          <a
104
            href="#"
105
            title=""
106
            className="btn-extended-edit"
107
            onClick={handleModalOpen}
108
          >
109
            <i className="fa fa-pencil"></i>
110
          </a>
111
        </h3>
112
        <span id="overview-description">{parse(`${settedOverview}`)}</span>
113
      </div>
114
 
115
      <Modal
116
        show={isModalOpen}
117
        onHide={handleModalOpen}
118
        style={{ overflowY: "scroll" }}
119
      >
120
        <Modal.Header closeButton>
233 steven 121
          <Modal.Title>Visión general</Modal.Title>
1 www 122
        </Modal.Header>
123
        <form onSubmit={handleSubmit(onSubmitHandler)}>
124
          <Modal.Body>
125
            <CKEditor
126
              data={settedOverview}
127
              onChange={(e) => {
128
                const text = e.editor.getData();
129
                setValue("description", text);
130
                if (errors.description) {
131
                  clearErrors("description");
132
                }
133
              }}
2044 steven 134
              config={CKEDITOR_OPTIONS}
1 www 135
              name="description"
136
            />
137
            {errors.description && (
138
              <FormErrorFeedback>
139
                {errors.description.message}
140
              </FormErrorFeedback>
141
            )}
142
          </Modal.Body>
143
          <Modal.Footer>
144
            <Button type="submit">Enviar</Button>
145
            <Button variant="danger" onClick={handleModalOpen}>
1206 stevensc 146
              Cancelar
1 www 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 Overview;