Skip to content

Commit

Permalink
improve code safety
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliiakaras committed Dec 12, 2024
1 parent 23b682d commit 95308df
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
import { AbstractControl, FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
import { OrderService } from 'src/app/ubs/ubs-admin/services/order.service';
import { OrderStatus, PaymnetStatus } from 'src/app/ubs/ubs/order-status.enum';
import { Masks, Patterns } from 'src/assets/patterns/patterns';
Expand Down Expand Up @@ -58,7 +58,7 @@ export class UbsAdminOrderDetailsFormComponent implements OnInit, OnChanges {

constructor(private readonly orderService: OrderService) {}

get customerComment() {
get customerComment(): AbstractControl {
return this.orderDetailsForm.get('customerComment');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TableCellInputComponent } from './table-cell-input.component';
import { AdminTableService } from '@ubs/ubs-admin/services/admin-table.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
import { IAlertInfo, IEditCell } from '@ubs/ubs-admin/models/edit-cell.model';
import { IColumnBelonging } from '@ubs/ubs-admin/models/ubs-admin.interface';
import { AdminTableService } from '@ubs/ubs-admin/services/admin-table.service';
import { take } from 'rxjs';
import { catchError, of, take } from 'rxjs';
import { CommentPopUpComponent } from '../../shared/components/comment-pop-up/comment-pop-up.component';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { LocalStorageService } from '@global-service/localstorage/local-storage.service';
Expand All @@ -27,7 +27,7 @@ export class TableCellInputComponent {
isBlocked: boolean;

private typeOfChange: number[];
private font = '12px Lato, sans-serif';
private readonly font = '12px Lato, sans-serif';

private dialogConfig = new MatDialogConfig();

Expand All @@ -44,16 +44,21 @@ export class TableCellInputComponent {
this.typeOfChange = this.adminTableService.howChangeCell(this.isAllChecked, this.ordersToChange, this.id);
this.adminTableService
.blockOrders(this.typeOfChange)
.pipe(take(1))
.subscribe((res: IAlertInfo[]) => {
if (res[0] === undefined) {
.pipe(
take(1),
catchError(() => {
this.isBlocked = false;
this.isEditable = true;
this.openPopUp();
} else {
this.isEditable = false;
this.isBlocked = false;
return of([]);
})
)
.subscribe((res: IAlertInfo[]) => {
this.isBlocked = false;
if (res && res[0]) {
this.showBlockedInfo.emit(res);
} else {
this.isEditable = true;
this.openPopUp();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { MatTooltipModule } from '@angular/material/tooltip';
import { ServerTranslatePipe } from 'src/app/shared/translate-pipe/translate-pipe.pipe';
import { TableCellReadonlyComponent } from './table-cell-readonly.component';
import { Language } from 'src/app/main/i18n/Language';
import { TableKeys } from '../../../services/table-keys.enum';
import { PaymnetStatus } from '../../../../ubs/order-status.enum';
import { TableKeys } from '@ubs/ubs-admin/services/table-keys.enum';
import { PaymnetStatus } from '@ubs/ubs/order-status.enum';
import { AdminTableService } from '@ubs/ubs-admin/services/admin-table.service';

describe('TableCellReadonlyComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class TableCellReadonlyComponent implements OnInit, OnChanges {
halfpaid: boolean;
dataObj: IColumnBelonging = null;
data: string | number | { ua: string; en: string } | null;
private font = '12px Lato, sans-serif';
private readonly font = '12px Lato, sans-serif';
typeof: any;

constructor(private adminTableService: AdminTableService) {}
Expand Down
39 changes: 21 additions & 18 deletions src/app/ubs/ubs-admin/services/admin-table.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,13 @@ describe('AdminTableService', () => {
});

it('should not show tooltip if textContainerWidth is greater than or equal to textWidth', () => {
const event = {
target: {
offsetWidth: 100,
innerText: 'Short text'
}
};
const event = new MouseEvent('mouseover');
spyOn(event, 'stopImmediatePropagation');
Object.defineProperty(event, 'target', {
value: { innerText: 'Short text' },
writable: true
});

const tooltip = {
show: jasmine.createSpy('show')
};
Expand All @@ -492,12 +493,13 @@ describe('AdminTableService', () => {
});

it('should not show tooltip if the difference between textContainerWidth and textWidth is greater than or equal to maxLength', () => {
const event = {
target: {
offsetWidth: 120,
innerText: 'Long text'
}
};
const event = new MouseEvent('mouseover');
spyOn(event, 'stopImmediatePropagation');
Object.defineProperty(event, 'target', {
value: { innerText: 'Short text' },
writable: true
});

const tooltip = {
show: jasmine.createSpy('show')
};
Expand All @@ -510,12 +512,13 @@ describe('AdminTableService', () => {
});

it('should hide tooltip if lengthStr is not greater than maxLength', () => {
const event = {
stopImmediatePropagation: jasmine.createSpy('stopImmediatePropagation'),
target: {
innerText: 'Short text'
}
};
const event = new MouseEvent('mouseover');
spyOn(event, 'stopImmediatePropagation');
Object.defineProperty(event, 'target', {
value: { innerText: 'Short text' },
writable: true
});

const tooltip = {
toggle: jasmine.createSpy('toggle'),
hide: jasmine.createSpy('hide')
Expand Down
12 changes: 7 additions & 5 deletions src/app/ubs/ubs-admin/services/admin-table.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,22 +355,24 @@ export class AdminTableService {
this.filters = this.filters.filter((filteredElem) => !filteredElem[colName]);
}

showTooltip(event: any, tooltip: any, font: string, maxLength = 50): void {
showTooltip(event: MouseEvent, tooltip: any, font: string, maxLength = 50): void {
event.stopImmediatePropagation();
const lengthStr = event.target?.innerText.split('').length;
const target = event.target as HTMLElement;
const lengthStr = target?.innerText.split('').length;
if (lengthStr > maxLength) {
tooltip.toggle();
}

event.type === MouseEvents.MouseEnter ? this.calculateTextWidth(event, tooltip, font) : tooltip.hide();
}

calculateTextWidth(event: any, tooltip: any, font: string, maxLength = 40): void {
const textContainerWidth = event.target.offsetWidth;
calculateTextWidth(event: MouseEvent, tooltip: any, font: string, maxLength = 40): void {
const target = event.target as HTMLElement;
const textContainerWidth = target?.offsetWidth;
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
context.font = font;
const textWidth = Math.round(context.measureText(event.target.innerText).width);
const textWidth = Math.round(context.measureText(target?.innerText).width);
if (textContainerWidth < textWidth || Math.abs(textContainerWidth - textWidth) < maxLength) {
tooltip.show();
}
Expand Down

0 comments on commit 95308df

Please sign in to comment.