Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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