Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4945 | Rev 4988 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 4945 Rev 4987
Línea 1... Línea 1...
1
/* eslint-disable react/prop-types */
1
/* eslint-disable react/prop-types */
2
import React, { useState, useEffect } from "react";
2
import React, { useState, useEffect } from "react"
3
import { connect } from "react-redux";
3
import { connect } from "react-redux"
4
import Modal from "react-bootstrap/Modal";
4
import Modal from "react-bootstrap/Modal"
5
import Button from "react-bootstrap/Button";
5
import Button from "react-bootstrap/Button"
6
import { useForm } from "react-hook-form";
6
import { useForm } from "react-hook-form"
7
import styled from "styled-components";
7
import styled from "styled-components"
8
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback";
8
import FormErrorFeedback from "../../../shared/form-error-feedback/FormErrorFeedback"
9
import Spinner from "../../../shared/loading-spinner/Spinner";
9
import Spinner from "../../../shared/loading-spinner/Spinner"
10
import { addNotification } from "../../../redux/notification/notification.actions";
10
import { addNotification } from "../../../redux/notification/notification.actions"
11
import {
-
 
12
  closeShareModal,
-
 
13
  openShareModal,
-
 
14
  setModalType,
-
 
15
} from "../../../redux/share-modal/shareModal.actions";
11
import { closeShareModal, openShareModal, setModalType } from "../../../redux/share-modal/shareModal.actions"
16
import { addFeed, fetchFeeds } from "../../../redux/feed/feed.actions";
12
import { addFeed, fetchFeeds } from "../../../redux/feed/feed.actions"
17
import DropzoneComponent from "../../../shared/dropzone/DropzoneComponent";
13
import DropzoneComponent from "../../../shared/dropzone/DropzoneComponent"
18
import { shareModalTypes } from "../../../redux/share-modal/shareModal.types";
14
import { shareModalTypes } from "../../../redux/share-modal/shareModal.types"
19
import { feedTypes } from "../../../redux/feed/feed.types";
15
import { feedTypes } from "../../../redux/feed/feed.types"
20
import { CKEditor } from "ckeditor4-react";
16
import { CKEditor } from "ckeditor4-react"
21
import { axios, CKEDITOR_OPTIONS } from "../../../utils";
17
import { axios, CKEDITOR_OPTIONS } from "../../../utils"
22
import ConfirmModal from "../../../shared/confirm-modal/ConfirmModal";
18
import ConfirmModal from "../../../shared/confirm-modal/ConfirmModal"
Línea 23... Línea 19...
23
 
19
 
24
const StyledSpinnerContainer = styled.div`
20
const StyledSpinnerContainer = styled.div`
25
  position: absolute;
21
  position: absolute
26
  left: 0;
22
  left: 0
27
  top: 0;
23
  top: 0
28
  width: 100%;
24
  width: 100%
29
  height: 100%;
25
  height: 100%
30
  background: rgba(255, 255, 255, 0.4);
26
  background: rgba(255, 255, 255, 0.4)
31
  display: flex;
27
  display: flex
32
  justify-content: center;
28
  justify-content: center
33
  align-items: center;
29
  align-items: center
34
  z-index: 300;
30
  z-index: 300
Línea 35... Línea 31...
35
`;
31
`
36
 
32
 
37
const ShareModal = (props) => {
33
const ShareModal = (props) => {
38
  // Redux State Destructuring
34
  // Redux State Destructuring
Línea 45... Línea 41...
45
    feedType,
41
    feedType,
46
    fetchFeeds,
42
    fetchFeeds,
47
    currentPage,
43
    currentPage,
48
    timelineUrl,
44
    timelineUrl,
49
    feedSharedId
45
    feedSharedId
50
  } = props;
46
  } = props
51
  // Redux dispatch Destructuring
47
  // Redux dispatch Destructuring
52
  const { closeShareModal, addNotification, addFeed, openShareModal } = props;
48
  const { closeShareModal, addNotification, addFeed, openShareModal } = props
53
  // states
49
  // states
54
  const [loading, setLoading] = useState(false);
50
  const [loading, setLoading] = useState(false)
