All files / app/shared/components/topics-sidebar-action topics-sidebar-action.component.ts

100% Statements 67/67
95% Branches 19/20
100% Functions 15/15
100% Lines 63/63

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 120 1214x 4x 4x 4x 4x 4x 4x 4x 4x 4x                               4x 28x 28x   28x   28x 28x 28x 28x 28x   28x 28x   28x 24x 24x 3x   21x 21x     28x 28x 24x 24x     28x 24x 41x     28x 28x   28x 28x   28x 28x   28x     12x 12x 11x   12x       1x       3x 3x 3x 2x   1x   3x         4x 4x 4x   4x     4x         3x 3x 3x 3x     1x 1x          
import { ArticleService } from '../../../services/articles';
import { AuthService } from '../../../services/auth/auth.service';
import { SidebarActionComponent } from '../sidebar-action/sidebar-action.component';
import { ChangeDetectionStrategy, Component, computed, DestroyRef, inject, input, output, signal } from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { FormsModule } from '@angular/forms';
import { LoggerService, NotificationService } from '@drevo-web/core';
import { getTopicsByIds, TOPICS } from '@drevo-web/shared';
import { ButtonComponent, CheckboxComponent, IconComponent, SidePanelComponent } from '@drevo-web/ui';
import { finalize } from 'rxjs';
 
@Component({
    selector: 'app-topics-sidebar-action',
    imports: [
        FormsModule,
        SidebarActionComponent,
        SidePanelComponent,
        CheckboxComponent,
        ButtonComponent,
        IconComponent,
    ],
    templateUrl: './topics-sidebar-action.component.html',
    styleUrl: './topics-sidebar-action.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TopicsSidebarActionComponent {
    readonly articleId = input.required<number>();
    readonly topics = input.required<ReadonlyArray<number>>();
 
    readonly topicsChanged = output<ReadonlyArray<number>>();
 
    private readonly authService = inject(AuthService);
    private readonly articleService = inject(ArticleService);
    private readonly destroyRef = inject(DestroyRef);
    private readonly logger = inject(LoggerService).withContext('TopicsSidebarAction');
    private readonly notification = inject(NotificationService);
 
    private readonly user = toSignal(this.authService.user$);
    readonly canModerate = computed(() => this.user()?.permissions.canModerate ?? false);
 
    readonly firstTopicIcon = computed(() => {
        const topicIds = this.topics();
        if (topicIds.length === 0) {
            return undefined;
        }
        const matched = getTopicsByIds(topicIds);
        return matched.length > 0 ? matched[0].icon : undefined;
    });
 
    readonly topicCount = computed(() => this.topics().length);
    readonly topicBadge = computed(() => {
        const count = this.topicCount();
        return count > 1 ? count : undefined;
    });
 
    readonly topicsLabel = computed(() => {
        const matched = getTopicsByIds(this.topics());
        return matched.length > 0 ? matched.map(t => t.name).join('\n') : 'Укажите словник';
    });
 
    private readonly _isPanelOpen = signal(false);
    readonly isPanelOpen = this._isPanelOpen.asReadonly();
 
    private readonly _isSaving = signal(false);
    readonly isSaving = this._isSaving.asReadonly();
 
    private readonly _selectedTopics = signal<ReadonlySet<number>>(new Set());
    readonly selectedTopics = this._selectedTopics.asReadonly();
 
    protected readonly allTopics = TOPICS;
 
    togglePanel(): void {
        const isOpen = this._isPanelOpen();
        if (!isOpen) {
            this._selectedTopics.set(new Set(this.topics()));
        }
        this._isPanelOpen.set(!isOpen);
    }
 
    closePanel(): void {
        this._isPanelOpen.set(false);
    }
 
    onTopicToggle(topicId: number, checked: boolean): void {
        this._selectedTopics.update(current => {
            const next = new Set(current);
            if (checked) {
                next.add(topicId);
            } else {
                next.delete(topicId);
            }
            return next;
        });
    }
 
    save(): void {
        this._isSaving.set(true);
        const articleId = this.articleId();
        const topics = [...this._selectedTopics()];
 
        this.articleService
            .updateTopics(articleId, topics)
            .pipe(
                finalize(() => this._isSaving.set(false)),
                takeUntilDestroyed(this.destroyRef),
            )
            .subscribe({
                next: updatedTopics => {
                    this._isPanelOpen.set(false);
                    this.topicsChanged.emit(updatedTopics);
                    this.notification.success('Словники сохранены');
                    this.logger.info('Topics updated', { articleId, topics: updatedTopics });
                },
                error: (err: unknown) => {
                    this.notification.error('Не удалось сохранить словники');
                    this.logger.error('Failed to update topics', err);
                },
            });
    }
}