Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3176 | Rev 3178 | 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
 
3174 stevensc 5
import { axios } from '@utils'
6
import { addFeed } from '@store/feed/feed.actions'
7
import { feedTypes } from '@store/feed/feed.types'
8
import { addNotification } from '@store/notification/notification.actions'
9
import { shareModalTypes } from '@store/share-modal/shareModal.types'
1231 stevensc 10
import {
11
  closeShareModal,
2807 stevensc 12
  openShareModal
3174 stevensc 13
} from '@store/share-modal/shareModal.actions'
1231 stevensc 14
 
3174 stevensc 15
import Modal from '@components/UI/modal/Modal'
16
import Form from '@components/common/form'
17
import Select from '@components/UI/inputs/Select'
2807 stevensc 18
import Ckeditor from '@components/UI/Ckeditor'
3174 stevensc 19
import DropzoneComponent from '@components/dropzone/DropzoneComponent'
2807 stevensc 20
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'
1607 stevensc 21
import ConfirmModal from './ConfirmModal'
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)
3174 stevensc 32
  const dispatch = useDispatch()
33
 
2807 stevensc 34
  const { isOpen, postUrl, modalType, lastModalType, feedType, feedSharedId } =
35
    useSelector((state) => state.shareModal)
1231 stevensc 36
 
37
  const {
2807 stevensc 38
    control,
3173 stevensc 39
    formState: { errors, isSubmitting },
1231 stevensc 40
    register,
41
    unregister,
42
    handleSubmit,
43
    setValue,
44
    clearErrors,
2807 stevensc 45
    watch
1231 stevensc 46
  } = useForm({
3176 stevensc 47
    defaultValues:
48
      modalType !== shareModalTypes.POST && modalType !== shareModalTypes.SHARE
49
        ? {
50
            posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',
51
            share_width: 'p',
52
            description: '',
53
            file: ''
54
          }
55
        : {
56
            posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',
57
            share_width: 'p',
58
            description: ''
59
          }
1231 stevensc 60
  })
3176 stevensc 61
  const watchedDescription = watch('description')
62
  const watchedFile = watch('file')
1231 stevensc 63
 
1608 stevensc 64
  const toggleConfirmModal = () => setShowConfirmModal(!showConfirmModal)
1231 stevensc 65
 
66
  const handleModalAccept = () => {
67
    setShowConfirmModal(false)
68
    setValue('description', '')
69
    setValue('file', '')
70
    clearErrors()
2807 stevensc 71
    dispatch(openShareModal(postUrl, modalType, feedType))
1231 stevensc 72
  }
73
 
74
  const handleModalCancel = () => {
75
    setShowConfirmModal(false)
76
    setModalType(lastModalType)
2807 stevensc 77
    dispatch(closeShareModal())
78
    dispatch(openShareModal(postUrl, lastModalType, feedType))
1231 stevensc 79
  }
80
 
3175 stevensc 81
  const onUploadedHandler = (files) => {
82
    setValue('file', files)
83
    clearErrors('file')
84
  }
85
 
86
  const onClose = () => {
87
    clearErrors()
88
    dispatch(closeShareModal())
89
  }
90
 
3176 stevensc 91
  const onSubmit = async (feed) => {
92
    try {
93
      const form = new FormData()
94
      Object.entries(feed).forEach(([key, value]) => form.append(key, value))
1979 stevensc 95
 
3176 stevensc 96
      const response = await axios.post(postUrl, form)
97
      const { data, success } = response.data
1231 stevensc 98
 
3176 stevensc 99
      if (!success) {
100
        const errorMessage =
101
          typeof data === 'string'
102
            ? data
103
            : 'Ha ocurrido un error al publicar, inténtalo de nuevo más tarde.'
104
        throw new Error(errorMessage)
105
      }
1231 stevensc 106
 
3176 stevensc 107
      const newFeed = data
1231 stevensc 108
 
3176 stevensc 109
      dispatch(
110
        addNotification({
111
          style: 'success',
112
          msg: 'La publicación ha sido compartida'
113
        })
114
      )
2250 stevensc 115
 
3176 stevensc 116
      onClose()
1231 stevensc 117
 
3176 stevensc 118
      if (feedSharedId) {
119
        dispatch(addFeed(newFeed, feedSharedId))
120
      } else {
121
        dispatch(addFeed(newFeed))
122
      }
2250 stevensc 123
 
3176 stevensc 124
      setValue('description', '')
125
      setValue('file', '')
126
    } catch (error) {
127
      console.error(error)
128
      dispatch(addNotification({ style: 'danger', msg: error.message }))
129
    }
1231 stevensc 130
  }
131
 
1609 stevensc 132
  useEffect(() => {
3176 stevensc 133
    if (watchedFile !== undefined) {
3177 stevensc 134
      register('file', { required: 'El archivo es requerido' })
1609 stevensc 135
    } else {
3176 stevensc 136
      unregister('file')
1231 stevensc 137
    }
138
 
3176 stevensc 139
    if ((watchedFile || watchedDescription) && modalType !== lastModalType) {
140
      onClose()
141
      toggleConfirmModal()
1231 stevensc 142
    }
1609 stevensc 143
  }, [modalType])
1231 stevensc 144
 
145
  return (
146
    <>
147
      <Modal
1402 stevensc 148
        show={isOpen}
1231 stevensc 149
        title={labels.share_a_post}
150
        labelAccept={labels.send}
151
        labelReject={labels.cancel}
3175 stevensc 152
        loading={isSubmitting}
1609 stevensc 153
        onClose={onClose}
3171 stevensc 154
        formId='share-form'
1231 stevensc 155
      >
3175 stevensc 156
        <Form onSubmit={handleSubmit(onSubmit)} id='share-form'>
3174 stevensc 157
          {feedType === feedTypes.DASHBOARD && (
3171 stevensc 158
            <Select
159
              name='shared_with'
160
              defaultValue='p'
161
              control={control}
162
              options={[
163
                { name: labels.public, value: 'p' },
164
                { name: labels.connections, value: 'c' }
165
              ]}
166
            />
3174 stevensc 167
          )}
3171 stevensc 168
 
169
          <Ckeditor
3173 stevensc 170
            name='description'
171
            control={control}
3177 stevensc 172
            rules={{ required: 'La descripción es requerida' }}
3174 stevensc 173
            error={errors.description?.message}
2807 stevensc 174
          />
1231 stevensc 175
 
3171 stevensc 176
          {![shareModalTypes.POST, shareModalTypes.SHARE].includes(
177
            modalType
3175 stevensc 178
          ) && (
3171 stevensc 179
            <DropzoneComponent
180
              modalType={modalType}
181
              onUploaded={onUploadedHandler}
182
              settedFile={watch('file')}
183
              recomendationText={recomendationText[modalType] ?? ''}
184
            />
3175 stevensc 185
          )}
1231 stevensc 186
 
3171 stevensc 187
          {errors.file && (
188
            <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>
189
          )}
190
        </Form>
1231 stevensc 191
      </Modal>
192
      <ConfirmModal
193
        show={showConfirmModal}
194
        onClose={handleModalCancel}
195
        onAccept={handleModalAccept}
196
        message='¿No se ha compartido tu publicación , desea descartarlo?'
197
      />
198
    </>
199
  )
200
}
201
 
2807 stevensc 202
export default ShareModal