Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1170 | Rev 2044 | 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;
28
  // react hook form
29
  const {
30
    register,
31
    errors,
32
    handleSubmit,
33
    setValue,
34
    clearErrors,
35
    getValues,
36
    setError,
37
  } = useForm();
38
 
233 steven 39
 
40
 
1 www 41
  // states
42
  const [isModalOpen, setIsModalOpen] = useState(false);
43
  const [loading, setLoading] = useState(false);
44
  const [settedOverview, setSettedOverview] = useState(overview);
45
 
46
  useEffect(() => {
47
    register("description", {
48
      required: "Este campo es requerido",
49
    });
50
    setValue("description", settedOverview);
51
  }, []);
52
 
53
  const handleModalOpen = (event) => {
54
    event && event.preventDefault();
55
    setIsModalOpen(!isModalOpen);
56
  };
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
     (data);
64
    Object.entries(data).map(([key, value]) => {
65
      formData.append(key, value);
66
    });
67
    await axios
68
      .post(`/group/my-groups/extended/${groupId}`, formData)
69
      .then((response) => {
70
        const resData = response.data;
71
         (resData);
72
        if (resData.success) {
73
          setSettedOverview(resData.data);
74
          handleModalOpen();
75
        } else {
76
          const resError = resData.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>
99
      <div className="user-profile-extended-ov">
100
        <h3>
230 steven 101
          Visión general
1 www 102
          <a
103
            href="#"
104
            title=""
105
            className="btn-extended-edit"
106
            onClick={handleModalOpen}
107
          >
108
            <i className="fa fa-pencil"></i>
109
          </a>
110
        </h3>
111
        <span id="overview-description">{parse(`${settedOverview}`)}</span>
112
      </div>
113
 
114
      <Modal
115
        show={isModalOpen}
116
        onHide={handleModalOpen}
117
        style={{ overflowY: "scroll" }}
118
      >
119
        <Modal.Header closeButton>
233 steven 120
          <Modal.Title>Visión general</Modal.Title>
1 www 121
        </Modal.Header>
122
        <form onSubmit={handleSubmit(onSubmitHandler)}>
123
          <Modal.Body>
124
            <CKEditor
125
              data={settedOverview}
126
              onChange={(e) => {
127
                const text = e.editor.getData();
128
                setValue("description", text);
129
                if (errors.description) {
130
                  clearErrors("description");
131
                }
132
              }}
133
              config={{
134
                startupFocus: "end",
1120 steven 135
                allowedContent: false,
1170 steven 136
                image_previewText: ' ',
1120 steven 137
                toolbarGroups: [
138
                  // { name: 'document',	   groups: [ 'mode', 'document', 'doctools' ] },
139
                  // { name: 'clipboard',   groups: [ 'undo' ] },
140
                  { name: 'editing', groups: ['find', 'selection', 'spellchecker'] },
141
                  { name: 'forms' },
142
                  { name: 'basicstyles', groups: ['basicstyles', 'cleanup'] },
143
                  { name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi'] },
144
                  { name: 'links' },
145
                  { name: 'insert' },
146
                  { name: 'styles' },
147
                  { name: 'colors' },
148
                  { name: 'tools' },
149
                  { name: 'others' },
150
                ],
151
                // removeButtons: 'Clipboard,Paste',
152
                removePlugins: 'elementspath'
1 www 153
              }}
154
              name="description"
155
            />
156
            {errors.description && (
157
              <FormErrorFeedback>
158
                {errors.description.message}
159
              </FormErrorFeedback>
160
            )}
161
          </Modal.Body>
162
          <Modal.Footer>
163
            <Button type="submit">Enviar</Button>
164
            <Button variant="danger" onClick={handleModalOpen}>
1206 stevensc 165
              Cancelar
1 www 166
            </Button>
167
          </Modal.Footer>
168
        </form>
169
        {loading && (
170
          <StyledSpinnerContainer>
171
            <Spinner />
172
          </StyledSpinnerContainer>
173
        )}
174
      </Modal>
175
    </React.Fragment>
176
  );
177
};
178
 
179
export default Overview;