Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
asliayk committed Nov 25, 2024
1 parent dcbd9d2 commit 2a8e4bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
HostListener,
Inject,
Input,
OnDestroy,
Output,
Renderer2,
ViewChild,
Expand Down Expand Up @@ -34,7 +35,7 @@ import { AnswerPostReactionsBarComponent } from 'app/shared/metis/posting-reacti
]),
],
})
export class AnswerPostComponent extends PostingDirective<AnswerPost> {
export class AnswerPostComponent extends PostingDirective<AnswerPost> implements OnDestroy {
@Input() lastReadDate?: dayjs.Dayjs;
@Input() isLastAnswer: boolean;
@Output() openPostingCreateEditModal = new EventEmitter<void>();
Expand Down Expand Up @@ -101,15 +102,18 @@ export class AnswerPostComponent extends PostingDirective<AnswerPost> {

onRightClick(event: MouseEvent) {
const targetElement = event.target as HTMLElement;
const isPointerCursor = window.getComputedStyle(targetElement).cursor === 'pointer';
let isPointerCursor = false;
try {
isPointerCursor = window.getComputedStyle(targetElement).cursor === 'pointer';
} catch (error) {
console.error('Failed to compute style:', error);
}

if (!isPointerCursor) {
event.preventDefault();

if (AnswerPostComponent.activeDropdownPost && AnswerPostComponent.activeDropdownPost !== this) {
AnswerPostComponent.activeDropdownPost.showDropdown = false;
AnswerPostComponent.activeDropdownPost.enableBodyScroll();
AnswerPostComponent.activeDropdownPost.changeDetector.detectChanges();
if (AnswerPostComponent.activeDropdownPost !== this) {
AnswerPostComponent.cleanupActiveDropdown();
}

AnswerPostComponent.activeDropdownPost = this;
Expand All @@ -133,4 +137,19 @@ export class AnswerPostComponent extends PostingDirective<AnswerPost> {
this.dropdownPosition.x = screenWidth - dropdownWidth - 10;
}
}

private static cleanupActiveDropdown(): void {
if (AnswerPostComponent.activeDropdownPost) {
AnswerPostComponent.activeDropdownPost.showDropdown = false;
AnswerPostComponent.activeDropdownPost.enableBodyScroll();
AnswerPostComponent.activeDropdownPost.changeDetector.detectChanges();
AnswerPostComponent.activeDropdownPost = null;
}
}

ngOnDestroy(): void {
if (AnswerPostComponent.activeDropdownPost === this) {
AnswerPostComponent.cleanupActiveDropdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe('AnswerPostComponent', () => {
});
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should contain an answer post header when isConsecutive is false', () => {
runInInjectionContext(fixture.debugElement.injector, () => {
component.isConsecutive = input<boolean>(false);
Expand Down Expand Up @@ -209,13 +213,6 @@ describe('AnswerPostComponent', () => {
expect(preventDefaultSpy).toHaveBeenCalledTimes(preventDefaultCalled ? 1 : 0);
expect(component.showDropdown).toBe(showDropdown);
expect(component.dropdownPosition).toEqual(dropdownPosition);

afterEach(() => {
jest.restoreAllMocks();
});

testCases.forEach(({ cursor, preventDefaultCalled, showDropdown, dropdownPosition }) => {
// ... test implementation ...
});
});
});
});

0 comments on commit 2a8e4bb

Please sign in to comment.