Skip to content

Commit 23ba408

Browse files
committed
Fix(Tags not working)
- Refactored every tag function to include it in its own function to improve readability. - Added last missing features (color changing, reset buttons) - Improved flow by checking outside clicks to close Choice Tag container - Fixed responsive App is back as before, but with a refactored code !
1 parent 436ee18 commit 23ba408

7 files changed

Lines changed: 211 additions & 155 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ Features ideas:
2121
- [x] To add tags
2222
- [ ] Make possible to change tag colors
2323
- [ ] Add a description to each TODO
24-
- [ ] Refactor code
24+
- [x] Refactor code
2525
- [ ] Have a custom logo

css/newstyle.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ nav button:hover {
9393
/** Tags' style*/
9494

9595
.tagtest {
96+
display: flex;
97+
flex-direction: row;
98+
align-items: center;
99+
gap: 10px;
96100
background-color: var(--tag-color);
97101
border-radius: 5px;
98102
width: fit-content;
@@ -151,6 +155,7 @@ section h1 {
151155
/** PRETODO container*/
152156
.containerx {
153157
display: flex;
158+
align-items: center;
154159
gap: 15px;
155160
height: 10%;
156161
width: 100%;
@@ -163,6 +168,7 @@ section h1 {
163168
gap: 10px;
164169
height: 50%;
165170
width: 100%;
171+
padding: 10px;
166172
background-color: var(--color-sec);
167173
box-shadow: 1px 1px 6px rgb(0,0,0,0.3);
168174
border-radius: 15px;
@@ -187,6 +193,9 @@ section h1 {
187193
}
188194

189195
.containerx button {
196+
display: flex;
197+
align-items: center;
198+
190199
background-color: var(--color-sec);
191200
border: none;
192201
font-size: xx-large;
@@ -272,6 +281,7 @@ section h1 {
272281

273282
/* * Settings */
274283
.settings {
284+
z-index: 1;
275285
position: absolute;
276286
top: 25%;
277287
left: -600px;
@@ -305,4 +315,11 @@ section h1 {
305315
margin: 0px;
306316
font-size: xx-large;
307317
font-weight: bold;
318+
}
319+
.removetagbtn {
320+
user-select: none;
321+
border: none;
322+
cursor: pointer;
323+
margin: 0px;
324+
308325
}

css/responsive.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@media (min-width: 600px) and (max-width: 1200px){
2+
.containerimportanttodos {
3+
width: 100%;
4+
}
5+
.settings.active {
6+
top: 5%;
7+
left: 35%;
8+
width: 55%;
9+
10+
transition: all 0.1s linear;
11+
}
12+
.containerx {
13+
width: 60%;
14+
}
15+
.containerimportanttodos h1 {
16+
width: 60%;
17+
}
18+
}

css/style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,4 @@ h1 {
449449
border-radius: 15px;
450450

451451
box-shadow: 1px 1px 6px rgb(0,0,0,0.5);
452-
}
452+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flyingtodo",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "A TODO list application to learn JS, that I will maybe continue later.",
55
"main": "main.js",
66
"scripts": {

script/newscript.js

Lines changed: 107 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
// TODO Rafraischissement de toutes les valeurs au démarrage
2-
3-
// TODO Fonctionnalité pour valider les todos et tags avec entrée
4-
// TODO sauvegarde des données
5-
6-
// TODO fonction pour display les tags
7-
// TODO Fonction pour créer les tags
8-
// TODO Fonction pour supprimer les tags
9-
// TODO Actualisation de la checkbox avec le style au démarrage et quand elles sont checked
10-
// TODO Fonction pour actualiser les tags
11-
// TODO Bouton pour tout reset
12-
// TODO Fonctions de debug
13-
// TODO Changement de couleur dans les settings
14-
// TODO Logique de tous les boutons
15-
161

172

183
let todos = [ ];
@@ -106,7 +91,7 @@ function displayTodo() {
10691
<img class="itemdel" data-id="${item.id}" src="../img/bin.png">
10792
</div>
10893
<div class="containertagsintodo">
109-
94+
<button class="addtagbutton" data-id="${item.id}" id="addtagbutton">+</button>
11095
</div>`;
11196

11297

@@ -126,19 +111,40 @@ function displayTodo() {
126111

127112

128113

129-
containertodo.prepend(divtodo)
130-
114+
containertodo.prepend(divtodo);
131115

116+
// * Display tags for this todo
117+
const containertagsintodo = divtodo.querySelector('.containertagsintodo');
118+
119+
item.todotag.forEach(tag => {
120+
const tagDiv = document.createElement('div');
121+
tagDiv.className = "tagtest";
122+
tagDiv.style.backgroundColor = tag.color;
123+
tagDiv.innerHTML = `<p class="removetagbtn" data-tag-id="${tag.id}">✖</p>${tag.name}`;
124+
containertagsintodo.appendChild(tagDiv);
125+
126+
// Remove tag from todo
127+
const removeTagBtn = tagDiv.querySelector('.removetagbtn');
128+
removeTagBtn.addEventListener('click', () => {
129+
item.todotag = item.todotag.filter(t => t.id !== tag.id);
130+
localStorage.setItem('todoitems', JSON.stringify(todos));
131+
displayTodo();
132+
});
133+
});
132134

133135

134136

135-
// * Add tag btn in each div
136-
displayTagsInTodo(item, divtodo);
137137

138138
const bintodo = document.querySelector('.itemdel');
139139
bintodo.addEventListener('click', () => {
140140
deleteTodo(item.id);
141141
});
142+
143+
const addtagbutton = document.getElementById('addtagbutton');
144+
145+
addtagbutton.addEventListener('click', () => {
146+
choiceTagg(item.id);
147+
});
142148

143149
});
144150

@@ -158,15 +164,12 @@ function deleteTodo(id) {
158164
});
159165
localStorage.setItem('todoitems', JSON.stringify(todos));
160166
displayTodo();
161-
162167
};
163168

164169

165170

166171
// * Tags
167172

168-
169-
170173
// * Tag creation && storage
171174

172175
const inputcreatetags = document.querySelector('.inputcreatetags');
@@ -246,77 +249,101 @@ function deleteTags(id) {
246249
};
247250

248251

249-
const choicetagcont = document.querySelector('.choicetagcont');
250-
251-
function applyTags(item, divtodo) {
252-
let titletag = document.createElement('p');
253-
titletag.innerText = "Choose a tag !"
254-
titletag.className = "titletag";
255-
choicetagcont.prepend(titletag);
256252

253+
// * Choice of tags
254+
function choiceTagg(todoId) {
255+
256+
choiceTag.classList.toggle('active');
257257

258258
allTags.forEach(tag => {
259+
taginchoice = document.createElement('div');
259260

260-
const tagInList = document.createElement('div');
261+
taginchoice.className = "tagtest";
262+
taginchoice.innerText = `${tag.name}`;
263+
taginchoice.style.backgroundColor = `${tag.color}`;
261264

262-
tagInList.className = "tagtest";
263-
tagInList.innerHTML = `<p>${tag.name}</p>`
264-
tagInList.style.backgroundColor = `${tag.color}`;
265-
tagInList.style.cursor = "pointer";
265+
choiceTag.append(taginchoice);
266266

267-
choicetagcont.append(tagInList);
267+
taginchoice.addEventListener('click', () => {
268+
addTagToTodo(tag, todoId);
269+
});
270+
});
271+
};
268272

269-
tagInList.addEventListener('click', () => {
270-
!item.todotag ? "" : item.todotag = [];
273+
function addTagToTodo(tag, todoId) {
274+
const todo = todos.find(item => item.id === todoId);
275+
276+
if (todo && !todo.todotag.some(t => t.id === tag.id)) {
277+
todo.todotag.push(tag);
278+
localStorage.setItem('todoitems', JSON.stringify(todos));
279+
displayTodo();
280+
choiceTag.classList.remove('active');
281+
choiceTag.innerHTML = '';
282+
};
283+
};
271284

272-
if (!item.todotag.some(t => t.name === tag.name)) {
273285

274-
item.todotag.push(tag);
275286

276-
localStorage.setItem('todoitems', JSON.stringify(todos));
277-
console.log(item);
278-
displayTagsInTodo(item, divtodo);
279-
choiceTag.classList.remove('active');
280-
};
281-
});
282-
283-
});
284-
};
287+
// * Style features to make the app more fluid
288+
document.addEventListener('click', (event) => {
289+
const addtagbutton = document.querySelector('.addtagbutton');
285290

291+
if (!choiceTag.contains(event.target) && !addtagbutton.contains(event.target) ) {
292+
choiceTag.classList.remove('active');
293+
};
294+
});
286295

287296

288-
function displayTagsInTodo(item, divtodo) {
289297

290-
const containertagsintodo = divtodo.querySelector('.containertagsintodo');
291-
containertagsintodo.innerHTML = `<button class="addtagbutton" data-id="${item.id}" id="addtagbutton">+</button>`;
292-
const addTagButton = divtodo.querySelector('.addtagbutton');
293-
294298

295-
let isChoiceTagOpen = false;
296-
299+
300+
// * Reset buttons
301+
const resetbtn = document.getElementById('resetbtn');
302+
303+
resetbtn.addEventListener('click', () => {
304+
305+
todos = [];
306+
allTags = [];
307+
localStorage.setItem('todoitems', JSON.stringify(todos));
308+
localStorage.setItem('alltags', JSON.stringify(allTags));
309+
310+
containerpretodo.innerHTML = '';
311+
312+
displayTodo();
313+
displayTags();
314+
315+
});
316+
317+
318+
319+
// * Color in settings
320+
const root = document.documentElement;
321+
const inputmaincolor = document.querySelector('.maincolorpick');
322+
const inputseccolor = document.querySelector('.seccolorpick');
323+
const resetcolbtn = document.querySelector('.resetclr');
324+
325+
// main color
326+
inputmaincolor.addEventListener('change', () => {
327+
const mainvalue = inputmaincolor.value;
328+
root.style.setProperty('--color-main', mainvalue);
297329

330+
console.log(inputmaincolor.value);
331+
localStorage.setItem('maincolor', mainvalue);
332+
});
333+
334+
// secondary color
335+
inputseccolor.addEventListener('change', () => {
336+
const seccolor = inputseccolor.value;
337+
root.style.setProperty('--color-sec', seccolor);
298338

299-
addTagButton.addEventListener('click', () => {
300-
if (isChoiceTagOpen == false) {
301-
console.log('open');
302-
isChoiceTagOpen = true;
303-
choiceTag.classList.add('active');
304-
305-
applyTags(item, divtodo);
306-
307-
} else {
308-
console.log('closed');
309-
isChoiceTagOpen = false;
310-
choiceTag.classList.remove('active');
311-
};
312-
});
313-
// ! ça duplique dans la liste et ça l'applique uniquement au premier
314-
item.todotag.forEach(tag => {
315-
let tagInTodo = document.createElement('div');
316-
tagInTodo.className = "tagtest";
317-
tagInTodo.innerText = `${tag.name}`;
318-
tagInTodo.style.backgroundColor = `${tag.color}`;
319-
320-
containertagsintodo.append(tagInTodo);
321-
});
322-
};
339+
console.log(seccolor);
340+
localStorage.setItem('seccolor', seccolor);
341+
});
342+
343+
// reset color button
344+
resetcolbtn.addEventListener('click', () => {
345+
root.style.setProperty('--color-main', 'rgb(177, 177, 147)');
346+
root.style.setProperty('--color-sec', 'rgb(231, 231, 217)');
347+
localStorage.removeItem('seccolor');
348+
localStorage.removeItem('maincolor');
349+
});

0 commit comments

Comments
 (0)