Is this the best way to do this? I'm using effect/constructor because that's what chat GPT spat out at me
Is there a way to do this using input.required<string>() or do I just need to use activeRoute.snapshot.params['id'] ?
export class BookUpdate {
private bookService = inject(BookService)
private router = inject(Router)
private fb = inject(FormBuilder)
id = input.required<string>()
bookData = signal<any>(null);
Using bookId = Number(this.activeRoute.snapshot.params['id'])
book = rxResource({
stream: () => this.bookService.fetchBookById(this.bookId)
BookForm: FormGroup = this.fb.group({
title: ['', Validators.required],
author: ['', Validators.required],
yearPublished: [null]
})
constructor() {
const bookSignal = toSignal(this.bookService.fetchBookById(Number(this.id())))
effect(() => {
const data = bookSignal();
if (data) {
this.bookData.set(data);
this.BookForm.setValue({
title: data.title,
author: data.author,
yearPublished: data.yearPublished
});
}
});
}
handleUpdate() {
const book = this.BookForm.value as UpdateBookDto
this.bookService.updateBook(Number(this.id()), book).subscribe({
next: () => this.router.navigateByUrl('/books')
})
}
}```