55
  const [isCKEditorLoading, setIsCKEditorLoading] = useState(true);
51
  const [isCKEditorLoading, setIsCKEditorLoading] = useState(true)
56
  const [showConfirmModal, setShowConfirmModal] = useState(false);
52
  const [showConfirmModal, setShowConfirmModal] = useState(false)
Línea 57... Línea 53...
57
 
53
 
58
  const {
54
  const {
59
    register,
55
    register,
60
    unregister,
56
    unregister,
Línea 68... Línea 64...
68
  } = useForm({
64
  } = useForm({
69
    defaultValues: {
65
    defaultValues: {
70
      description: "",
66
      description: "",
71
      share_width: "",
67
      share_width: "",
72
    },
68
    },
73
  });
69
  })
Línea 74... Línea 70...
74
 
70
 
75
  useEffect(() => {
71
  useEffect(() => {
76
    register("description", {
72
    register("description", {
77
      required: { value: "true", message: "El campo es requerido" },
73
      required: { value: "true", message: "El campo es requerido" },
78
    });
74
    })
79
    register("posted_or_shared");
75
    register("posted_or_shared")
80
    if (
76
    if (
81
      modalType !== shareModalTypes.POST &&
77
      modalType !== shareModalTypes.POST &&
82
      modalType !== shareModalTypes.SHARE
78
      modalType !== shareModalTypes.SHARE
83
    ) {
79
    ) {
84
      register("file", {
80
      register("file", {
85
        required: { value: "true", message: "El campo es requerido" },
81
        required: { value: "true", message: "El campo es requerido" },
86
      });
82
      })
87
    } else {
83
    } else {
88
      if (!getValues("file")) unregister("file");
84
      if (!getValues("file")) unregister("file")
89
    }
85
    }
Línea 90... Línea 86...
90
  }, [modalType]);
86
  }, [modalType])
91
 
87
 
92
  const recomendationText = () => {
88
  const recomendationText = () => {
93
    switch (modalType) {
89
    switch (modalType) {
94
      case shareModalTypes.IMAGE:
90
      case shareModalTypes.IMAGE:
95
        return "Tamaño recomendado: 720x720";
91
        return "Tamaño recomendado: 720x720"
96
      case shareModalTypes.FILE:
92
      case shareModalTypes.FILE:
97
        return "solo documentos PDF";
93
        return "solo documentos PDF"
98
      case shareModalTypes.VIDEO:
94
      case shareModalTypes.VIDEO:
99
        return "Video de extensión mp4, mpeg, webm";
95
        return "Video de extensión mp4, mpeg, webm"
100
      default:
96
      default:
101
        return "";
97
        return ""
102
    }
98
    }
103
  };
99
  }
104
  useEffect(() => {
100
  useEffect(() => {
105
    const postedOrShared = modalType === shareModalTypes.SHARE ? "s" : "p";
101
    const postedOrShared = modalType === shareModalTypes.SHARE ? "s" : "p"
106
    setValue("posted_or_shared", postedOrShared);
102
    setValue("posted_or_shared", postedOrShared)
107
    if (getValues("file") || getValues("description")) {
103
    if (getValues("file") || getValues("description")) {
108
      if (modalType !== lastModalType) {
104
      if (modalType !== lastModalType) {
109
        closeShareModal();
105
        closeShareModal()
110
        handleShowConfirmModal();
106
        handleShowConfirmModal()
111
      }
107
      }
Línea 112... Línea 108...
112
    }
108
    }
113
  }, [modalType]);
109
  }, [modalType])
114
 
110
 
115
  const hideDuplicatedModal = () => {
111
  const hideDuplicatedModal = () => {
116
    setTimeout(() => {
112
    setTimeout(() => {
117
      const modals = document.getElementsByClassName('modal');
113
      const modals = document.getElementsByClassName('modal')
118
      if (modals.length > 1 && modals[0].style.display !== 'none') {
114
      if (modals.length > 1 && modals[0].style.display !== 'none') {
119
        const currentModal = modals[0];
115
        const currentModal = modals[0]
120
        currentModal.style.display = 'none';
116
        currentModal.style.display = 'none'
121
        for (let index = 0; index < modals.length; index++) {
117
        for (let index = 0; index < modals.length; index++) {
122
          const element = modals[index];
118
          const element = modals[index]
123
          element.removeAttribute("tabindex");
119
          element.removeAttribute("tabindex")
124
        }
120
        }
Línea 125... Línea 121...
125
      }
121
      }
126
    }, 3000);
122
    }, 3000)
127
  }
123
  }
