Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (119 loc) · 4.93 KB
/
Copy pathscript.js
File metadata and controls
146 lines (119 loc) · 4.93 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
141
142
143
144
145
146
// script.js
document.getElementById('upload-button').addEventListener('click', function () {
const photoInput = document.getElementById('photo-input');
const commentInput = document.getElementById('comment-input');
//valida si es una imagen lo que está seleccionado para subir (no se si va en esta parte)
if (!file.type.startsWith('image/')) {
Swal.fire({
icon: "error",
title: "Archivo no válido",
text: "Por favor, selecciona una imagen.",
});
return;
}
////////////////////////////////////////////////////////////////////////////////////////
if (photoInput.files.length > 0 && commentInput.value.trim() !== "") {
const file = photoInput.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const photoUrl = e.target.result;
const comment = commentInput.value;
addPost(photoUrl, comment);
// Clear inputs after uploading
photoInput.value = '';
commentInput.value = '';
Swal.fire({
icon: "success",
title: "Post creado con éxito",
text: "Tu imagen y comentario han sido añadidos al feed.",
});
};
reader.readAsDataURL(file);
} else {
Swal.fire({
icon: "error",
title: "Faltan datos",
text: "Por favor, selecciona una imagen y escribe un comentario antes de subir.",
});
}
});
function createElement(tag, className = '', attributes = {}) {
const element = document.createElement(tag);
if (className) element.className = className;
Object.keys(attributes).forEach(attr => element.setAttribute(attr, attributes[attr]));
return element;
}
function addPost(photoUrl, comment) {
const feed = document.getElementById('feed');
const post = createElement('div', 'post card mb-3');
// Post image
const img = createElement('img', 'card-img-top', { src: photoUrl });
// Card body
const cardBody = createElement('div', 'card-body');
// Initial comment
const initialComment = createElement('p', 'card-text');
initialComment.innerText = comment;
// Like button
const likeButton = createElement('button', 'btn btn-info like-button mb-2', { 'data-likes': 0 });
likeButton.innerText = 'Me gusta';
likeButton.addEventListener('click', function () {
const likes = parseInt(likeButton.getAttribute('data-likes')) + 1;
likeButton.innerText = `Me gusta (${likes})`;
likeButton.setAttribute('data-likes', likes);
likeButton.classList.add('liked');
});
// User comment input
const commentInput = createElement('input', 'form-control comment-input', { type: 'text', placeholder: 'Escribe aquí tu comentario' });
const commentButton = createElement('button', 'btn btn-primary mt-2 comment-button');
commentButton.innerText = 'Comentar';
commentButton.addEventListener('click', function () {
if (commentInput.value.trim() !== "") {
const newComment = createElement('p', 'comment-user');
newComment.innerText = commentInput.value;
cardBody.appendChild(newComment);
commentInput.value = '';
} else {
Swal.fire({
icon: "warning",
text: "Por favor, escribe algo antes de comentar.",
});
}
});
// Build post structure
cardBody.appendChild(initialComment);
cardBody.appendChild(likeButton);
cardBody.appendChild(commentInput);
cardBody.appendChild(commentButton);
post.appendChild(img);
post.appendChild(cardBody);
feed.prepend(post);
}
// guardar las imagenes el local storage
function savePostsToLocalStorage(posts) {
localStorage.setItem('posts', JSON.stringify(posts));
}
function loadPostsFromLocalStorage() {
const posts = JSON.parse(localStorage.getItem('posts')) || [];
posts.forEach(post => addPost(post.photoUrl, post.comment));
}
document.getElementById('upload-button').addEventListener('click', function () {
const photoInput = document.getElementById('photo-input');
const commentInput = document.getElementById('comment-input');
if (photoInput.files.length > 0 && commentInput.value.trim() !== "") {
const file = photoInput.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const photoUrl = e.target.result;
const comment = commentInput.value;
addPost(photoUrl, comment);
const currentPosts = JSON.parse(localStorage.getItem('posts')) || [];
currentPosts.push({ photoUrl, comment });
savePostsToLocalStorage(currentPosts);
photoInput.value = '';
commentInput.value = '';
};
reader.readAsDataURL(file);
}
});
// Llama a esta función al cargar la página
loadPostsFromLocalStorage();