Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3697 | | Comparar con el anterior | Ultima modificación | Ver Log |

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