Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3171 | Rev 3174 | 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,
3173 stevensc 38
    formState: { errors, isSubmitting },
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
 
3173 stevensc 161
  console.log(isSubmitting)
1609 stevensc 162
 
1231 stevensc 163
  return (
164
    <>
165
      <Modal
1402 stevensc 166
        show={isOpen}
1231 stevensc 167
        title={labels.share_a_post}
168
        labelAccept={labels.send}
169
        labelReject={labels.cancel}
1609 stevensc 170
        onClose={onClose}
3173 stevensc 171
        loading={isSubmitting}
3171 stevensc 172
        formId='share-form'
1231 stevensc 173
      >
3171 stevensc 174
        <Form onSubmit={onSubmit} id='share-form'>
175
          {feedType === feedTypes.DASHBOARD ? (
176
            <Select
177
              name='shared_with'
178
              defaultValue='p'
179
              control={control}
180
              options={[
181
                { name: labels.public, value: 'p' },
182
                { name: labels.connections, value: 'c' }
183
              ]}
184
            />
185
          ) : null}
186
 
187
          <Ckeditor
3173 stevensc 188
            name='description'
189
            control={control}
3171 stevensc 190
            defaultValue={watchedDescription}
191
            onChange={(value) => setValue('description', value)}
3173 stevensc 192
            rules={{}}
2807 stevensc 193
          />
1231 stevensc 194
 
3171 stevensc 195
          {errors.description && (
196
            <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
197
          )}
1231 stevensc 198
 
3171 stevensc 199
          {![shareModalTypes.POST, shareModalTypes.SHARE].includes(
200
            modalType
201
          ) ? (
202
            <DropzoneComponent
203
              modalType={modalType}
204
              onUploaded={onUploadedHandler}
205
              settedFile={watch('file')}
206
              recomendationText={recomendationText[modalType] ?? ''}
207
            />
208
          ) : null}
1231 stevensc 209
 
3171 stevensc 210
          {errors.file && (
211
            <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>
212
          )}
213
        </Form>
1231 stevensc 214
      </Modal>
215
      <ConfirmModal
216
        show={showConfirmModal}
217
        onClose={handleModalCancel}
218
        onAccept={handleModalAccept}
219
        message='¿No se ha compartido tu publicación , desea descartarlo?'
220
      />
221
    </>
222
  )
223
}
224
 
2807 stevensc 225
export default ShareModal