Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1607 stevensc 1
import React, { useEffect, useState } from 'react'
1231 stevensc 2
import { useForm } from 'react-hook-form'
2807 stevensc 3
import { useDispatch, useSelector } from 'react-redux'
1231 stevensc 4
 
2521 stevensc 5
import { axios } from '@app/utils'
2807 stevensc 6
import { feedTypes } from '@app/redux/feed/feed.types'
1231 stevensc 7
import {
8
  closeShareModal,
2807 stevensc 9
  openShareModal
1607 stevensc 10
} from '@app/redux/share-modal/shareModal.actions'
2807 stevensc 11
import { addNotification } from '@app/redux/notification/notification.actions'
1650 stevensc 12
import { addFeed } from '@app/redux/feed/feed.actions'
1607 stevensc 13
import { shareModalTypes } from '@app/redux/share-modal/shareModal.types'
1231 stevensc 14
 
2807 stevensc 15
import Ckeditor from '@components/UI/Ckeditor'
16
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'
17
import Modal from '@components/UI/modal/Modal'
18
import DropzoneComponent from '@components/dropzone/DropzoneComponent'
1607 stevensc 19
import ConfirmModal from './ConfirmModal'
2807 stevensc 20
import Select from '@components/UI/inputs/Select'
1231 stevensc 21
 
2807 stevensc 22
const recomendationText = {
23
  [shareModalTypes.IMAGE]: 'Tamaño recomendado: 720x720',
24
  [shareModalTypes.FILE]: 'solo documentos PDF',
25
  [shareModalTypes.VIDEO]: 'Video de extensión mp4, mpeg, webm, mov'
26
}
27
 
