Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3183 stevensc 1
import React, { useState } from 'react'
3181 stevensc 2
import { Controller, useForm } from 'react-hook-form'
2807 stevensc 3
import { useDispatch, useSelector } from 'react-redux'
1231 stevensc 4
 
3281 stevensc 5
import { axios } from '@utils'
3181 stevensc 6
import { useShareModal } from '@hooks'
3174 stevensc 7
import { feedTypes } from '@store/feed/feed.types'
3281 stevensc 8
import { addFeed, addFeedSuccess } from '@store/feed/feed.actions'
9
import { shareModalTypes } from '@store/share-modal/shareModal.types'
3174 stevensc 10
import { addNotification } from '@store/notification/notification.actions'
1231 stevensc 11
 
3281 stevensc 12
import Form from '@components/common/form'
3174 stevensc 13
import Modal from '@components/UI/modal/Modal'
14
import Select from '@components/UI/inputs/Select'
3269 stevensc 15
import Ckeditor from '@components/common/ckeditor/Ckeditor'
3174 stevensc 16
import DropzoneComponent from '@components/dropzone/DropzoneComponent'
2807 stevensc 17
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'
1607 stevensc 18
import ConfirmModal from './ConfirmModal'
1231 stevensc 19
 
2807 stevensc 20
const recomendationText = {
21
  [shareModalTypes.IMAGE]: 'Tamaño recomendado: 720x720',
22
  [shareModalTypes.FILE]: 'solo documentos PDF',
23
  [shareModalTypes.VIDEO]: 'Video de extensión mp4, mpeg, webm, mov'
24
}
25
 
3181 stevensc 26
const ShareModal = () => {
3183 stevensc 27
  const [showConfirm, setShowConfirm] = useState(false)
1231 stevensc 28
  const labels = useSelector(({ intl }) => intl.labels)
3174 stevensc 29
  const dispatch = useDispatch()
30
 
3184 stevensc 31
  const {
32
    show,
33
    modalType,
34
    feedType,
35
    feedSharedId,
36
    postUrl,
37
    closeModal,
38
    openModal
39
  } = useShareModal()
1231 stevensc 40
 
41
  const {
2807 stevensc 42
    control,
3173 stevensc 43
    formState: { errors, isSubmitting },
1231 stevensc 44
    handleSubmit,
3183 stevensc 45
    reset,
46
    watch
1231 stevensc 47
  } = useForm({
3181 stevensc 48
    defaultValues: {
49
      posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',
50
      share_width: 'p',
51
      description: ''
52
    }
1231 stevensc 53
  })
3183 stevensc 54
  const description = watch('description')
55
  const file = watch('file')
1231 stevensc 56
 
3183 stevensc 57
  const toggleConfirm = () => setShowConfirm(!showConfirm)
58
 
3181 stevensc 59
  const onSubmit = handleSubmit(async (feed) => {
3176 stevensc 60
    try {
3281 stevensc 61
      const form = new FormData()
62
      Object.entries(feed).forEach(([key, value]) => form.append(key, value))
1979 stevensc 63
 
3281 stevensc 64
      const response = await axios.post(postUrl, form)
65
      const { data, success } = response.data
66
 
67
      if (!success) {
68
        const errorMessage =
69
          typeof data === 'string'
70
            ? data
71
            : 'Ha ocurrido un error al publicar, inténtalo de nuevo más tarde.'
72
        throw new Error(errorMessage)
73
      }
74
 
75
      const newFeed = data
76
 
3176 stevensc 77
      dispatch(
78
        addNotification({
79
          style: 'success',
3281 stevensc 80
          msg: 'La publicación ha sido compartida'
3176 stevensc 81
        })
82
      )
3281 stevensc 83
      dispatch(addFeedSuccess(newFeed, feedSharedId))
84
 
3181 stevensc 85
      reset()
3183 stevensc 86
      closeModal()
3176 stevensc 87
    } catch (error) {
88
      dispatch(addNotification({ style: 'danger', msg: error.message }))
89
    }
3181 stevensc 90
  })
1231 stevensc 91
 
3183 stevensc 92
  const handleClose = () => {
3184 stevensc 93
    if (description || file) toggleConfirm()
3183 stevensc 94
    closeModal()
95
  }
96
 
97
  const handleConfirm = () => {
98
    reset()
3184 stevensc 99
    toggleConfirm()
3183 stevensc 100
    closeModal()
101
  }
102
 
3184 stevensc 103
  const handleCancel = () => {
104
    toggleConfirm()
105
    openModal({ url: postUrl, modalType, feedType })
106
  }
107
 
1231 stevensc 108
  return (
109
    <>
110
      <Modal
3181 stevensc 111
        show={show}
1231 stevensc 112
        title={labels.share_a_post}
113
        labelAccept={labels.send}
114
        labelReject={labels.cancel}
3175 stevensc 115
        loading={isSubmitting}
3183 stevensc 116
        onClose={handleClose}
3279 stevensc 117
        formId='share_modal-form'
1231 stevensc 118
      >
3279 stevensc 119
        <Form onSubmit={onSubmit} id='share_modal-form'>
120
          {feedType === feedTypes.DASHBOARD && (
121
            <Select
122
              name='shared_with'
123
              defaultValue='p'
124
              control={control}
125
              options={[
126
                { name: labels.public, value: 'p' },
127
                { name: labels.connections, value: 'c' }
128
              ]}
129
            />
130
          )}
1231 stevensc 131
 
3279 stevensc 132
          <Ckeditor
133
            name='description'
3181 stevensc 134
            control={control}
3279 stevensc 135
            rules={{ required: 'La descripción es requerida' }}
136
            error={errors.description?.message}
3178 stevensc 137
          />
138
 
3279 stevensc 139
          {![shareModalTypes.POST, shareModalTypes.SHARE].includes(
140
            modalType
141
          ) && (
142
            <Controller
143
              name='file'
144
              control={control}
145
              rules={{ required: 'El archivo es requerido' }}
146
              render={({
147
                field: { value, onChange },
148
                fieldState: { error }
149
              }) => (
150
                <>
151
                  <DropzoneComponent
152
                    type={modalType}
153
                    onUploaded={onChange}
154
                    settedFile={value}
155
                    recomendationText={recomendationText[modalType] ?? ''}
156
                  />
157
                  {error && (
158
                    <FormErrorFeedback>{error.message}</FormErrorFeedback>
159
                  )}
160
                </>
161
              )}
162
            />
163
          )}
164
        </Form>
1231 stevensc 165
      </Modal>
166
      <ConfirmModal
3181 stevensc 167
        show={showConfirm}
3183 stevensc 168
        onAccept={handleConfirm}
3184 stevensc 169
        onClose={handleCancel}
3183 stevensc 170
        message='¿Estás seguro de que quieres salir sin publicar tu post?'
1231 stevensc 171
      />
172
    </>
173
  )
174
}
175
 
2807 stevensc 176
export default ShareModal