Proyectos de Subversion LeadersLinked - SPA

Rev

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