All files / app/features/article/pages/version-redirect version-redirect.component.ts

100% Statements 26/26
87.5% Branches 7/8
100% Functions 4/4
100% Lines 24/24

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 561x   1x 1x 1x 1x 1x                 1x 5x 5x 5x 5x 5x   5x     5x 5x   5x 1x 1x 1x     4x         2x       2x     2x       2x          
import { ArticleService } from '../../../../services/articles';
import { HttpErrorResponse } from '@angular/common/http';
import { ChangeDetectionStrategy, Component, DestroyRef, inject, OnInit, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router } from '@angular/router';
import { LoggerService } from '@drevo-web/core';
import { SpinnerComponent } from '@drevo-web/ui';
 
@Component({
    selector: 'app-version-redirect',
    imports: [SpinnerComponent],
    templateUrl: './version-redirect.component.html',
    styleUrl: './version-redirect.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class VersionRedirectComponent implements OnInit {
    private readonly route = inject(ActivatedRoute);
    private readonly router = inject(Router);
    private readonly articleService = inject(ArticleService);
    private readonly destroyRef = inject(DestroyRef);
    private readonly logger = inject(LoggerService).withContext('VersionRedirect');
 
    readonly error = signal<string | undefined>(undefined);
 
    ngOnInit(): void {
        const idParam = this.route.snapshot.paramMap.get('versionId');
        const versionId = idParam ? parseInt(idParam, 10) : NaN;
 
        if (isNaN(versionId) || versionId <= 0) {
            this.error.set('Неверный ID версии');
            this.logger.error('Invalid version ID for redirect', idParam);
            return;
        }
 
        this.articleService
            .getVersionShow(versionId)
            .pipe(takeUntilDestroyed(this.destroyRef))
            .subscribe({
                next: article => {
                    this.logger.info('Redirecting version URL', {
                        versionId,
                        articleId: article.articleId,
                    });
                    this.router.navigate(['/articles', article.articleId, 'version', versionId], { replaceUrl: true });
                },
                error: (err: HttpErrorResponse) => {
                    this.logger.error('Failed to load version for redirect', {
                        versionId,
                        status: err.status,
                    });
                    this.error.set(err.status === 404 ? 'Версия не найдена' : 'Ошибка загрузки версии');
                },
            });
    }
}