128
 
124
 
Línea 129... Línea 125...
129
  useEffect(() => {
125
  useEffect(() => {
130
    clearErrors();
126
    clearErrors()
131
    hideDuplicatedModal();
127
    hideDuplicatedModal()
Línea 132... Línea 128...
132
  }, [isOpen]);
128
  }, [isOpen])
133
 
129
 
134
  const handleShowConfirmModal = () => {
130
  const handleShowConfirmModal = () => {
135
    setShowConfirmModal(!showConfirmModal);
131
    setShowConfirmModal(!showConfirmModal)
136
  };
132
  }
137
 
133
 
138
  const handleModalAccept = () => {
134
  const handleModalAccept = () => {
Línea 139... Línea 135...
139
    setShowConfirmModal(false);
135
    setShowConfirmModal(false)
140
    setValue("description", "");
136
    setValue("description", "")
141
    setValue("file", "");
137
    setValue("file", "")
142
    openShareModal(postUrl, modalType, feedType);
138
    openShareModal(postUrl, modalType, feedType)
143
    clearErrors();
139
    clearErrors()
144
  };
140
  }
Línea 145... Línea 141...
145
 
141
 
146
  const handleModalCancel = () => {
142
  const handleModalCancel = () => {
147
    setShowConfirmModal(false);
143
    setShowConfirmModal(false)
148
    closeShareModal();
144
    closeShareModal()
149
    setModalType(lastModalType);
145
    setModalType(lastModalType)
150
    openShareModal(postUrl, lastModalType, feedType);
-
 
151
  };
146
    openShareModal(postUrl, lastModalType, feedType)
152
 
147
  }
153
  const onSubmit = async (data, e) => {
148
 
154
    setLoading(true);
149
  const onSubmit = async (data, e) => {
155
    const currentFormData = new FormData();
-
 
156
    for (let input in data) {
150
    setLoading(true)
157
      currentFormData.append(input, data[input]);
151
    const currentFormData = new FormData()
158
      (`${input}:${data[input]}`);
152
    for (let input in data) {
159
    }
153
      currentFormData.append(input, data[input])
160
    await axios.post(postUrl, currentFormData).then((response) => {
154
    }
161
      const data = response.data;
155
    await axios.post(postUrl, currentFormData).then((response) => {
162
      const newFeed = data.data;
156
      const data = response.data
163
      (data);
157
      const newFeed = data.data
164
      if (data.success) {
158
      if (data.success) {
165
        closeShareModal();
159
        closeShareModal()
166
        // reset data
160
        // reset data
167
        e.target.reset();
161
        e.target.reset()
168
        setValue("description", "");
162
        setValue("description", "")
169
        setValue("file", "");
163
        setValue("file", "")
170
        clearErrors();
164
        clearErrors()
171
        addNotification({
165
        addNotification({
172
          style: "success",
166
          style: "success",
173
          msg: "La publicación ha sido compartida",
167
          msg: "La publicación ha sido compartida",
174
        });
168
        })
Línea 175... Línea 169...
175
        if (feedSharedId) {
169
        if (feedSharedId) {
176
          addFeed(newFeed, feedSharedId);
170
          addFeed(newFeed, feedSharedId)
177
        } else {
171
        } else {
178
          addFeed(newFeed);
172
          addFeed(newFeed)
179
        }
173
        }
180
        if (currentPage && timelineUrl) {
174
        if (currentPage && timelineUrl) {
181
          fetchFeeds(timelineUrl, currentPage)
175
          fetchFeeds(timelineUrl, currentPage)
182
        }
176
        }
183
 
177
 
184
      } else {
178
      } else {
185
        if (data.data.description || data.data.file || data.data.share_width) {
179
        if (data.data.description || data.data.file || data.data.share_width) {
186
          Object.entries(data.data).map(([key, value]) => {
180
          Object.entries(data.data).map(([key, value]) => {
187
            setError(key, { type: "required", message: value });
181
            setError(key, { type: "required", message: value })
Línea 188... Línea 182...
188
          });
182
          })
189
        } else {
183
        } else {
Línea 190... Línea 184...
190
          addNotification({
184
          addNotification({
191
            style: "danger",
185
            style: "danger",
192
            msg: data.data,
186
            msg: data.data,
193
          });
187
          })
Línea 194... Línea 188...
194
        }
188
        }
195
      }
189
      }
196
    });
190
    })
197
 
191
 
Línea 213... Línea 207...
213
          modalType={modalType}
207
          modalType={modalType}
214
          onUploaded={onUploadedHandler}
208
          onUploaded={onUploadedHandler}
215
          settedFile={getValues("file")}
209
          settedFile={getValues("file")}
216
          recomendationText={recomendationText()}
210
          recomendationText={recomendationText()}
217
        />
211
        />
218
      );
212
      )
