Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev 16684 Rev 16685
Línea 1... Línea 1...
1
import React, { useState, useEffect } from 'react'
1
import React, { useState, useEffect } from 'react'
2
import axios from 'axios'
2
import axios from 'axios'
3
import { Button, Modal } from 'react-bootstrap'
3
import { config } from './helpers/ckeditor_config'
4
import { useForm } from 'react-hook-form'
4
import { useForm } from 'react-hook-form'
5
import { shareModalTypes } from '../redux/share-modal/shareModal.types'
5
import { Button, Modal } from 'react-bootstrap'
-
 
6
import { CKEditor } from 'ckeditor4-react'
-
 
7
 
6
import {
8
import {
7
  closeShareModal,
9
  closeShareModal,
8
  setModalType
10
  setModalType
9
} from '../redux/share-modal/shareModal.actions'
11
} from '../redux/share-modal/shareModal.actions'
10
import { useDispatch, useSelector } from 'react-redux'
-
 
11
import DropzoneComponent from './Dropzone/DropzoneComponent'
-
 
12
import { addFeed, fetchFeeds } from '../redux/feed/feed.actions'
12
import { addFeed, fetchFeeds } from '../redux/feed/feed.actions'
13
import { addNotification } from '../redux/notification/notification.actions'
13
import { addNotification } from '../redux/notification/notification.actions'
-
 
14
 
-
 
15
import { shareModalTypes } from '../redux/share-modal/shareModal.types'
-
 
16
 
14
import Spinner from './Spinner'
17
import Spinner from './Spinner'
15
import { CKEditor } from 'ckeditor4-react'
18
import DropzoneComponent from './Dropzone/DropzoneComponent'
-
 
19
 
-
 
