Skip to content

Commit

Permalink
[ADD] fetch post editorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanner Enrique De La Hoz Barraza committed Apr 30, 2023
1 parent 0b249c5 commit 7721c24
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 75 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"react": "^18.2.0",
"react-bootstrap": "^2.7.4",
"react-dom": "^18.2.0",
"react-router-dom": "^6.11.0"
"react-router-dom": "^6.11.0",
"sweetalert2": "^11.7.3"
},
"devDependencies": {
"@types/react": "^18.0.28",
Expand Down
31 changes: 0 additions & 31 deletions src/components/API/EditorialAPI.jsx

This file was deleted.

82 changes: 76 additions & 6 deletions src/components/Editorial/Editorial.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
import React from "react";
import React, {useState} from "react";
import Swal from "sweetalert2";

import Error from "../Handle/Error";
import Loading from "../Handle/Loading";
import {getEditorials} from "../API/EditorialAPI";
import Error from "../../utils/Error";
import Loading from "../../utils/Loading";
import {useFetch} from "../../utils/useFetch";
import Modal from "../Modal/Modal";

export default function Editorial() {
const {data, loading, error} = getEditorials();
let url = `http://localhost:4444/api/v1/book-reserve/editorials`;
const {data, loading, error} = useFetch(url);

const [editorialId, setEditorialId] = useState(null);
const [editorialName, setEditorialName] = useState("");
const [editorialDescription, setEditorialDescription] = useState("");

function addEditorial(e) {
e.preventDefault();
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: editorialName,
description: editorialDescription,
}),
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.log(error.message))
.finally(() =>
Swal.fire({
icon: "success",
title: "Editorial agregado",
text: `${editorialName}`,
showConfirmButton: false,
timer: 1500,
})
);
}

return (
<>
{/* start-toolbar */}
<div className="row">
<div className="col-12 mb-4 text-center">
<button className="btn btn-dark">
<button className="btn btn-dark" data-bs-toggle="modal" data-bs-target="#addEditorial">
<i className="fa-solid fa-circle-plus"></i> Añadir
</button>
</div>
Expand Down Expand Up @@ -41,6 +75,42 @@ export default function Editorial() {
))}
</div>
{/* end-content */}
{/* start-modal */}
<Modal
id="addEditorial"
title="Agregar editorial"
textbtn="Agregar"
submit={addEditorial}
body={
<>
<div className="mb-3">
<label className="form-label">Nombre</label>
<input
type="text"
className="form-control"
placeholder="Nombre"
required
onChange={(e) => {
setEditorialName(e.target.value);
}}
/>
</div>
<div className="mb-3">
<label className="form-label">Descripción</label>
<input
type="text"
className="form-control"
placeholder="Descripción"
required
onChange={(e) => {
setEditorialDescription(e.target.value);
}}
/>
</div>
</>
}
/>
{/* end-modal */}
</>
);
}
7 changes: 3 additions & 4 deletions src/components/Home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from "react";
import {useFetch} from "../../useFetch";

import Error from "../Handle/Error";
import Loading from "../Handle/Loading";
import Error from "../../utils/Error";
import Loading from "../../utils/Loading";
import {useFetch} from "../../utils/useFetch";

export default function Home() {
const {data, loading, error} = useFetch("https://rickandmortyapi.com/api/character");
Expand Down
4 changes: 2 additions & 2 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default function Modal({id, title, textbtn, body, submit}) {
<form onSubmit={submit}>
<div className="modal-body">{body}</div>
<div className="modal-footer">
<button type="button" className="btn secondary btn-sm px-4" data-bs-dismiss="modal">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">
Cerrar
</button>
<button type="submit" className="btn primary btn-sm px-4">
<button type="submit" className="btn btn-primary">
{textbtn}
</button>
</div>
Expand Down
31 changes: 0 additions & 31 deletions src/interfaces/api.interface.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7721c24

Please sign in to comment.