library/src/app/modals/issue-book-modal/issue-book-modal.page.ts

81 lines
1.9 KiB
TypeScript

import { Component, Input, ViewChild } from '@angular/core';
import { IonModal } from '@ionic/angular';
import {
bookIssued,
readerUser,
StorageService,
} from '../../services/storage/storage.service';
@Component({
selector: 'app-issue-book-modal',
templateUrl: './issue-book-modal.page.html',
styleUrls: ['./issue-book-modal.page.scss'],
})
export class IssueBookModalPage {
@ViewChild(IonModal) modal: IonModal | undefined;
@Input() trigger!: string;
constructor(private storage: StorageService) {}
cancel() {
this.modal?.dismiss(null, 'cancel');
}
protected user: readerUser | undefined = undefined;
public userId = 0;
public userFio = '';
public date = '';
onWillDismiss(event: Event) {}
private updater(user: readerUser | undefined) {
if (user !== null && user !== undefined) {
this.user = user;
this.userId = user.id;
this.userFio = user.fio;
} else {
user = undefined;
}
}
idChanged(event: any) {
this.storage.init().then(() => {
this.storage
.searchReaderUserById(parseInt(event.detail.value))
.then((user) => this.updater(user));
});
}
fioChanged(event: any) {
this.storage.init().then(() => {
this.storage
.searchReaderUser(event.detail.value)
.then((user) => this.updater(user));
});
}
dateChanged(event: any) {
this.date = new Date(event.detail.value).toString();
}
issueBook() {
if (this.user === undefined || this.user === null) return;
let _date = new Date();
const issue: bookIssued = {
userId: this.user.id,
bookId: this.trigger,
date: _date.toString(),
dateTo: this.date,
};
this.storage.addBookIssued(issue).then((r) => {
this.modal?.dismiss(null, 'confirm');
});
}
changed(e: any) {
console.log(e.detail.value);
}
protected readonly open = open;
}