20
const ShareModal = (props) => {
-
 
21
  const { addNotification, closeShareModal, setModalType, addFeed } = props // Redux actions
-
 
22
  const { postUrl, isOpen, modalType, currentPage, timelineUrl, feedSharedId } =
-
 
23
    props // Redux states
16
import { config } from './helpers/ckeditor_config'
24
  const [loading, setLoading] = useState(false)
Línea 17... Línea -...
17
 
-
 
18
const ShareModal = () => {
25
 
19
  const {
26
  const {
20
    register,
-
 
21
    unregister,
27
    register,
22
    errors,
28
    errors,
23
    handleSubmit,
29
    handleSubmit,
24
    setValue,
30
    setValue,
25
    getValues,
31
    getValues,
26
    clearErrors,
32
    clearErrors,
27
    reset
33
    reset
28
  } = useForm()
-
 
29
  const [loading, setLoading] = useState(false)
-
 
30
  const { postUrl, isOpen, modalType } = useSelector(
-
 
31
    ({ shareModal }) => shareModal
-
 
32
  )
-
 
33
  const { currentPage, timelineUrl, feedSharedId } = useSelector(
-
 
34
    ({ feed }) => feed
-
 
35
  )
-
 
Línea 36... Línea 34...
36
  const dispatch = useDispatch()
34
  } = useForm()
37
 
35
 
38
  const recomendationText = {
36
  const recomendationText = {
39
    IMAGE: 'Tamaño recomendado: 720x720',
37
    IMAGE: 'Tamaño recomendado: 720x720',
40
    FILE: 'solo documentos PDF',
38
    FILE: 'solo documentos PDF',
Línea 41... Línea 39...
41
    VIDEO: 'Video de extensión mp4, mpeg, webm'
39
    VIDEO: 'Video de extensión mp4, mpeg, webm'
42
  }
40
  }
43
 
41
 
Línea 44... Línea 42...
44
  const closeModal = () => {
42
  const closeModal = () => {
45
    dispatch(closeShareModal())
43
    closeShareModal()
46
  }
44
  }
Línea 55... Línea 53...
55
    const formData = new FormData()
53
    const formData = new FormData()
Línea 56... Línea 54...
56
 
54
 
Línea 57... Línea 55...
57
    Object.entries(data).map(([entry, value]) => formData.append(entry, value))
55
    Object.entries(data).map(([entry, value]) => formData.append(entry, value))
58
 
56
 
59
    axios
57
    axios
60
      .post(postUrl, currentFormData)
58
      .post(postUrl, formData)
61
      .then(({ data }) => {
59
      .then(({ data: response }) => {
62
        const newFeed = data.data
60
        const { success, data } = response
63
        if (!data.success) {
-
 
64
          typeof data.data === 'string'
-
 
65
            ? dispatch(
-
 
66
                addNotification({
-
 
67
                  style: 'danger',
-
 
68
                  msg: data.data
-
 
69
                })
61
        if (!success) {
70
              )
-
 
71
            : Object.entries(data.data).map(([key, value]) =>
-
 
72
                value.map((err) =>
-
 
73
                  dispatch(
-
 
74
                    addNotification({
62
          typeof data !== 'string'
75
                      style: 'danger',
-
 
76
                      msg: `${key}: ${err}`
-
 
77
                    })
-
 
78
                  )
63
            ? Object.entries(data).map(([key, value]) =>
-
 
64
                addNotification({ style: 'danger', msg: `${key}: ${value[0]}` })
79
                )
65
              )
80
              )
66
            : addNotification({ style: 'danger', msg: data })
81
          return
-
 
82
        }
-
 
83
        reset()
-
 
84
        clearErrors()
-
 
85
        dispatch(
-
 
86
          addNotification({
-
 
87
            style: 'success',
-
 
88
            msg: 'La publicación ha sido compartida'
-
 
Línea 89... Línea 67...
89
          })
67
          return
90
        )
68
        }
-
 
69
 
Línea -... Línea 70...
-
 
70
        if (currentPage && timelineUrl) {
91
 
71
          fetchFeeds(timelineUrl, currentPage)
-
 
72
        }
-
 
73
 
Línea 92... Línea 74...
92
        if (currentPage && timelineUrl)
74
        if (feedSharedId) {
93
          dispatch(fetchFeeds(timelineUrl, currentPage))
75
          addFeed(data, feedSharedId)
94
 
76
          return
95
        if (feedSharedId) return dispatch(addFeed(newFeed, feedSharedId))
77
        }
96
 
78
 
97
        return dispatch(addFeed(newFeed))
79
        addFeed(data)
98
      })
80
        addNotification({
99
      .catch((err) =>
81
          style: 'success',
100
        dispatch(addNotification({ style: 'danger', msg: `Error: ${err}` }))
82
          msg: 'La publicación ha sido compartida'
-
 
83
        })
-
 
84
        reset()
-
 
85
        clearErrors()
-
 
86
        closeModal()
-
 
87
      })
101
      )
88
      .catch((err) => {
Línea 102... Línea 89...
102
      .finally(() => {
89
        addNotification({ style: 'danger', msg: `Error: ${err}` })
103
        setLoading(false)
90
        throw new Error(err)
104
        closeModal()
91
      })
Línea 185... Línea 172...
185
            </Button>
172
            </Button>
186
            <Button
173
            <Button
187
              size="sm"
174
              size="sm"
188
              className="btn-tertiary"
175
              className="btn-tertiary"
189
              disabled={loading}
176
              disabled={loading}
190
              onClick={() => dispatch(setModalType(shareModalTypes.SURVEY))}
177
              onClick={() => setModalType(shareModalTypes.SURVEY)}
191
            >
178
            >
192
              Añadir encuesta
179
              Añadir encuesta
193
            </Button>
180
            </Button>
194
          </div>
181
          </div>
195
        </form>
182
        </form>
Línea 257... Línea 244...
257
      )}
244
      )}
258
    </>
245
    </>
259
  )
246
  )
260
}
247
}
Línea -... Línea 248...
-
 
248
 
-
 
249
const mapStateToProps = (state) => ({
261
 
250
  isOpen: state.shareModal.isOpen,
-
 
251
  postUrl: state.shareModal.postUrl,
-
 
252
  modalType: state.shareModal.modalType,
-
 
253
  feedSharedId: state.shareModal.feedSharedId,
-
 
254
  currentPage: state.feed.currentPage,
-
 
255
  timelineUrl: state.feed.timelineUrl
-
 
256
})
-
 
257
 
-
 
258
const mapDispatchToProps = {
-
 
259
  addNotification: (notification) => addNotification(notification),
-
 
260
  closeShareModal: () => closeShareModal(),
-
 
261
  setModalType: (modalType) => setModalType(modalType),
-
 
262
  addFeed: (feed, feedSharedId) => addFeed(feed, feedSharedId)
-
 
263
}
-
 
264