|
@@ -1,27 +1,124 @@
|
1
|
1
|
import React from "react";
|
|
2
|
+import axios from "axios";
|
|
3
|
+import { useState, useEffect } from "react";
|
|
4
|
+import { useNavigate} from "react-router-dom";
|
2
|
5
|
import './Noticias.css';
|
3
|
6
|
|
4
|
|
-const Noticias = () => {
|
|
7
|
+//Al momento la base de datos esta siendo accedida de manera local y esta en processo a ser accedido desde el servidor
|
|
8
|
+
|
|
9
|
+export default function Noticias(){
|
|
10
|
+ const navigate = useNavigate();
|
|
11
|
+
|
|
12
|
+ const [inputs, setInputs] = useState([]);
|
|
13
|
+
|
|
14
|
+ //Esta funcion manejea los cambio que uno hace en cuanto a la informacion
|
|
15
|
+ const handleChange = (event) => {
|
|
16
|
+ const name = event.target.name;
|
|
17
|
+ const value = event.target.value;
|
|
18
|
+ setInputs(values => ({...values, [name]: value}));
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ //Esta funcion maneja los cambios al uno presionar el boton y lo sube a la base de datos
|
|
22
|
+ const handleSubmit = (event) => {
|
|
23
|
+ event.preventDefault();
|
|
24
|
+
|
|
25
|
+ axios.post('http://localhost/apicenso/user/save', inputs).then(function(response){
|
|
26
|
+ //axios.post('https://ada.uprrp.edu/~luis.ortiz79/api/user/save', inputs).then(function(response){
|
|
27
|
+ console.log(response.data);
|
|
28
|
+ navigate('/');
|
|
29
|
+ });
|
|
30
|
+
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ const [users, setUsers] = useState([]);
|
|
34
|
+ useEffect(() => {
|
|
35
|
+ getUsers();
|
|
36
|
+ }, []);
|
|
37
|
+
|
|
38
|
+ //Esta funcion busca el id de la noticia selecionada en la base de datos
|
|
39
|
+ function getUsers() {
|
|
40
|
+ axios.get('http://localhost/apicenso/users/').then(function(response) {
|
|
41
|
+ //axios.get('https://ada.uprrp.edu/~luis.ortiz79/api/users/').then(function(response) {
|
|
42
|
+ console.log(response.data);
|
|
43
|
+ setUsers(response.data);
|
|
44
|
+ });
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+ //Esta funcion envie el id a la base de datos para ser borrada
|
|
49
|
+ const deleteUser = (id) => {
|
|
50
|
+ axios.delete(`http://localhost/apicenso/user/${id}/delete`).then(function(response){
|
|
51
|
+ //axios.delete(`https://ada.uprrp.edu/~luis.ortiz79/api/user/${id}/delete`).then(function(response){
|
|
52
|
+ console.log(response.data);
|
|
53
|
+ getUsers();
|
|
54
|
+ });
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ //Crea el pop up window para asegura de que si el usuario quiere borrar la noticia o no
|
|
58
|
+ const deleteConfirm = (id) => {
|
|
59
|
+ if (window.confirm("Estas seguro que lo quieres borrar?")) {
|
|
60
|
+ deleteUser(id);
|
|
61
|
+ }
|
|
62
|
+ };
|
|
63
|
+
|
5
|
64
|
return (
|
|
65
|
+ //En la primera tabla es donde el usuario puede subir la noticia en la base de datos
|
|
66
|
+ //En la segunda tabla es donde le da la lista de lo que contiene la base de datos y la opcion de poder borrar esa noticia
|
6
|
67
|
<div className="Noticias">
|
7
|
68
|
|
8
|
69
|
<h1>Noticias</h1>
|
9
|
70
|
|
10
|
|
- <form>
|
11
|
|
- <label>Title:</label>
|
12
|
|
- <input type="text" id="uname" name="Title" placeholder="Title for the news.." />
|
|
71
|
+ <form onSubmit={handleSubmit}>
|
|
72
|
+ <table cellSpacing="10">
|
|
73
|
+ <tbody>
|
|
74
|
+ <tr>
|
|
75
|
+ <th>
|
|
76
|
+ <label>Title:</label>
|
|
77
|
+ </th>
|
|
78
|
+ <td>
|
|
79
|
+ <input type="text" name="title" placeholder="Title for the news.." onChange={handleChange} />
|
|
80
|
+ </td>
|
|
81
|
+ </tr>
|
|
82
|
+ <tr>
|
|
83
|
+ <th>
|
|
84
|
+ <label>Subject:</label>
|
|
85
|
+ </th>
|
|
86
|
+ <td>
|
|
87
|
+ <textarea name="subject" placeholder="Write something.." onChange={handleChange} ></textarea>
|
|
88
|
+ </td>
|
|
89
|
+ </tr>
|
|
90
|
+ <tr>
|
|
91
|
+ <td colSpan="2" align ="right">
|
|
92
|
+ <input type="submit" value="Submit" />
|
|
93
|
+ </td>
|
|
94
|
+ </tr>
|
|
95
|
+ </tbody>
|
|
96
|
+ </table>
|
13
|
97
|
</form>
|
14
|
98
|
|
15
|
|
- <form>
|
16
|
|
- <label>Subject:</label>
|
17
|
|
- <textarea id="subject" name="subject" placeholder="Write something.."></textarea>
|
18
|
|
- </form>
|
19
|
|
-
|
20
|
|
- <form>
|
21
|
|
- <input type="submit" value="Submit" />
|
22
|
|
- </form>
|
|
99
|
+ <table>
|
|
100
|
+ <thead>
|
|
101
|
+ <tr>
|
|
102
|
+ <th>#</th>
|
|
103
|
+ <th>Title</th>
|
|
104
|
+ <th>Subject</th>
|
|
105
|
+ <th>Actions</th>
|
|
106
|
+ </tr>
|
|
107
|
+ </thead>
|
|
108
|
+ <tbody>
|
|
109
|
+ {users.map((user, key) =>
|
|
110
|
+ <tr key={key}>
|
|
111
|
+ <td>{user.id}</td>
|
|
112
|
+ <td>{user.title}</td>
|
|
113
|
+ <td>{user.subject}</td>
|
|
114
|
+ <td>
|
|
115
|
+ <input type="submit" value="Delete" onClick={() => deleteConfirm(user.id)} />
|
|
116
|
+ </td>
|
|
117
|
+ </tr>
|
|
118
|
+ )}
|
|
119
|
+
|
|
120
|
+ </tbody>
|
|
121
|
+ </table>
|
23
|
122
|
</div>
|
24
|
123
|
);
|
25
|
|
-};
|
26
|
|
-
|
27
|
|
-export default Noticias;
|
|
124
|
+}
|