From cd4514fd2dde03f4f2da71527b38346dc241306c Mon Sep 17 00:00:00 2001 From: X4Lo Date: Wed, 3 Apr 2024 21:44:58 +0000 Subject: [PATCH] changed style for list selection modal + fixed functions --- .../list-selection-modal.component.css | 36 ++++ .../list-selection-modal.component.html | 57 ++++++ .../list-selection-modal.component.spec.ts | 21 +++ .../list-selection-modal.component.ts | 168 ++++++++++++++++++ 4 files changed, 282 insertions(+) create mode 100644 frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.css create mode 100644 frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.html create mode 100644 frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.spec.ts create mode 100644 frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.ts diff --git a/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.css b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.css new file mode 100644 index 0000000..00e957a --- /dev/null +++ b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.css @@ -0,0 +1,36 @@ +.modal-content { + padding: 20px 20px 0 20px; +} + +.modal-body { + padding-right: 5px; + padding-left: 5px; + margin-bottom: 10px; +} + +.modal-footer { + margin-bottom: 15px; +} + +.modal-input { + padding-right: 10px !important; +} + +.simplebar-content-wrapper { + padding-right: 10px; +} + +.simplebar-content-wrapper { + max-height: 300px; + overflow-y: auto; +} + +.user-lists { + height: 100%; + background-color: var(--bs-body-bg) !important; + border-right: 1px solid var(--bs-border-color); +} + +.user-lists .btn { + color: var(--bs-border-color); +} diff --git a/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.html b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.html new file mode 100644 index 0000000..ece3909 --- /dev/null +++ b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.html @@ -0,0 +1,57 @@ + + \ No newline at end of file diff --git a/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.spec.ts b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.spec.ts new file mode 100644 index 0000000..8cd84ea --- /dev/null +++ b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ListSelectionModalComponent } from './list-selection-modal.component'; + +describe('ListSelectionModalComponent', () => { + let component: ListSelectionModalComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + declarations: [ListSelectionModalComponent] + }); + fixture = TestBed.createComponent(ListSelectionModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.ts b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.ts new file mode 100644 index 0000000..f174c2d --- /dev/null +++ b/frontend/src/app/features/modals/list-selection-modal/list-selection-modal.component.ts @@ -0,0 +1,168 @@ +import { Component, Inject, Input, OnInit, ViewChild } from '@angular/core'; +import { FormControl } from '@angular/forms'; +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; +import { ListService } from '../../../core/services/list.service'; +import { List } from '../../../shared/models/list.model'; +import { NotificationPopupComponent } from '../../../shared/notification-popup/notification-popup.component'; +import { NotificationService } from '../../../shared/services/notification.service'; +import { + trigger, + state, + style, + transition, + animate, +} from '@angular/animations'; + +@Component({ + selector: 'app-list-selection-modal', + templateUrl: './list-selection-modal.component.html', + styleUrls: ['./list-selection-modal.component.css'], + animations: [ + trigger('fadeInOut', [ + state('in', style({ opacity: 1 })), + transition(':enter', [style({ opacity: 0 }), animate(600)]), + transition(':leave', [animate(600, style({ opacity: 0 }))]), + ]), + trigger('scaleInOut', [ + transition(':enter', [ + state('in', style({ opacity: 1 })), + style({ transform: 'scale(0)' }), + animate('200ms ease-in-out', style({ transform: 'scale(1)' })), + ]), + transition(':leave', [ + animate('200ms ease-in-out', style({ transform: 'scale(0)' })), + ]), + ]), + ], +}) +export class ListSelectionModalComponent implements OnInit { + @Input() entryId: number = 0; + @ViewChild(NotificationPopupComponent) + notificationComponent!: NotificationPopupComponent; + searchControl = new FormControl(''); + showAddButton: boolean = false; + + lists: List[] = []; + listsToShow: List[] = []; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private listService: ListService, + private notificationService: NotificationService + ) {} + + ngOnInit() { + this.entryId = this.data.entryId; + this.getLists(); + + this.searchControl.valueChanges.subscribe((value) => { + this.applyFilter(value); + }); + } + + getLists() { + this.listService.getLists().subscribe({ + next: (l) => { + l.forEach((list) => { + if ( + list.listEntries.some((entry) => entry.entry_id == this.entryId) + ) { + list.isChecked = true; + } + }); + + this.lists = l; + this.listsToShow = this.lists; + this.applyFilter(this.searchControl.value); + }, + error: (error) => { + console.log("Counldn't get lists from backend."); + }, + }); + } + + applyFilter(filterValue: string | null) { + if (filterValue == null) { + this.listsToShow = [...this.lists]; // Show all lists if filter is cleared or null + this.showAddButton = false; // Assuming you want to hide the button when there's no filter + return; + } + + const normalizedFilter = filterValue.toLowerCase().trim(); + + this.listsToShow = this.lists.filter((list) => + list.name.toLowerCase().trim().includes(normalizedFilter) + ); + + this.showAddButton = this.listsToShow.length === 0; + } + + createList() { + const listName = this.searchControl.value; // string + + let myNewList = { name: listName } as List; + + this.listService.createList(myNewList).subscribe(async (list) => { + await this.getLists(); + }); + } + + doShowAddButton(): boolean { + return this.showAddButton; + } + + onSelectionChange(list: List, event: Event): void { + const inputElement = event.target as HTMLInputElement; + const isChecked = inputElement.checked; + + if (isChecked || isChecked == null) { + console.log(`Adding Entry ${this.entryId} to list: ${list.name}`); + this.listService + .addEntryToList(list.id, this.entryId, list.listEntries.length + 1) + .subscribe({ + next: (response) => { + console.log('Success:', response); + this.getLists(); + this.showNotification( + 'Entry Added', + 'Entry added successfully', + 'success' + ); + }, + error: (error) => { + console.error('Error:', error); + this.showNotification('Error', 'Failed to add entry', 'error'); + }, + }); + } else { + console.log(`Removing Entry ${this.entryId} from list: ${list.name}`); + this.listService.removeEntryFromList(list.id, this.entryId).subscribe({ + next: (_) => { + this.showNotification( + 'Entry Removed', + 'Entry removed successfully', + 'success' + ); + }, + error: (error) => { + console.error('Error removing entry:', error); + this.showNotification('Error', 'Failed to remove entry', 'error'); + }, + }); + } + } + + closeDialog(): void { + this.dialogRef.close(); + } + + confirmSelection(): void { + this.dialogRef.close(/* Optionally pass back some data */); + } + + showNotification(title: string, message: string, type: string): void { + // this.notificationComponent.showNotification(message, type); + this.notificationService.showNotification(title, message, type); + } +}