Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3173 | Rev 3175 | 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
    getValues,
45
    clearErrors,
46
    setError,
2807 stevensc 47
    watch
1231 stevensc 48
  } = useForm({
49
    defaultValues: {
50
      description: '',
51
      share_width: 'p'
52
    }
53
  })
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) => {
3174 stevensc 73
    const form = new FormData()
74
    form.append(
75
      'posted_or_shared',
76
      modalType === shareModalTypes.SHARE ? 's' : 'p'
1979 stevensc 77
    )
3174 stevensc 78
    Object.entries(data).forEach(([key, value]) => form.append(key, value))
1979 stevensc 79
 
1231 stevensc 80
    axios
3174 stevensc 81
      .post(postUrl, form)
1231 stevensc 82
      .then((response) => {
83
        const { data, success } = response.data
84
 
1765 stevensc 85
        if (!success && typeof data !== 'string') {
86
          Object.entries(data).map(([key, value]) =>
87
            setError(key, { type: 'required', message: value[0] })
88
          )
1231 stevensc 89
          return
90
        }
91
 
1765 stevensc 92
        if (!success && typeof data === 'string') {
93
          throw new Error(data)
94
        }
95
 
1231 stevensc 96
        const newFeed = data
97
 
2807 stevensc 98
        dispatch(
99
          addNotification({
100
            style: 'success',
101
            msg: 'La publicación ha sido compartida'
102
          })
103
        )
2250 stevensc 104
 
1611 stevensc 105
        onClose()
1231 stevensc 106
 
107
        if (feedSharedId) {
2807 stevensc 108
          dispatch(addFeed(newFeed, feedSharedId))
1231 stevensc 109
        } else {
2807 stevensc 110
          dispatch(addFeed(newFeed))
1231 stevensc 111
        }
2250 stevensc 112
 
113
        setValue('description', '')
114
        setValue('file', '')
1231 stevensc 115
      })
1765 stevensc 116
      .catch((error) => {
2807 stevensc 117
        console.error(error)
118
        dispatch(
119
          addNotification({
120
            style: 'danger',
121
            msg: 'Ha ocurrido un error al publicar, intente más tarde.'
122
          })
123
        )
1765 stevensc 124
      })
1609 stevensc 125
  })
1231 stevensc 126
 
127
  const onUploadedHandler = (files) => {
128
    setValue('file', files)
129
    clearErrors('file')
130
  }
131
 
1609 stevensc 132
  const onClose = () => {
133
    clearErrors()
2807 stevensc 134
    dispatch(closeShareModal())
1609 stevensc 135
  }
136
 
137
  useEffect(() => {
1231 stevensc 138
    if (
139
      modalType !== shareModalTypes.POST &&
140
      modalType !== shareModalTypes.SHARE
141
    ) {
1609 stevensc 142
      register('file', {
143
        required: { value: 'true', message: 'El campo es requerido' }
144
      })
145
    } else {
146
      if (!getValues('file')) unregister('file')
1231 stevensc 147
    }
148
 
1609 stevensc 149
    if (getValues('file') || getValues('description')) {
150
      if (modalType !== lastModalType) {
151
        onClose()
152
        toggleConfirmModal()
153
      }
1231 stevensc 154
    }
1609 stevensc 155
  }, [modalType])
1231 stevensc 156
 
157
  return (
158
    <>
159
      <Modal
1402 stevensc 160
        show={isOpen}
1231 stevensc 161
        title={labels.share_a_post}
162
        labelAccept={labels.send}
163
        labelReject={labels.cancel}
1609 stevensc 164
        onClose={onClose}
3173 stevensc 165
        loading={isSubmitting}
3171 stevensc 166
        formId='share-form'
1231 stevensc 167
      >
3171 stevensc 168
        <Form onSubmit={onSubmit} id='share-form'>
3174 stevensc 169
          {feedType === feedTypes.DASHBOARD && (
3171 stevensc 170
            <Select
171
              name='shared_with'
172
              defaultValue='p'
173
              control={control}
174
              options={[
175
                { name: labels.public, value: 'p' },
176
                { name: labels.connections, value: 'c' }
177
              ]}
178
            />
3174 stevensc 179
          )}
3171 stevensc 180
 
181
          <Ckeditor
3173 stevensc 182
            name='description'
183
            control={control}
3174 stevensc 184
            rules={{ required: 'El campo es requerido' }}
185
            error={errors.description?.message}
2807 stevensc 186
          />
1231 stevensc 187
 
3171 stevensc 188
          {![shareModalTypes.POST, shareModalTypes.SHARE].includes(
189
            modalType
190
          ) ? (
191
            <DropzoneComponent
192
              modalType={modalType}
193
              onUploaded={onUploadedHandler}
194
              settedFile={watch('file')}
195
              recomendationText={recomendationText[modalType] ?? ''}
196
            />
197
          ) : null}
1231 stevensc 198
 
3171 stevensc 199
          {errors.file && (
200
            <FormErrorFeedback>{errors.file.message}</FormErrorFeedback>
201
          )}
202
        </Form>
1231 stevensc 203
      </Modal>
204
      <ConfirmModal
205
        show={showConfirmModal}
206
        onClose={handleModalCancel}
207
        onAccept={handleModalAccept}
208
        message='¿No se ha compartido tu publicación , desea descartarlo?'
209
      />
210
    </>
211
  )
212
}
213
 
2807 stevensc 214
export default ShareModal