All files / app/features/article/components/preview preview.component.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 5/5
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 572x 2x 2x 2x 2x 2x 2x                 2x 6x 6x   6x 6x 6x   6x 6x 6x   6x 6x 6x     4x       4x 4x   4x         3x 3x 3x     1x 1x 1x          
import { ArticleService } from '../../../../services/articles';
import { ErrorComponent } from '../../../../shared/components/error/error.component';
import { ArticleContentComponent } from '../article-content/article-content.component';
import { ChangeDetectionStrategy, Component, DestroyRef, inject, input, OnInit, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { LoggerService } from '@drevo-web/core';
import { SpinnerComponent } from '@drevo-web/ui';
 
@Component({
    selector: 'app-preview',
    templateUrl: './preview.component.html',
    styleUrl: './preview.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [ArticleContentComponent, ErrorComponent, SpinnerComponent],
})
export class PreviewComponent implements OnInit {
    readonly content = input.required<string>();
    readonly articleId = input.required<number>();
 
    private readonly articleService = inject(ArticleService);
    private readonly destroyRef = inject(DestroyRef);
    private readonly logger = inject(LoggerService).withContext('PreviewComponent');
 
    private readonly _previewHtml = signal('');
    private readonly _isLoading = signal(true);
    private readonly _error = signal<string | undefined>(undefined);
 
    readonly previewHtml = this._previewHtml.asReadonly();
    readonly isLoading = this._isLoading.asReadonly();
    readonly error = this._error.asReadonly();
 
    ngOnInit(): void {
        this.loadPreview();
    }
 
    private loadPreview(): void {
        this._isLoading.set(true);
        this._error.set(undefined);
 
        this.articleService
            .previewArticle(this.content(), this.articleId())
            .pipe(takeUntilDestroyed(this.destroyRef))
            .subscribe({
                next: html => {
                    this._previewHtml.set(html);
                    this._isLoading.set(false);
                    this.logger.info('Preview loaded', { articleId: this.articleId() });
                },
                error: err => {
                    this._isLoading.set(false);
                    this._error.set('Не удалось загрузить предпросмотр');
                    this.logger.error('Failed to load preview', err);
                },
            });
    }
}