mantis-app
Version:
M.A.N.T.I.S (MongoDB, Analog, Nx, Tailwind CSS, Ionic, Storybook) is not just a CLI tool; it's your passport to a seamless full-stack project launch.
144 lines (119 loc) • 4.02 kB
text/typescript
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import type {
IonRefresherCustomEvent,
RefresherEventDetail,
} from '@ionic/core/components';
import { Todo, TodosService } from '../../app/services/todos.service';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { ModalController } from '@ionic/angular';
import { TodoModalComponent } from '../components/todo-modal/todo-modal.component';
import { IonicModule } from '@ionic/angular';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { TodoItemComponent } from '../components/todo-item/todo-item.component';
import { add } from 'ionicons/icons';
import { addIcons } from 'ionicons';
addIcons({ add });
export default class HomePageComponent implements OnInit {
todos$ = new BehaviorSubject<Todo[]>([]);
constructor(
private todosService: TodosService,
private modalCtrl: ModalController,
private cdf: ChangeDetectorRef,
) {}
ngOnInit(): void {
this.loadTodos();
}
loadTodos(): void {
this.todosService.getAllTodos().subscribe((todos) => {
this.todos$.next(todos);
this.cdf.detectChanges();
});
}
removeTodo(todo: Todo): void {
this.todosService.removeTodo(todo).subscribe(() => this.loadTodos());
}
updateTodo(todo: Todo): void {
this.todosService.updateTodo(todo).subscribe(() => this.loadTodos());
}
async openAddTodoModal() {
const modal = await this.modalCtrl.create({
component: TodoModalComponent,
});
modal.present();
const { data, role } = await modal.onWillDismiss();
if (role === 'confirm') {
this.addTodo(data);
}
}
async openEditTodoModal(todo: Todo) {
const modal = await this.modalCtrl.create({
component: TodoModalComponent,
componentProps: {
todo,
},
});
modal.present();
const { data, role } = await modal.onWillDismiss();
if (role === 'confirm') {
this.updateTodo(data);
}
}
addTodo(todo: Todo): void {
this.todosService.addTodo(todo).subscribe(() => this.loadTodos());
}
private refreshing = false;
async refresh(ev: IonRefresherCustomEvent<RefresherEventDetail>) {
if (this.refreshing) return;
this.refreshing = true;
const todosPromise = firstValueFrom(this.todosService.getAllTodos());
const [todos] = await Promise.all([todosPromise, delay(200)]);
ev.detail.complete();
this.todos$.next(todos);
this.refreshing = false;
}
}
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));