219
    }
213
    }
220
  };
214
  }
Línea 221... Línea 215...
221
 
215
 
222
  const SharedWithSelectRender = () => {
216
  const SharedWithSelectRender = () => {
223
    if (feedType === feedTypes.DASHBOARD) {
217
    if (feedType === feedTypes.DASHBOARD) {
224
      return (
218
      return (
225
        <React.Fragment>
219
        <>
226
          <select
-
 
227
            // value={formData.shared_with}
220
          <select
228
            name="shared_with"
221
            name="shared_with"
229
            id="shared_with"
222
            id="shared_with"
230
            className="form-control"
-
 
231
            // onChange={(e) => onInputChangeHandler(e)}
-
 
232
            ref={register({
223
            className="form-control"
233
              required: "El campo es requerido",
-
 
234
            })}
224
            ref={register({ required: "El campo es requerido" })}
235
            defaultValue="p"
225
            defaultValue="p"
236
          >
226
          >
237
            <option disabled="disabled" value="" style={{ display: "none" }}>
227
            <option disabled="disabled" value="" style={{ display: "none" }}>
238
              Compartir con
228
              Compartir con
239
            </option>
229
            </option>
240
            <option value="p">Público</option>
230
            <option value="p">Público</option>
241
            <option value="c">Conexiones</option>
231
            <option value="c">Conexiones</option>
242
          </select>
-
 
243
          {errors.shared_with && (
232
          </select>
244
            <FormErrorFeedback>{errors.shared_with.message}</FormErrorFeedback>
233
          {errors.shared_with && <FormErrorFeedback>{errors.shared_with.message}</FormErrorFeedback>}
245
          )}
-
 
246
        </React.Fragment>
234
        </>
247
      );
235
      )
248
    }
236
    }
Línea 249... Línea 237...
249
  };
237
  }
250
 
238
 
