Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
6547 stevensc 1
import axios from 'axios'
2
import React, { useState } from 'react'
3
import { Modal, Button } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
5
 
6
const CompanyImgModal = ({
7
    isOpen,
8
    closeModal,
9
    imageUploadUrl,
10
    action
11
}) => {
12
 
13
    const { handleSubmit, register } = useForm()
14
    const [error, setError] = useState(null);
15
 
16
    const onSubmit = ({ image }) => {
17
        const data = new FormData()
18
        data.append("image", image[0])
19
        axios.post(imageUploadUrl, data)
20
            .then(({ data }) => {
21
                if (!data.success) {
22
                    return setError(data.data)
23
                }
24
                action(data.data)
25
                setError(null)
26
                closeModal()
27
            })
28
            .catch((err) => console.log(err))
29
    }
30
 
31
    return (
32
        <Modal
33
            size="md"
34
            show={isOpen}
35
            onHide={closeModal}
36
            autoFocus={false}
37
        >
38
            <Modal.Header closeButton>
39
                <Modal.Title>Cambiar</Modal.Title>
40
            </Modal.Header>
41
            <form onSubmit={handleSubmit(onSubmit)}>
42
                <Modal.Body>
43
                    <div className="mb-3">
44
                        <label className="form-label">Imagen recomendada de (180 x 180px)</label>
45
                        <input
46
                            className="form-control form-control-sm"
47
                            id="formFileSm"
48
                            type="file"
49
                            name="image"
50
                            accept='.jpg, .jpeg, .png, .gif'
51
                            ref={register}
52
                            style={{ padding: '1px' }}
53
                        />
54
                    </div>
55
                    {error && <p>{error}</p>}
56
                </Modal.Body>
57
                <Modal.Footer>
58
                    <Button
59
                        variant="primary"
60
                        type="submit"
61
                    >
62
                        Enviar
63
                    </Button>
64
                    <Button
65
                        variant="default"
66
                        onClick={closeModal}
67
                    >
68
                        Cancelar
69
                    </Button>
70
                </Modal.Footer>
71
            </form>
72
        </Modal >
73
    )
74
}
75
 
76
export default CompanyImgModal