-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal.js
More file actions
120 lines (91 loc) · 3.78 KB
/
journal.js
File metadata and controls
120 lines (91 loc) · 3.78 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
const favoritesList = document.getElementById('favoritesList');
const favoritesCount = document.getElementById("favoritesCount");
const updateFavoritesCount = ()=> {
const favorites = JSON.parse(localStorage.getItem("pokeArray"))|| [];
favoritesCount.textContent= favorites.length;
favoritesCount.className= " bg-blue-500 text-lg inline-block rounded"
};
// Load favorites from localStorage
function loadFavorites() {
const favorites = JSON.parse(localStorage.getItem('pokeArray')) || [];
favoritesList.textContent='';
if (favorites.length === 0) {
const emptyMsg = document.createElement('p');
emptyMsg.textContent = 'No favorites added yet.';
favoritesList.appendChild(emptyMsg);
return;
}
console.log(favorites);
favorites.forEach(favorite => {
const card = createPokemonCard(favorite);
favoritesList.appendChild(card);
});
updateFavoritesCount();
}
//favorites count
// Create Pokémon card with DOM
function createPokemonCard(pokemon) {
const card = document.createElement('div');
card.className = 'pokemon bg-violet-300 rounded-lg shadow-lg p-4 transform hover:scale-105 transition-transform';
const img = document.createElement('img');
img.src = pokemon.image;
img.alt = pokemon.name;
img.className = 'w-44 opacity-75 transition-all duration-300 ease-in-out hover:opacity-100 hover:scale-125';
const name = document.createElement('h3');
name.className = 'text-xl font-bold capitalize mb-2';
name.textContent = pokemon.name;
const hp = document.createElement('p');
hp.className = 'text-gray-600 mb-2 text-sm';
hp.textContent = `HP: ${pokemon.hp}`;
const attack = document.createElement('p');
attack.className = 'text-gray-600 mb-2 text-sm';
attack.textContent = ` Attack ${pokemon.attack}`;
const defense = document.createElement('p');
defense.className = 'text-gray-600 mb-2 text-sm';
defense.textContent = `Defense: ${pokemon.defense}`;
const speed = document.createElement('p');
speed.className = 'text-gray-600 mb-2 text-sm';
speed.textContent = `Speed: ${pokemon.speed}`;
const notesDiv = document.createElement('div');
notesDiv.className = 'mt-4';
const notesLabel = document.createElement('label');
notesLabel.textContent = 'Your Notes';
notesLabel.className = 'block text-gray-700 font-bold mb-2';
notesDiv.appendChild(notesLabel);
const notesTextarea = document.createElement('textarea');
notesTextarea.className = 'w-full p-2 border rounded';
notesTextarea.value = pokemon.note || '';
notesDiv.appendChild(notesTextarea);
const saveButton = document.createElement('button');
saveButton.textContent = 'Save Notes';
saveButton.className = 'm-2 bg-blue-500 text-white p-2 rounded';
const displayNotes = document.createElement('p');
displayNotes.className = 'text-red-700 mt-2';
displayNotes.textContent = pokemon.note ? `Notes: ${pokemon.note}` : 'No notes yet';
notesDiv.appendChild(displayNotes);
saveButton.addEventListener('click', () => {
const newNotes = notesTextarea.value;
saveNotes(pokemon.name, newNotes);
displayNotes.textContent = newNotes ? `${newNotes}` : 'No notes yet';
notesTextarea.value = '';
});
notesDiv.appendChild(saveButton);
card.appendChild(img);
card.appendChild(name);
card.appendChild(hp);
card.appendChild(attack);
card.appendChild(defense);
card.appendChild(speed);
card.appendChild(notesDiv);
return card;
}
// Save notes localStorage
function saveNotes(pokemonName, notes) {
const pokeArray = JSON.parse(localStorage.getItem('pokeArray')) || [];
const updatedPokeArray = pokeArray.map(pokemon =>
pokemon.name === pokemonName ? { ...pokemon, note: notes } : pokemon
);
localStorage.setItem('pokeArray', JSON.stringify(updatedPokeArray));
}
// Initialize
document.addEventListener('DOMContentLoaded', loadFavorites);