Skip to content

Commit

Permalink
js
Browse files Browse the repository at this point in the history
  • Loading branch information
BaderSarah committed Apr 28, 2024
1 parent 6c994ec commit ae6655f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/CoursesComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ export default class CoursesComponent {
#names = [];
#storage;
#coursesRepository;
#courses;

constructor() {
this.#coursesRepository = new CoursesRepository();
this.#storage = window.localStorage;
this.#setCoursesInStorage();
this.#addStorageCoursesToRepo();

this.#printCoursesConsole();

document.getElementById("add").onclick = () => {
Expand All @@ -33,6 +37,7 @@ export default class CoursesComponent {
#addNameCourse(name) {
this.#names.push(name);
this.#setNamesInStorage();
this.#setCoursesInStorage();
}

#toHtml() {
Expand All @@ -44,13 +49,34 @@ export default class CoursesComponent {
}

#setNamesInStorage() {
this.#storage.setItem("namesCourses", JSON.stringify(this.#names));
this.#storage.setItem("namesCourses", this.#names);
}

#setCoursesInStorage() {
this.#storage.setItem(
"courses",
JSON.stringify(this.#coursesRepository.courses)
);
}

#getCoursesInStorage() {
this.#courses = [];
if (this.#storage.getItem("courses")) {
this.#courses = JSON.parse(this.#storage.getItem("courses"));
}
}

#addStorageCoursesToRepo() {
this.#getCoursesInStorage();
this.#courses.forEach((c) => {
this.#coursesRepository.addExistingCourse(c);
});
}

#getNamesInStorage() {
this.#names = [];
if (this.#storage.getItem("namesCourses")) {
this.#names = JSON.parse(this.#storage.getItem("namesCourses"));
this.#names = this.#storage.getItem("namesCourses");
}
}

Expand Down Expand Up @@ -189,8 +215,6 @@ export default class CoursesComponent {
divCourse.appendChild(spanElementSecond);
divCourse.appendChild(btnElement);

// parent div

// parent div
const divParentEl = document.getElementById("courseId");
divParentEl.appendChild(divCourse);
Expand Down
11 changes: 10 additions & 1 deletion src/CoursesRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class CoursesRepository {
}

giveCourse(name) {
return this.#courses.find((c) => c.name == name);
return this.#courses.find((c) => c.name === name);
}

giveAllCoursesNames() {
Expand All @@ -19,11 +19,20 @@ export default class CoursesRepository {
return this.#courses;
}

set courses(courses) {
this.#courses = courses;
}

addExistingCourse(course) {
this.#courses.push(course);
}

addToDoToCourse(name, title, time, urgency) {
const course = this.giveCourse(name);
course.addToDo(title, time, urgency);
}

// non
giveToDosOfCourse(name) {
return this.#courses.find((c) => c.name == name).toDos;
}
Expand Down

0 comments on commit ae6655f

Please sign in to comment.