28
const ShareModal = ({ setModalType }) => {
1231 stevensc 29
  const [showConfirmModal, setShowConfirmModal] = useState(false)
30
  const labels = useSelector(({ intl }) => intl.labels)
2807 stevensc 31
  const { isOpen, postUrl, modalType, lastModalType, feedType, feedSharedId } =
32
    useSelector((state) => state.shareModal)
33
  const dispatch = useDispatch()
1231 stevensc 34
 
35
  const {
2807 stevensc 36
    control,
3166 stevensc 37
    formState: { errors, isSubmitting },
1231 stevensc 38
    register,
39
    unregister,
40
    handleSubmit,
41
    setValue,
42
    getValues,
43
    clearErrors,
44
    setError,
2807 stevensc 45
    watch
1231 stevensc 46
  } = useForm({
47
    defaultValues: {
48
      description: '',
49
      share_width: 'p'
50
    }
51
  })
2521 stevensc 52
  const watchedDescription = watch('description')
1231 stevensc 53
 
1608 stevensc 54
  const toggleConfirmModal = () => setShowConfirmModal(!showConfirmModal)
1231 stevensc 55
 
56
  const handleModalAccept = () => {
57
    setShowConfirmModal(false)
58
    setValue('description', '')
59
    setValue('file', '')
60
    clearErrors()
2807 stevensc 61
    dispatch(openShareModal(postUrl, modalType, feedType))
1231 stevensc 62
  }
63
 
64
  const handleModalCancel = () => {
65
    setShowConfirmModal(false)
66
    setModalType(lastModalType)
2807 stevensc 67
    dispatch(closeShareModal())
68
    dispatch(openShareModal(postUrl, lastModalType, feedType))
1231 stevensc 69
  }
70
 
1609 stevensc 71
  const onSubmit = handleSubmit((data) => {
1979 stevensc 72
    const currentFormData = new FormData()
73
 
74
    Object.entries(data).forEach(([key, value]) =>
75
      currentFormData.append(key, value)
76
    )
77
 
1231 stevensc 78
    axios
1979 stevensc 79
      .post(postUrl, currentFormData)
1231 stevensc 80
      .then((response) => {
81
        const { data, success } = response.data
82
 
1765 stevensc 83
        if (!success && typeof data !== 'string') {
84
          Object.entries(data).map(([key, value]) =>
85
            setError(key, { type: 'required', message: value[0] })
86
          )
1231 stevensc 87
          return
88
        }
89
 
1765 stevensc 90
        if (!success && typeof data === 'string') {
91
          throw new Error(data)
92
        }
93
 
1231 stevensc 94
        const newFeed = data
95
 
2807 stevensc 96
        dispatch(
97
          addNotification({
98
            style: 'success',
99
            msg: 'La publicación ha sido compartida'
100
          })
101
        )
2250 stevensc 102
 
1611 stevensc 103
        onClose()
1231 stevensc 104
 
105
        if (feedSharedId) {
2807 stevensc 106
          dispatch(addFeed(newFeed, feedSharedId))
1231 stevensc 107
        } else {
2807 stevensc 108
          dispatch(addFeed(newFeed))
1231 stevensc 109
        }
2250 stevensc 110
 
111
        setValue('description', '')
112
        setValue('file', '')
1231 stevensc 113
      })
1765 stevensc 114
      .catch((error) => {
2807 stevensc 115
        console.error(error)
116
        dispatch(
117
          addNotification({
118
            style: 'danger',
119
            msg: 'Ha ocurrido un error al publicar, intente más tarde.'
120
          })
121
        )
1765 stevensc 122
      })
1609 stevensc 123
  })
1231 stevensc 124
 
125
  const onUploadedHandler = (files) => {
126
    setValue('file', files)
127
    clearErrors('file')
128
  }
129
 
1609 stevensc 130
  const onClose = () => {
131
    clearErrors()
2807 stevensc 132
    dispatch(closeShareModal())
1609 stevensc 133
  }
134
 
135
  useEffect(() => {
136
    const postedOrShared = modalType === shareModalTypes.SHARE ? 's' : 'p'
137
 
138
    register('posted_or_shared')
139
    setValue('posted_or_shared', postedOrShared)
140
 
1231 stevensc 141
    if (
142
      modalType !== shareModalTypes.POST &&
143
      modalType !== shareModalTypes.SHARE
144
    ) {
1609 stevensc 145
      register('file', {
146
        required: { value: 'true', message: 'El campo es requerido' }
147
      })
148
    } else {
149
      if (!getValues('file')) unregister('file')
1231 stevensc 150
    }
151
 
1609 stevensc 152
    if (getValues('file') || getValues('description')) {
153
      if (modalType !== lastModalType) {
154
        onClose()
155
        toggleConfirmModal()
156
      }
1231 stevensc 157
    }
1609 stevensc 158
  }, [modalType])
1231 stevensc 159
 
1609 stevensc 160
  useEffect(() => {
1611 stevensc 161
    if (isOpen) {
162
      register('description', { required: 'El campo es requerido' })
163
    }
164
  }, [isOpen])
1609 stevensc 165
 
1231 stevensc 166
  return (
167
    <>
168
      <Modal
1402 stevensc 169
        show={isOpen}
1231 stevensc 170
        title={labels.share_a_post}
171
        labelAccept={labels.send}
172
        labelReject={labels.cancel}
1609 stevensc 173
        onClose={onClose}
174
        onAccept={onSubmit}
3168 stevensc 175
        loading={true}
1231 stevensc 176
      >
1609 stevensc 177
        {feedType === feedTypes.DASHBOARD ? (
2807 stevensc 178
          <Select
1610 stevensc 179
            name='shared_with'
180
            defaultValue='p'
2807 stevensc 181
            control={control}
182
            options={[
183
              { name: labels.public, value: 'p' },
184
              { name: labels.connections, value: 'c' }
185
            ]}
186
          />
1609 stevensc 187
        ) : null}
1231 stevensc 188
 
2807 stevensc 189
        <Ckeditor
2521 stevensc 190
          defaultValue={watchedDescription}
191
          onChange={(value) => setValue('description', value)}
1231 stevensc 192
        />
193
 
194
        {errors.description && (
195
          <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
196
        )}
197
 
1609 stevensc 198
        {![shareModalTypes.POST, shareModalTypes.SHARE].includes(modalType) ? (
199
          <DropzoneComponent
200
            modalType={modalType}
201
            onUploaded={onUploadedHandler}
1931 stevensc 202
            settedFile={watch('file')}
2807 stevensc 203
            recomendationText={recomendationText[modalType] ?? ''}
1609 stevensc 204
          />
205
        ) : null}
1231 stevensc 206
 
207
        {errors.file && (
208
          <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>
209
        )}
210
      </Modal>
211
      <ConfirmModal
212
        show={showConfirmModal}
213
        onClose={handleModalCancel}
214
        onAccept={handleModalAccept}
215
        message='¿No se ha compartido tu publicación , desea descartarlo?'
216
      />
217
    </>
218
  )
219
}
220
 
2807 stevensc 221
export default ShareModal