All files / app/features/article/pages/article-page/tabs/article-version-tab article-version-tab.component.ts

93.44% Statements 57/61
65% Branches 13/20
69.23% Functions 9/13
96.49% Lines 55/57

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 1x 1x 1x   1x 1x 1x 1x   1x 1x                 1x 10x 10x 10x 10x 10x 10x   10x 10x 10x   10x 10x 10x   10x 5x 5x     10x 5x 5x                       10x     10x 10x           10x 1x 1x 1x 1x 1x     9x         9x 9x 9x   9x         6x 6x 1x         1x     1x     5x 5x 5x           2x 2x       2x 2x          
import { ArticleService } from '../../../../../../services/articles';
import { ArticleContentComponent } from '../../../../components/article-content/article-content.component';
import { ArticleSidebarActionsComponent } from '../../../../components/article-sidebar-actions/article-sidebar-actions.component';
import { ArticlePageService } from '../../../../services/article-page.service';
import { HttpErrorResponse } from '@angular/common/http';
import { ChangeDetectionStrategy, Component, computed, DestroyRef, inject, OnInit, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { LoggerService } from '@drevo-web/core';
import { ArticleVersion, ModerationResult } from '@drevo-web/shared';
import { FormatTimePipe, SpinnerComponent, StatusIconComponent } from '@drevo-web/ui';
import { distinctUntilChanged, map } from 'rxjs/operators';
 
@Component({
    selector: 'app-article-version-tab',
    imports: [ArticleContentComponent, ArticleSidebarActionsComponent, RouterLink, FormatTimePipe, StatusIconComponent, SpinnerComponent],
    templateUrl: './article-version-tab.component.html',
    styleUrl: './article-version-tab.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ArticleVersionTabComponent implements OnInit {
    private readonly route = inject(ActivatedRoute);
    private readonly router = inject(Router);
    private readonly articleService = inject(ArticleService);
    private readonly pageService = inject(ArticlePageService);
    private readonly destroyRef = inject(DestroyRef);
    private readonly logger = inject(LoggerService).withContext('ArticleVersionTab');
 
    private readonly _version = signal<ArticleVersion | undefined>(undefined);
    private readonly _isLoading = signal(false);
    private readonly _error = signal<string | undefined>(undefined);
 
    readonly version = this._version.asReadonly();
    readonly isLoading = this._isLoading.asReadonly();
    readonly error = this._error.asReadonly();
 
    readonly articleUrl = computed(() => {
        const id = this.pageService.articleId();
        return id ? `/articles/${id}` : undefined;
    });
 
    readonly versionEditUrl = computed(() => {
        const v = this.version();
        return v ? `/articles/${v.articleId}/version/${v.versionId}/edit` : undefined;
    });
 
    onModerated(result: ModerationResult): void {
        this._version.update(v => (v ? { ...v, approved: result.approved, comment: result.comment } : v));
    }
 
    onTopicsChanged(topics: ReadonlyArray<number>): void {
        this._version.update(v => (v ? { ...v, topics } : v));
    }
 
    ngOnInit(): void {
        this.route.paramMap
            .pipe(
                map(params => {
                    const idParam = params.get('versionId');
                    return idParam ? parseInt(idParam, 10) : NaN;
                }),
                distinctUntilChanged(),
                takeUntilDestroyed(this.destroyRef)
            )
            .subscribe(versionId => {
                if (isNaN(versionId) || versionId <= 0) {
                    this._version.set(undefined);
                    this._error.set('Неверный ID версии');
                    this._isLoading.set(false);
                    this.logger.error('Invalid version ID', versionId);
                    return;
                }
 
                this.loadVersion(versionId);
            });
    }
 
    private loadVersion(versionId: number): void {
        this._version.set(undefined);
        this._isLoading.set(true);
        this._error.set(undefined);
 
        this.articleService
            .getVersionShow(versionId)
            .pipe(takeUntilDestroyed(this.destroyRef))
            .subscribe({
                next: version => {
                    const expectedArticleId = this.pageService.articleId();
                    if (expectedArticleId && version.articleId !== expectedArticleId) {
                        this.logger.info('Version belongs to different article, redirecting', {
                            versionId: version.versionId,
                            urlArticleId: expectedArticleId,
                            actualArticleId: version.articleId,
                        });
                        this.router.navigate(['/articles', version.articleId, 'version', version.versionId], {
                            replaceUrl: true,
                        });
                        return;
                    }
 
                    this._version.set(version);
                    this._isLoading.set(false);
                    this.logger.info('Version loaded', {
                        articleId: version.articleId,
                        versionId: version.versionId,
                    });
                },
                error: (err: HttpErrorResponse) => {
                    this._version.set(undefined);
                    this.logger.error('Failed to load version', {
                        versionId,
                        status: err.status,
                    });
                    this._error.set(err.status === 404 ? 'Версия не найдена' : 'Ошибка загрузки версии');
                    this._isLoading.set(false);
                },
            });
    }
}