Proyectos de Subversion LeadersLinked - SPA

Rev

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