In my project, I came across an intriguing case of executing several requests in a pipe chain. It's all about displaying images in the quill text editor.
backend returns content in the form:
content: "<img alt="alt.png" id="1234567890"><p>content</p>"
As you can see, the src attribute is missing in the image. We need 2 extra shots to the api to get it. It is difficult because we operate on the id of the image that is sewn into string.
Incorrect solution for the purposes of understanding the essence only!
ngOnInit(): void {
this.selectedTemplate
.pipe(
map(e => {
const document = new DOMParser().parseFromString(e.content, 'text/html');
document.querySelectorAll('img').forEach(img => {
this._imageService.getImageToken(img.getAttribute('id')).pipe(
switchMap(result => {
return this._imageService.getImageByToken(result.token);
})).subscribe(result => {
img.setAttribute('src', result)
});
});
return {
...e,
content: document.body.innerHTML
}
}),
tap(e => this.form.setValue({ id: e.id, content: e.content, title: e.name })),
)
.subscribe();
}
I want all the data to be fetched before going to the tap operators. As it stands, the solution works, but it looks terrible. Any ideas how to improve this without touching the backend?