Skip to content

Commit cdc8d51

Browse files
committed
crud operation implemented and modified(almost all bugs fixed/tests required)
TODO tests form builder improved (autofilling of fields in case of note editing) notes' table builder updated/fixed bug with eventhandler
1 parent 1af11a0 commit cdc8d51

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

scripts/HTMLBuilder.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,34 @@ function loadIconsIntoHeader(){
1818
});
1919
}
2020

21-
function buildForm(name, created, category, content){
21+
function buildForm(note){
2222
let form = document.createElement('form');
2323

2424
form.innerHTML = `
25-
<input type="text" name="name" value="${typeof name === "string"? name : ''}" placeholder="Name">
26-
<!-- <input type="date" name="date" value="${created}">-->
25+
<input type="text" name="name" value="${typeof note.name === "string"? note.name : ''}" placeholder="Name">
2726
<select name="categories">
28-
` + Object.keys(categories).map(c => `<option value="${c}">${c}</option>`) + `
27+
` + Object.keys(categories).map(c => `<option value="${c}" ${note.category === c? 'selected' : ''}>${c}</option>`) + `
2928
</select>
30-
<input type="text" name="content" value="${content? content : ''}" placeholder="Content">
29+
<input type="text" name="content" value="${note.content? note.content : ''}" placeholder="Content">
3130
<input type="submit" value="Submit">
3231
`;
3332

3433
form.onsubmit = (event)=>{
3534
event.preventDefault();
36-
let note = {
37-
id: makeRandomID(10),
35+
let newNote = {
36+
id: (note)? note.id : makeRandomID(10),
3837
name: event.target.name.value,
39-
created: new Date(),
38+
created: (note)? note.created : new Date(),
4039
category: event.target.categories.value,
4140
content: event.target.content.value,
4241
dates: getDatesFromText(event.target.content.value),
43-
archived: false
42+
archived: (note)? note.archived : false
4443
}
4544

46-
createNote(note);
45+
if (typeof note.name === "string")
46+
updateNote(newNote);
47+
else
48+
createNote(newNote);
4749

4850
document.getElementsByClassName('wrapper-div')[0].remove();
4951
};
@@ -62,14 +64,19 @@ function createNote(note){
6264
refreshTables();
6365
}
6466
function updateNote(note){
65-
notes.push(note);
67+
notes.splice(notes.findIndex(n => n.id === note.id),1, note);
6668
refreshTables();
6769
}
6870
function deleteNote(noteID){
69-
notes = notes.filter(note => note.id !== noteID);
71+
notes.splice(notes.indexOf(notes.find(note => note.id === noteID)),1);
7072
document.getElementById(noteID).remove();
73+
clearInnerHTML(statisticsTable);
74+
buildStatisticTable();
75+
}
76+
function changeArchiveState(note){
77+
notes[notes.findIndex(n => n.id === note.id)].archived = !notes[notes.findIndex(n => n.id === note.id)].archived;
78+
refreshTables()
7179
}
72-
7380

7481
function refreshTables(){
7582
clearAllTables();
@@ -130,24 +137,41 @@ function buildStatTr(category, active, total){
130137
function buildNotesTable(){
131138
notes.forEach(note => {
132139
if (!note.archived === activeNoteTableShown)
133-
notesTable.innerHTML += buildNotesTr(note);
140+
notesTable.append( buildNotesTr(note) );
134141
})
135142

136143
}
144+
137145
function buildNotesTr(note){
138-
return `
139-
<tr>
146+
let tr = document.createElement('tr');
147+
tr.id = note.id;
148+
tr.innerHTML = `
140149
<td className="category-icon">${ categories[note.category] }</td>
141150
<td className="name">${ note.name }</td>
142151
<td className="created">${ note.created.toLocaleDateString() }</td>
143152
<td className="category1">${ note.category }</td>
144153
<td className="content">${ note.content }</td>
145154
<td className="dates">${ note.dates }</td>
146-
<td className="row-icon edit"></td>
147-
<td className="row-icon archive"></td>
148-
<td className="row-icon delete"></td>
149-
</tr>
150155
`;
156+
let tdEdit = document.createElement('td'),
157+
tdArchive = document.createElement('td'),
158+
tdDelete = document.createElement('td');
159+
160+
tdEdit.className = "row-icon edit";
161+
tdEdit.addEventListener("click", ()=>{ buildForm(note) });
162+
tdEdit.innerHTML = icons.EDIT_ICON;
163+
164+
tdArchive.className = "row-icon archive";
165+
tdArchive.addEventListener("click", ()=>{ changeArchiveState(note) });
166+
tdArchive.innerHTML = (activeNoteTableShown)? icons.UNARCHIVE_ICON : icons.ARCHIVE_ICON;
167+
168+
tdDelete.className = "row-icon delete";
169+
tdDelete.addEventListener("click", ()=>{ deleteNote(note.id) });
170+
tdDelete.innerHTML = icons.DELETE_ICON;
171+
172+
tr.append(tdEdit, tdArchive, tdDelete);
173+
return tr;
174+
151175
}
152176

153177
export {

scripts/functions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function makeRandomID (length) {
1111
export function getDatesFromText(text){
1212
let results = text.match(/[0-9]{1,2}([\-\/ \.])[0-9]{1,2}([\-\/ \.])((19)|(20))[0-9]{2}/g);
1313

14-
if (results.length)
14+
if (results && results.length)
1515
return results;
16-
return;
16+
return [];
1717
}

0 commit comments

Comments
 (0)