Files
hv2-all-in-one/ui/hv2-ui/src/app/note/note.component.ts
2021-09-09 18:39:18 +02:00

78 lines
2.4 KiB
TypeScript

import { Component, Input, OnInit, OnChanges, ViewChild } from '@angular/core';
import { MatButton } from '@angular/material/button';
import { MatTableDataSource } from '@angular/material/table';
import { NoteService } from '../data-object-service';
import { Note, NULL_Note } from '../data-objects';
import { MessageService } from '../message.service';
@Component({
selector: 'app-note',
templateUrl: './note.component.html',
styleUrls: ['./note.component.css']
})
export class NoteComponent implements OnInit {
@Input() selectedTenantId: number
@ViewChild('addNoteButton') addNoteButton: MatButton
collapse: boolean = false
notes: Note[]
notesDataSource: MatTableDataSource<Note>
notesDisplayedColumns: string[] = [ "createdAt", "note" ]
newNote : Note = NULL_Note
constructor(
private noteService: NoteService,
private messageService: MessageService
) { }
async getNotes(): Promise<void> {
try {
if (this.selectedTenantId) {
this.messageService.add(`Trying to load note for ${this.selectedTenantId}`)
this.notes = await this.noteService.getNotesByTenant(this.selectedTenantId)
this.notes.reverse()
this.messageService.add(`Notes: ${JSON.stringify(this.notes, undefined, 4)}`)
this.notesDataSource = new MatTableDataSource<Note>(this.notes)
}
} catch (err) {
this.messageService.add(`Error in getNotes: ${JSON.stringify(err, undefined, 4)}`)
}
}
async addNote(): Promise<void> {
try {
this.addNoteButton.disabled = true
this.newNote.tenant = this.selectedTenantId
this.newNote.created_at = new Date().toDateString()
this.messageService.add(`addNote: ${ JSON.stringify(this.newNote, undefined, 4) }`)
this.newNote = await this.noteService.postNote(this.newNote)
this.messageService.add(`New addNote created: ${this.newNote.id}`)
this.newNote = { 'tenant': this.selectedTenantId, 'note': '', 'created_at': '', 'id': 0 }
this.getNotes()
} catch (err) {
this.messageService.add(`Error in addNote: ${JSON.stringify(err, undefined, 4)}`)
} finally {
this.addNoteButton.disabled = false
}
}
ngOnInit(): void {
this.messageService.add(`NoteComponent.ngOnInit, account: ${this.selectedTenantId}`)
this.getNotes()
}
ngOnChanges(): void {
this.messageService.add(`NoteComponent.ngOnChanges, account: ${this.selectedTenantId}`)
this.getNotes()
}
}