Proyectos de Subversion LeadersLinked - SPA

Rev

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

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