Newer
Older
import React, { useState, useContext } from "react";
import { Row, Form, Spinner, Alert } from "react-bootstrap";
import AuthContext from "./AuthContext";
function Sender({ url, method, body, onSuccess, onError, children }) {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const auth = useContext(AuthContext);
if (e) {
e.preventDefault();
}
setIsLoading(true);
const fetchMethod = method ? method : "POST";
const bodyObj = typeof body === "function" ? body() : body;
const bodyJson = JSON.stringify(bodyObj);
fetch(cleanUrl(url), {
headers: { "x-authentication": auth.token },
method: fetchMethod,
body: bodyJson,
if (!response.ok) {
throw new ResponseError(response);
}
if (response.headers.get("content-type") !== "application/json") {
return response.text();
}
return response.json();
})
.catch((e) => {
if (
e instanceof ResponseError &&
<Row className="justify-content-center">
<Spinner animation="border" />
</Row>
let errorStr;
if (error) {
errorStr = (
<Alert variant="danger">
Se produjo un error enviando datos: {error}
</Alert>
);
}
return (
<Form onSubmit={submit}>
{errorStr}
{children}
</Form>
);