-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.js
164 lines (147 loc) · 5.25 KB
/
categories.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// DEFAULT CATEGORIES
const categories = [
{
id: uuidv4(),
name: "Comida",
},
{
id: uuidv4(),
name: "Servicios",
},
{
id: uuidv4(),
name: "Salidas",
},
{
id: uuidv4(),
name: "Educación",
},
{
id: uuidv4(),
name: "Transporte",
},
{
id: uuidv4(),
name: "Trabajo",
}
];
// GENERATE TABLE OF CATEGORIES
const createCategoriesTable = (data) => {
const tableOfCategories = document.getElementById("categories-table-body");
tableOfCategories.innerHTML = "";
data = validateLocalStorage("categories", categories);
if (data && data.length > 0) {
for (let category of data) {
tableOfCategories.innerHTML += `
<tr class="flex columns-2 justify-between items-center py-1">
<td class="text-center w-2/5 bg-primary dark:bg-secondary px-2 py-1 rounded text-light font-bold">${category.name}</td>
<td class="flex gap-2 tablet:gap-5 w-2/5 justify-end">
<button
class="delete-category-btn flex items-center rounded py-1 px-2 h-8 justify-center bg-dark hover:bg-primary shadow-inner font-bold dark:text-light dark:hover:text-light gap-2"
id="btn-delete-${category.id}"
>
<i class="fa-solid fa-trash pointer-events-none"></i>
Eliminar
</button>
<button
class="edit-category-btn flex items-center rounded py-1 px-3 h-8 justify-center hover:bg-accent bg-secondary shadow-inner font-bold dark:text-light dark:hover:text-light gap-2"
id="btn-edit-${category.id}"
>
<i class="fa-solid fa-pen pointer-events-none"></i>
Editar
</button>
</td>
</tr>
`;
}
setStyleNone('no-categories')
linkCategoriesWithSelect();
editCategoryEvent(document.getElementsByClassName("edit-category-btn"));
deleteCategoryEvent(document.getElementsByClassName("delete-category-btn"));
} else {
setStyleFlex('no-categories')
};
};
// RETURN CATEGORIES FROM LOCAL STORAGE
const getCategories = () => {
return JSON.parse(localStorage.getItem("categories"));
};
// UPDATE CATEGORIES
const updateCategories = (categories) => {
localStorage.setItem("categories", JSON.stringify(categories));
};
// CREATE CATEGORY
const createCategory = (newCategoryName) => {
let savedCategories = getCategories();
let newCategory = { id: uuidv4(), name: newCategoryName };
savedCategories.push(newCategory);
updateCategories(savedCategories);
createCategoriesTable(savedCategories);
};
// CONECT CATEGORIES WITH THE SELECTS OF CATEGORIES
const linkCategoriesWithSelect = () => {
const categoriesSelect = document.getElementsByClassName('category-select');
const savedCategories = validateLocalStorage("categories", categories);
for (let select of categoriesSelect) {
if (select.classList.contains('filter')) {
select.innerHTML = '';
select.innerHTML += `<option value="Todas">Todas</option>`;
} else {
select.innerHTML = '';
}
for (let category of savedCategories) {
select.innerHTML += `<option value="${category.name}">${category.name}</option>`;
}
}
};
// EDIT CATEGORY
const editCategoryEvent = (editCategoryButtons) => {
const savedCategories = getCategories();
for (let btn of editCategoryButtons) {
btn.addEventListener("click", (e) => {
const category = seekId(savedCategories, e.target.id, 9);
if (category) {
document.getElementById("edit-category-name").value = category.name;
document.querySelector('.save-edit-category').setAttribute("id", `confirm-${btn.id.slice(9)}`);
setStyleFlex("rename-category");
setStyleNone("categories");
}
});
}
};
const renameCategory = (array, categoryId, newName) => {
const savedCategories = getCategories();
const editedCategories = savedCategories.map((object) => {
if (object.id === categoryId) {
return {
...object,
name: newName
};
} else {
return object;
}
});
updateCategories(editedCategories);
createCategoriesTable(editedCategories);
};
// DELETE CATEGORY
const deleteCategoryEvent = (deleteCategoryButtons) => {
const savedCategories = getCategories();
for (let btn of deleteCategoryButtons) {
btn.addEventListener("click", (e) => {
const category = seekId(savedCategories, e.target.id, 11);
if (category) {
document.getElementById("category-name").innerHTML = category.name;
document.querySelector('.confirm-delete-category').setAttribute("id", `confirm-${btn.id.slice(11)}`);
document.querySelector('.confirm-delete-category').setAttribute("name", category.name);
setStyleFlex("delete-category");
setStyleNone("categories");
}
});
}
};
const confirmDeleteCategory = (array, categoryId) => {
const filteredCategories = array.filter(object => object.id !== categoryId);
updateCategories(filteredCategories);
createCategoriesTable(filteredCategories);
};