Skip to content

Commit cedfdfb

Browse files
authored
Merge pull request #5 from despediteerik/v0.4-backend-cleanup
Limpieza de código: comentarios en prácticamente todo, reestructuración de JS y CSS
2 parents e2e8ec9 + f376554 commit cedfdfb

File tree

9 files changed

+498
-378
lines changed

9 files changed

+498
-378
lines changed

crear.html

Lines changed: 68 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -1,246 +1,76 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<!--UTF-8-->
5-
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
6-
<meta content="utf-8" http-equiv="encoding">
7-
<!--Favicon y nombre-->
8-
<link rel="icon" type="image/png" href="img/favicon.png">
9-
<title>Crear persona - APIHook</title>
10-
<!--Import Google Icon Font-->
11-
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"><!--Import materialize.css-->
12-
<link href="css/materialize.min.css" media="screen,projection" rel="stylesheet" type="text/css">
13-
<script src="js/jquery-3.4.1.min.js" type="text/javascript">
14-
</script><!--Let browser know website is optimized for mobile-->
15-
<meta content="width=device-width, initial-scale=1.0" name="viewport">
16-
<title></title>
4+
<!-- UTF-8 -->
5+
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
6+
<meta content="utf-8" http-equiv="encoding">
7+
<!-- Favicon y nombre -->
8+
<link href="img/favicon.png" rel="icon" type="image/png">
9+
<!-- Si "?edit=true", entonces "Modificar persona - APIHook" -->
10+
<title>Agregar persona - APIHook</title>
11+
<!-- Iconos de Material Design -->
12+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
13+
<!-- Materialize -->
14+
<link href="css/materialize.min.css" media="screen,projection" rel="stylesheet" type="text/css">
15+
<script src="js/jquery-3.4.1.min.js" type="text/javascript">
16+
</script>
17+
<!-- Código para avisarle al navegador que es comp. con móviles -->
18+
<meta content="width=device-width, initial-scale=1.0" name="viewport">
19+
<!-- CSS personalizado -->
20+
<link href="css/style.css" media="screen,projection" rel="stylesheet" type="text/css">
21+
<!-- Código de Javascript -->
22+
<script src="js/crear.js" type="text/javascript"></script>
1723
</head>
1824
<body>
19-
<nav>
20-
<div class="nav-wrapper purple darken-2" style="padding: 0px 50px 0px 50px;">
21-
<a class="brand-logo" href="index.html"><img src="img/logo.png" style="height: 32px; vertical-align: middle; padding-bottom: 7px;"></a>
22-
<ul class="right hide-on-med-and-down" id="nav-mobile">
23-
<li>
24-
<a href="https://github.com/despediteerik/Trinomio-API">Github</a>
25-
</li>
26-
<li>
27-
<a href="index.html">Personas</a>
28-
</li>
29-
</ul>
30-
</div>
31-
</nav>
32-
<div class="container">
33-
<h4 id="title" class="objpadding">Crear una persona</h4><br>
34-
<div class="row">
35-
<form action="./script.php" id="dropdown" method="get" name="dropdown">
36-
<div class="input-field col s12">
37-
<input id="text" name="first_name" class="validate">
38-
<label for="first_name" class="active">Nombre</label>
39-
</div>
40-
<div class="input-field col s12">
41-
<input id="surname" name="last_name" class="validate">
42-
<label for="last_name" class="active">Apellido</label>
43-
</div>
44-
<div class="input-field col s12">
45-
<input id="email" name="email" class="validate">
46-
<label for="email" class="active">Correo electrónico</label>
47-
<span class="helper-text" data-error="Escribe un correo electrónico válido." data-success="¡Excelente!"></span>
48-
</div>
49-
<!--Gets deleted after checkbox populates itself with JSON values -->
50-
<div id="preloader">
51-
<div class="col s4 cbox">
52-
<p>
53-
<label>
54-
<input type="checkbox" disabled="disabled" />
55-
<span>Cargando...</span>
56-
</label>
57-
</p>
58-
</div>
59-
<div class="col s4 cbox">
60-
<p>
61-
<label>
62-
<input type="checkbox" disabled="disabled" />
63-
<span>Cargando...</span>
64-
</label>
65-
</p>
66-
</div>
67-
<div class="col s4 cbox">
68-
<p>
69-
<label>
70-
<input type="checkbox" disabled="disabled" />
71-
<span>Cargando...</span>
72-
</label>
73-
</p>
74-
</div>
75-
<a class="btn disabled nicebtn objpadding">SUBIR</a>
76-
</div>
77-
</form>
78-
79-
<script type="text/javascript">
80-
var urlParams = new URLSearchParams(window.location.search);
81-
let dropdown = $('#dropdown');
82-
83-
$.getJSON('http://evera.challenge.trinom.io/api/courses', function(data) {
84-
var ele = document.getElementById('dropdown');
85-
for (var i = 0; i < data.length; i++) {
86-
ele.innerHTML += '<div class="col s4 cbox"><p><label><input type="checkbox" name="' + data[i].id + '" id="' + data[i].id + '" value="yes"/><span>' + data[i].name + '</span></label></p></div>'
87-
}
88-
ele.innerHTML += '<br><a onclick="enviarAJson();" class="waves-effect waves-light btn nicebtn objpadding purple darken-1">Subir</a>'
89-
loadData();
90-
91-
//remove preloader (Cargando...)
92-
var elem = document.getElementById('preloader')
93-
elem.parentNode.removeChild(elem);
94-
95-
});
96-
97-
function enviarAJson() {
98-
var first_name = document.getElementById("text").value;
99-
var last_name = document.getElementById("surname").value;
100-
var email = document.getElementById("email").value;
101-
var selected_courses = [];
102-
var amountOfObjects;
25+
<!-- Barra de Navegación -->
26+
<nav>
27+
<div class="nav-wrapper purple darken-2">
28+
<a class="brand-logo" href="index.html"><img src="img/logo.png" id="logo"></a>
29+
<ul class="right hide-on-med-and-down" id="nav-mobile">
30+
<li>
31+
<a href="https://github.com/despediteerik/Trinomio-API">Github</a>
32+
</li>
33+
<li>
34+
<a href="index.html">Personas</a>
35+
</li>
36+
</ul>
37+
</div>
38+
</nav>
10339

104-
if (!($("#dropdown input:checkbox:checked").length > 0))
105-
{
106-
M.toast({html: '¡No introduciste ningún curso!', classes: 'rounded'});
107-
return;
108-
}
109-
110-
// Might be a very round-about way to check how many checkboxes are there
111-
// TO-DO: check if I can make this in a less round-about way
112-
$.getJSON('http://evera.challenge.trinom.io/api/courses', function(data) {
113-
amountOfObjects = data.length;
114-
for (var i = 0; i < amountOfObjects; i++) {
115-
selected_courses[i] = document.getElementById(data[i].id).checked;
116-
}
117-
});
118-
var dateObj = new Date();
119-
var curTime = dateObj.toISOString();
120-
var jsonObj = {
121-
'id': 0,
122-
'first_name': first_name,
123-
'last_name': last_name,
124-
'email': email,
125-
'created_at': curTime,
126-
'updated_at': curTime,
127-
'courses': []
128-
}
129-
//template for the JSON we're going to add the courses to!
130-
$.getJSON('http://evera.challenge.trinom.io/api/courses', function(data) {
131-
for (var i = 0; i < data.length; i++) {
132-
if (selected_courses[i] == true) {
133-
jsonObj.courses.push({
134-
'id': (i + 1),
135-
'name': 'placeholder',
136-
'language_code': 'ph',
137-
'level_id': 0,
138-
'created_at': curTime,
139-
'updated_at': curTime,
140-
'level': {
141-
'id': 0,
142-
'name': 'placeholder'
143-
},
144-
'language': {
145-
'code': 'placeholder',
146-
'name': 'placeholder'
147-
}
148-
})
149-
//luckily Trinomio did all of the heavy lifting, and by just inputting the ID number it fills all of the spaces for us.
150-
}
151-
}
152-
//Finally, FINALLY, we have our JSON object. Finally.
153-
//Now we can just send a POST request to the API server.
154-
//I got stuck for like a day on this part because JS variables were messing with me.
155-
console.log(JSON.stringify(jsonObj))
156-
if (urlParams.has('edit')) { //urlparams edit = true
157-
var peoplesId = urlParams.get('id');
158-
var url = "http://evera.challenge.trinom.io/api/peoples/" + peoplesId;
159-
console.log(url);
160-
var peoplesType = "PUT";
161-
var peoplesTerm = "actualizado";
162-
} else {
163-
var url = "http://evera.challenge.trinom.io/api/peoples";
164-
var peoplesType = "POST";
165-
var peoplesTerm = "subido";
166-
}
167-
$.ajax({
168-
url: url,
169-
type: peoplesType,
170-
data: JSON.stringify(jsonObj),
171-
contentType: "application/json",
172-
dataType: 'json',
173-
error: function(xhr, status, error) {
174-
// Error catching
175-
console.log(error);
176-
console.log(xhr.responseText);
177-
errores = JSON.parse(xhr.responseText);
178-
for (var key in errores.errors) {
179-
for (var error in errores.errors[key]) {
180-
// Switch-case structure for translating the errors JSON may give.
181-
// It isn't translating everything - more technical errors I'd rather have in English anyways,
182-
// but it's the four most common errors having to do with user error.
183-
switch(errores.errors[key][error]) {
184-
case "The first name field is required.":
185-
M.toast({html: "Se requiere escribir el nombre.", classes: 'rounded'});
186-
break;
187-
case "The last name field is required.":
188-
M.toast({html: "Se requiere escribir el apellido.", classes: 'rounded'});
189-
break;
190-
case "The email field is required.":
191-
M.toast({html: "Se requiere escribir el correo electrónico.", classes: 'rounded'});
192-
break;
193-
case "The email has already been taken.":
194-
M.toast({html: "Este correo electrónico ya está en uso.", classes: 'rounded'});
195-
break;
196-
default:
197-
// In case the error doesn't exist, just print the original error in English.
198-
M.toast({html: errores.errors[key][error], classes: 'rounded'});
199-
break;
200-
}
201-
}
202-
}
203-
},
204-
success: function() {
205-
var toastHTML = '<span>¡Tu entrada se ha ' + peoplesTerm + ' con éxito!</span><a href="./index.html"><button class="btn-flat toast-action">Mirar</button></a>';
206-
M.toast({html: toastHTML, classes: 'rounded'});
207-
} //TO-DO: send them back to index. probably add an alert afterwards
208-
});
209-
});
210-
}
211-
212-
213-
function loadData() {
214-
if(urlParams.has('edit')) {
215-
var first = document.getElementById('text');
216-
var last = document.getElementById('surname');
217-
var email = document.getElementById('email');
218-
first.value = urlParams.get('first_name')
219-
last.value = urlParams.get('last_name')
220-
email.value = urlParams.get('email')
221-
}
222-
}
223-
//NOTE: The following could be on the function loadData();, but since we have to wait until the JSON-populated
224-
//checkboxes load before changing the title, it looks jarring and breaks the illusion of two different forms.
225-
if(urlParams.has('edit')) {
226-
var ele = document.getElementById('title');
227-
ele.innerHTML = "Modificar una persona"
228-
document.title = "Modificar persona - APIHook";
229-
}
230-
</script>
231-
</div>
232-
</div>
40+
<!-- Formulario (aka el inicio real de la página) -->
41+
<div class="container">
42+
<!-- El título cambia dependiendo de si "?edit=true" existe -->
43+
<h4 class="objpadding" id="mainTitle">Agregar una persona</h4><br>
44+
<div class="row">
45+
<form action="./script.php" id="checkboxForm" method="get" name="dropdown">
46+
<div class="input-field col s12">
47+
<input class="validate" id="text" name="first_name"> <label class="active" for="first_name">Nombre</label>
48+
</div>
49+
<div class="input-field col s12">
50+
<input class="validate" id="surname" name="last_name"> <label class="active" for="last_name">Apellido</label>
51+
</div>
52+
<div class="input-field col s12">
53+
<input class="validate" id="email" name="email"> <label class="active" for="email">Correo electrónico</label>
54+
</div>
55+
<!-- Estos placeholders de "Cargando..." se mantienen hasta que la checkbox se puebla con los valores en JSON. -->
56+
<!-- Probé poniendo unos preloaders al estilo de spinners, pero quedaban mal porque duraba un par de ms. la carga. -->
57+
<!-- Si no hay Internet, esta parte se va a mantener en su lugar, porque no le llegan las requests a la API. -->
58+
<div id="preloader">
59+
<div class="col s4 cbox">
60+
<p><label><input disabled="disabled" type="checkbox"> <span>Cargando...</span></label></p>
61+
</div>
62+
<div class="col s4 cbox">
63+
<p><label><input disabled="disabled" type="checkbox"> <span>Cargando...</span></label></p>
64+
</div>
65+
<div class="col s4 cbox">
66+
<p><label><input disabled="disabled" type="checkbox"> <span>Cargando...</span></label></p>
67+
</div><a class="btn disabled nicebtn objpadding">SUBIR</a>
68+
</div>
69+
</form>
70+
</div>
23371
</div>
234-
<script type="text/javascript" src="js/materialize.min.js"></script>
235-
<style>
236-
.nicebtn
237-
{
238-
margin-top: 15px;
239-
}
240-
.objpadding
241-
{
242-
margin-left: 10px;
243-
}
244-
</style>
72+
73+
<!-- Javascript para Materialize! (Aparentemente, cargarlo al final de la página lo hace más rápido.) -->
74+
<script src="js/materialize.min.js" type="text/javascript"></script>
24575
</body>
246-
</html>
76+
</html>

css/style.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* CSS para Index */
2+
3+
i.icon-black {
4+
color: black;
5+
}
6+
7+
8+
#tableContainer {
9+
width: 90%;
10+
margin-top: 20px;
11+
margin-bottom: 50px;
12+
}
13+
14+
.childTable {
15+
background-color: gainsboro;
16+
}
17+
18+
/* CSS para Crear */
19+
20+
.nicebtn
21+
{
22+
margin-top: 15px;
23+
}
24+
.objpadding
25+
{
26+
margin-left: 10px;
27+
}
28+
29+
/* CSS multi-uso */
30+
31+
#logo {
32+
height: 32px;
33+
vertical-align: middle;
34+
padding-bottom: 7px;
35+
}
36+
37+
.nav-wrapper {
38+
padding: 0px 50px 0px 50px;
39+
}

img/botones.png

2.66 KB
Loading

img/expandir.png

8.14 KB
Loading

img/toasteliminado.png

5.1 KB
Loading

0 commit comments

Comments
 (0)