-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
140 lines (96 loc) · 3.73 KB
/
app.js
File metadata and controls
140 lines (96 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Variables
const listaTweets = document.getElementById('lista-tweets');
//EventListeners
eventListeners();
function eventListeners(){
//Cuando se envia el formulario
document.querySelector('#formulario').addEventListener('submit',agregarTweet);
//Se ejecuta funcion agregarTweet
// Borrar Tweets
listaTweets.addEventListener('click', borrarTweet);
//Contenido cargado
document.addEventListener('DOMContentLoaded', localStorageListo);
}
//Funciones
//agregar tweet del formulario
function agregarTweet(e){
e.preventDefault();
//Leer el valor del textarea .value accede al valor del textarea
const tweet = document.getElementById('tweet').value;
//crear boton de eliminar - JS MODERNO: CREATEELEMENT
const botonBorrar = document.createElement('a');
botonBorrar.classList = 'borrar-tweet';
botonBorrar.innerText = "X";
//crear elemento y añadirle el contenido a la lista
const li = document.createElement('li');
li.innerText = tweet;
//añade el boton de borrar al tweet
li.appendChild(botonBorrar);
//añade el tweet a la lista
listaTweets.appendChild(li);
//añadir a local storage
agregarTweetLocalStorage(tweet);
}
//Elimina el tweet del dom
function borrarTweet(e){
e.preventDefault();
//JS moderno
if(e.target.className === 'borrar-tweet'){
e.target.parentElement.remove();
borrarTweetLocalStorage(e.target.parentElement.innerText);
alert('Tweet eliminado');
}
}
//Añadir a localstorage <---------
//Mando el contenido del tweet ingresado por form
function agregarTweetLocalStorage(tweet){
let tweets;//lee todos los tweet que haya
tweets = obtenerTweetLocalStorage();
//Añadir el nuevo tweet
tweets.push(tweet);
//Convertir de string a arreglo para localstorage
localStorage.setItem('tweets', JSON.stringify(tweets));
}
//Lee cuantos tweets hay en localstorage
//Comprueba que haya elementos en localstorage, retorna un arreglo
//Si hay se añade al arreglo
function obtenerTweetLocalStorage(){
let tweets;
//Reviso los values de localstorage
if(localStorage.getItem('tweets') === null){
tweets = [];
}else{
tweets = JSON.parse(localStorage.getItem('tweets'));
}
return tweets;
}
//['Tweet 1', 'Tweet 2', 'Tweet 3']
function localStorageListo(){
let tweets;
tweets = obtenerTweetLocalStorage();
tweets.forEach(function(tweet){
const botonBorrar = document.createElement('a');
botonBorrar.classList = 'borrar-tweet';
botonBorrar.innerText = "X";
//crear elemento y añadirle el contenido a la lista
const li = document.createElement('li');
li.innerText = tweet;
//añade el boton de borrar al tweet
li.appendChild(botonBorrar);
//añade el tweet a la lista
listaTweets.appendChild(li);
});
}
//Eliminar tweet de localstorage
function borrarTweetLocalStorage(tweet, index){
let tweets, tweetBorrar;
//Elimina la X del tweet
tweetBorrar = tweet.substring(0, tweet.length - 1);
tweets = obtenerTweetLocalStorage();
tweets.forEach(function(tweet){
if(tweetBorrar === tweet){
tweets.splice(index, 1)
}
});
localStorage.setItem('tweets', JSON.stringify(tweets));
}