251
  return (
239
  return (
252
    <React.Fragment>
240
    <React.Fragment>
Línea 262... Línea 250...
262
          <Modal.Body>
250
          <Modal.Body>
263
            {SharedWithSelectRender()}
251
            {SharedWithSelectRender()}
264
            <CKEditor
252
            <CKEditor
265
              data={watch("description")}
253
              data={watch("description")}
266
              onChange={(e) => {
254
              onChange={(e) => {
267
                const text = e.editor.getData();
255
                const text = e.editor.getData()
268
                setValue("description", text);
256
                setValue("description", text)
269
                if (errors.description && getValues('description')) clearErrors("description")
257
                if (errors.description && getValues('description')) clearErrors("description")
270
              }}
258
              }}
271
              config={CKEDITOR_OPTIONS}
259
              config={CKEDITOR_OPTIONS}
272
              name="description"
260
              name="description"
273
              onBeforeLoad={() => {
261
              onBeforeLoad={() => {
274
                setIsCKEditorLoading(false);
262
                setIsCKEditorLoading(false)
275
              }}
263
              }}
-
 
264
              onDialogShow={() => console.log('Show')}
276
            />
265
            />
277
            {isCKEditorLoading && (
266
            {isCKEditorLoading &&
278
              <StyledSpinnerContainer>
267
              <StyledSpinnerContainer>
279
                <Spinner />
268
                <Spinner />
280
              </StyledSpinnerContainer>
269
              </StyledSpinnerContainer>}
281
            )}
-
 
282
            {errors.description && (
-
 
283
              <FormErrorFeedback>
-
 
284
                {errors.description.message}
270
            {errors.description && <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>}
285
              </FormErrorFeedback>
-
 
286
            )}
-
 
Línea 287... Línea 271...
287
 
271
 
288
            {dropZoneRender()}
-
 
289
            {errors.file && (
272
            {dropZoneRender()}
290
              <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>
-
 
291
            )}
273
            {errors.file && <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>}
292
          </Modal.Body>
274
          </Modal.Body>
293
          <Modal.Footer>
275
          <Modal.Footer>
294
            <Button size="sm" type="submit">Enviar</Button>
276
            <Button size="sm" type="submit">Enviar</Button>
295
            <Button color="danger" size="sm" variant="danger" onClick={closeShareModal}>
277
            <Button color="danger" size="sm" variant="danger" onClick={closeShareModal}>
296
              Cancelar
278
              Cancelar
297
            </Button>
279
            </Button>
298
          </Modal.Footer>
280
          </Modal.Footer>
299
        </form>
281
        </form>
300
        {loading ? (
282
        {loading &&
301
          <StyledSpinnerContainer>
283
          <StyledSpinnerContainer>
302
            <Spinner />
284
            <Spinner />
303
          </StyledSpinnerContainer>
-
 
304
        ) : (
-
 
305
          ""
-
 
306
        )}
285
          </StyledSpinnerContainer>}
307
      </Modal>
286
      </Modal>
308
      <ConfirmModal
287
      <ConfirmModal
309
        show={showConfirmModal}
288
        show={showConfirmModal}
310
        onClose={handleModalCancel}
289
        onClose={handleModalCancel}
311
        onAccept={handleModalAccept}
290
        onAccept={handleModalAccept}
312
        acceptLabel="Aceptar"
291
        acceptLabel="Aceptar"
313
        message="¿No se ha compartido tu publicación , desea descartarlo?"
292
        message="¿No se ha compartido tu publicación , desea descartarlo?"
314
      />
293
      />
315
    </React.Fragment>
294
    </React.Fragment>
316
  );
295
  )
Línea 317... Línea 296...
317
};
296
}
318
 
297
 
319
const mapStateToProps = (state) => ({
298
const mapStateToProps = (state) => ({
320
  isOpen: state.shareModal.isOpen,
299
  isOpen: state.shareModal.isOpen,
321
  postUrl: state.shareModal.postUrl,
300
  postUrl: state.shareModal.postUrl,
322
  modalType: state.shareModal.modalType,
301
  modalType: state.shareModal.modalType,
323
  lastModalType: state.shareModal.lastModalType,
302
  lastModalType: state.shareModal.lastModalType,
324
  feedType: state.shareModal.feedType,
303
  feedType: state.shareModal.feedType,
Línea 325... Línea 304...
325
  feedSharedId: state.shareModal.feedSharedId,
304
  feedSharedId: state.shareModal.feedSharedId,
326
});
305
})
327
 
306
 
328
const mapDispatchToProps = {
307
const mapDispatchToProps = {
329
  addNotification: (notification) => addNotification(notification),
308
  addNotification: (notification) => addNotification(notification),
330
  closeShareModal: () => closeShareModal(),
309
  closeShareModal: () => closeShareModal(),
331
  openShareModal: (postUrl, modalType, feedType) => openShareModal(postUrl, modalType, feedType),
310
  openShareModal: (postUrl, modalType, feedType) => openShareModal(postUrl, modalType, feedType),
332
  setModalType: (modalType) => setModalType(modalType),
311
  setModalType: (modalType) => setModalType(modalType),
Línea 333... Línea 312...
333
  addFeed: (feed, feedSharedId) => addFeed(feed, feedSharedId),
312
  addFeed: (feed, feedSharedId) => addFeed(feed, feedSharedId),