Rev 3180 | Rev 3183 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React from 'react'import { Controller, useForm } from 'react-hook-form'import { useDispatch, useSelector } from 'react-redux'import { axios } from '@utils'import { useShareModal } from '@hooks'import { addFeed } from '@store/feed/feed.actions'import { feedTypes } from '@store/feed/feed.types'import { addNotification } from '@store/notification/notification.actions'import { shareModalTypes } from '@store/share-modal/shareModal.types'import Modal from '@components/UI/modal/Modal'import Select from '@components/UI/inputs/Select'import Ckeditor from '@components/UI/Ckeditor'import DropzoneComponent from '@components/dropzone/DropzoneComponent'import FormErrorFeedback from '@components/UI/form/FormErrorFeedback'import ConfirmModal from './ConfirmModal'const recomendationText = {[shareModalTypes.IMAGE]: 'Tamaño recomendado: 720x720',[shareModalTypes.FILE]: 'solo documentos PDF',[shareModalTypes.VIDEO]: 'Video de extensión mp4, mpeg, webm, mov'}const ShareModal = () => {const labels = useSelector(({ intl }) => intl.labels)const dispatch = useDispatch()const {show,showConfirm,postUrl,modalType,feedType,feedSharedId,onConfirm,onReject,close} = useShareModal()const {control,formState: { errors, isSubmitting },handleSubmit,reset} = useForm({defaultValues: {posted_or_shared: modalType === shareModalTypes.SHARE ? 's' : 'p',share_width: 'p',description: ''}})const onSubmit = handleSubmit(async (feed) => {try {const form = new FormData()Object.entries(feed).forEach(([key, value]) => form.append(key, value))const response = await axios.post(postUrl, form)const { data, success } = response.dataif (!success) {const errorMessage =typeof data === 'string'? data: 'Ha ocurrido un error al publicar, inténtalo de nuevo más tarde.'throw new Error(errorMessage)}const newFeed = datadispatch(addNotification({style: 'success',msg: 'La publicación ha sido compartida'}))dispatch(addFeed(newFeed, feedSharedId))reset()close()} catch (error) {console.error(error)dispatch(addNotification({ style: 'danger', msg: error.message }))}})return (<><Modalshow={show}title={labels.share_a_post}labelAccept={labels.send}labelReject={labels.cancel}loading={isSubmitting}onAccept={onSubmit}onClose={close}>{feedType === feedTypes.DASHBOARD && (<Selectname='shared_with'defaultValue='p'control={control}options={[{ name: labels.public, value: 'p' },{ name: labels.connections, value: 'c' }]}/>)}<Ckeditorname='description'control={control}rules={{ required: 'La descripción es requerida' }}error={errors.description?.message}/>{![shareModalTypes.POST, shareModalTypes.SHARE].includes(modalType) && (<Controllername='file'control={control}rules={{ required: 'El archivo es requerido' }}render={({ field: { value, onChange } }) => (<DropzoneComponentmodalType={modalType}onUploaded={(file) => onChange(file)}settedFile={value}recomendationText={recomendationText[modalType] ?? ''}/>)}/>)}{errors.file && (<FormErrorFeedback>{errors.file.message}</FormErrorFeedback>)}</Modal><ConfirmModalshow={showConfirm}onClose={onReject}onAccept={onConfirm}message='¿No se ha compartido tu publicación , desea descartarlo?'/></>)}export default ShareModal