Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5490 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useState, useEffect } from 'react'
3
import { axios } from '../../utils'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { Button, Modal } from 'react-bootstrap'
7
import { addNotification } from '../../redux/notification/notification.actions'
8
import { coverTypes } from './cover.types'
9
 
10
import Spinner from '../loading-spinner/Spinner'
11
import DropzoneComponent from '../dropzone/DropzoneComponent'
12
import FormErrorFeedback from '../form-error-feedback/FormErrorFeedback'
13
 
14
const CoverModal = ({ isOpen, type, size, id, onClose, onComplete }) => {
15
  const {
16
    register,
17
    errors,
18
    handleSubmit,
19
    setValue,
20
    clearErrors,
21
    setError,
22
    getValues,
23
  } = useForm()
24
  const [loading, setLoading] = useState(false)
25
  const dispatch = useDispatch()
26
 
27
  const postPath = {
28
    [coverTypes.USER]: `/profile/my-profiles/cover/${id}/operation/upload`,
29
    [coverTypes.COMPANY]: `/my-company/${id}/profile/cover/upload`,
30
    [coverTypes.GROUP]: `/group/my-groups/cover/${id}/operation/upload`,
31
  }
32
 
33
  const onUploadedHandler = (files) => {
34
    setValue('cover', files)
35
    clearErrors('cover')
36
  }
37
 
38
  const onSubmitHandler = async (data) => {
39
    setLoading(true)
40
 
41
    const formData = new FormData()
42
    Object.entries(data).map(([key, value]) => formData.append(key, value))
43
 
44
    await axios.post(postPath[type], formData).then((response) => {
45
      const resData = response.data
46
      if (resData.success) {
47
        setLoading(false)
48
        const newCoverImg = {
49
          path: resData.data,
50
          uid: Date.now(),
51
        }
52
        onComplete(newCoverImg)
53
        setValue('cover', '')
54
        onClose()
55
      } else {
56
        const resError = resData.data
57
        if (resError.constructor.name === 'Object') {
58
          Object.entries(resError).map(([key, value]) => {
59
            if (key in getValues()) {
60
              setError(key, {
61
                type: 'manual',
62
                message: Array.isArray(value) ? value[0] : value,
63
              })
64
            }
65
          })
66
        } else {
67
          dispatch(addNotification({ style: 'danger', msg: resError }))
68
        }
69
      }
70
    })
71
    setLoading(false)
72
  }
73
 
74
  useEffect(() => {
75
    register('cover', {
76
      required: { value: 'true', message: 'El campo es requerido' },
77
    })
78
  }, [])
79
 
80
  return (
81
    <Modal show={isOpen} onHide={onClose}>
82
      <form onSubmit={handleSubmit(onSubmitHandler)}>
83
        <Modal.Header closeButton>
84
          <Modal.Title>Portada</Modal.Title>
85
        </Modal.Header>
86
        <Modal.Body>
87
          {loading && <Spinner />}
88
          <DropzoneComponent
89
            modalType="IMAGE"
90
            onUploaded={onUploadedHandler}
91
            recomendationText={`Imágenes recomendadas de ${size}`}
92
          />
93
          {errors.cover && (
94
            <FormErrorFeedback>{errors.cover.message}</FormErrorFeedback>
95
          )}
96
        </Modal.Body>
97
        <Modal.Footer>
98
          <Button size="sm" type="submit">
99
            Enviar
100
          </Button>
101
          <Button size="sm" variant="danger" onClick={onClose}>
102
            Cancelar
103
          </Button>
104
        </Modal.Footer>
105
      </form>
106
    </Modal>
107
  )
108
}
109
 
110
export default CoverModal