Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/delete subject: subject removal function #5

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/deleteIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions css/subject.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
border-radius: 1rem;
}

.subject-header {
display: flex;
justify-content: space-between;
}

.add-subject-button {
background: transparent;

Expand All @@ -26,3 +31,16 @@
width: 2rem;
height: 2rem;
}

.delete-subject-button {
background: transparent;

border: none;
outline: none;

cursor: pointer;
}
.delete-subject-icon {
width: 1.5rem;
height: 1.5rem;
}
2 changes: 1 addition & 1 deletion js/constant.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const OPEN = 'open';
const IN_PROGRESS = 'in-progress';
const DONE = 'done';
const COLUMN_LIST = [OPEN, IN_PROGRESS, DONE];
const STATE_LIST = [OPEN, IN_PROGRESS, DONE];
6 changes: 3 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ document.addEventListener('DOMContentLoaded', () => {
taskViewModel.addTask('task 1', doneSubject1.getId());
taskViewModel.addTask('task 2', doneSubject1.getId());

COLUMN_LIST.forEach((column) => {
STATE_LIST.forEach((state) => {
const addSubjectButtonElement = document.getElementById(
`add-subject-button-${column.toLowerCase()}`
`add-subject-button-${state.toLowerCase()}`
);

addSubjectButtonElement.addEventListener('click', () => {
const subjectName = 'New Subject';
subjectViewModel.addSubject(subjectName, column);
subjectViewModel.addSubject(subjectName, state);
});
});
});
36 changes: 29 additions & 7 deletions js/viewModels/subjectViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,55 @@ class SubjectViewModel {
return subject;
}

getSubjectsByColumn(columnId) {
return this.subjectList.get(columnId) || [];
deleteSubject(subjectId, state) {
const subjects = this.subjectList.get(state);
const subjectIndex = subjects.findIndex(
(subject) => subject.getId() === subjectId
);
subjects.splice(subjectIndex, 1);

this.render();
}

getSubjectsByState(state) {
return this.subjectList.get(state) || [];
}

render() {
COLUMN_LIST.forEach((column) => {
STATE_LIST.forEach((state) => {
const subjectListElement = document.getElementById(
`${column}-subject-list`
`${state}-subject-list`
);
subjectListElement.innerHTML = '';

this.getSubjectsByColumn(column).forEach((subject) => {
this.getSubjectsByState(state).forEach((subject) => {
const subjectId = subject.getId();
if (subject.getState() !== column) {
if (subject.getState() !== state) {
return;
}

const subjectElement = document.createElement('li');
subjectElement.classList.add('subject');
subjectElement.innerHTML = `
<h3>${subject.getTitle()}</h3>
<header class="subject-header">
<h3>${subject.getTitle()}</h3>
<button class="delete-subject-button" id="${subjectId}-delete-button">
<img src="assets/deleteIcon.svg" class="delete-subject-icon" />
</button>
</header>
<main>
<ol id=${`${subjectId}-task-list`} />
</main>
`;
subjectListElement.appendChild(subjectElement);

const deleteButtonElement = document.getElementById(
`${subjectId}-delete-button`
);
deleteButtonElement.addEventListener('click', () =>
this.deleteSubject(subjectId, state)
);

this.taskViewModel.render(subjectId);
});
});
Expand Down