created list selection modal component + logic

This commit is contained in:
X4Lo committed 2024-04-02 01:18:15 +00:00
1 parent 863f6bbf11
commit ef920d14e7
4 files changed
+120

No files matched your search

@@ -0,0 +1,3 @@
mat-form-field {
width: 100%;
}
@@ -0,0 +1,19 @@
<h2 mat-dialog-title>Choose a list</h2>
<mat-dialog-content>
<mat-form-field appearance="fill">
<mat-label>List Name</mat-label>
<input [formControl]="searchControl" matInput>
</mat-form-field>
<mat-dialog-actions align="center" *ngIf="doShowAddButton()">
<button mat-button (click)="createList()" *ngIf="doShowAddButton()">Create List</button>
</mat-dialog-actions>
<mat-selection-list #shoes>
<mat-list-option *ngFor="let list of listsToShow" [value]="list">
{{list.name}}
</mat-list-option>
</mat-selection-list>
</mat-dialog-content>
<mat-dialog-actions align="end">
<button mat-button [mat-dialog-close]="true">OK</button>
<button mat-button mat-dialog-close>Cancel</button>
</mat-dialog-actions>
@@ -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<ListSelectionModalComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ListSelectionModalComponent]
});
fixture = TestBed.createComponent(ListSelectionModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,77 @@
import { Component, Inject, OnInit } 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';
@Component({
selector: 'app-list-selection-modal',
templateUrl: './list-selection-modal.component.html',
styleUrls: ['./list-selection-modal.component.css'],
})
export class ListSelectionModalComponent implements OnInit {
searchControl = new FormControl('');
showAddButton: boolean = false;
lists: List[] = [];
listsToShow: List[] = [];
constructor(
public dialogRef: MatDialogRef<ListSelectionModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private listService: ListService
) {}
ngOnInit() {
this.getLists();
this.searchControl.valueChanges.subscribe((value) => {
this.applyFilter(value);
});
}
getLists() {
this.listService.getLists().subscribe({
next: (l) => {
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((collection) =>
collection.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();
console.log(this.searchControl.value);
});
}
doShowAddButton(): boolean {
return this.showAddButton;
}
}