Skip to content

Commit

Permalink
Implemented that stock in AddEditBookForm only allows whole numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelPelegrina committed Apr 22, 2024
1 parent 5221a05 commit 17dcd6f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { ButtonGroupComponent } from './components/cart/button-group/button-grou
import { NgxPermissionsModule } from 'ngx-permissions';
import { FooterComponent } from './components/footer/footer.component';
import { BookExcelParserComponent } from './components/book/book-excel-parser/book-excel-parser.component';
import { WholeNumberDirective } from './shared/utils/directives/whole-number.directive';

@NgModule({
declarations: [
Expand Down Expand Up @@ -89,6 +90,7 @@ import { BookExcelParserComponent } from './components/book/book-excel-parser/bo
ViewBookComponent,
FooterComponent,
BookExcelParserComponent,
WholeNumberDirective,
],
imports: [
AppRoutingModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ <h1 class="text-center m-2 p-2" *ngIf="!isAddMode">Edit book</h1>
<!-- STOCK -->
<mat-label>Stock</mat-label>
<input
appWholeNumber
formControlName="stock"
matInput
placeholder="Enter the stock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="col-sm">
<mat-form-field class="form-group w-100" appearance="fill">
<mat-label>Email</mat-label>
<input formControlName="email" matInput placeholder="Enter your email" required type="text">
<input formControlName="email" matInput placeholder="Enter your email" [readonly]="" required type="text" >
<mat-error *ngIf="f['email'].invalid">{{getErrorMessage('email')}}</mat-error>
</mat-form-field>
</div>
Expand Down
14 changes: 0 additions & 14 deletions src/app/components/user/forms/user/add-edit-user-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,5 @@ export class AddEditUserForm extends AbstractForm implements OnDestroy, OnInit {
this.id = this.route.snapshot.params['id'];

this.form = this.rootFormGroup.control.get(this.formGroupName) as FormGroup;

if(this.id){
this.loadUser();
}
}

// Private methods
/**
* Load user data using the user service based on the provided ID.
*/
private loadUser(){
this.userService.getById(this.id!).subscribe((response) => {
this.form.patchValue(response);
})
}
}
31 changes: 31 additions & 0 deletions src/app/shared/utils/directives/whole-number.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Directive, ElementRef, HostListener } from '@angular/core';
import { AbstractControl, NG_VALIDATORS } from '@angular/forms';

@Directive({
selector: '[appWholeNumber]',
})
export class WholeNumberDirective {
private regex: RegExp = new RegExp(/^[0-9]+$/);

private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', '-', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];

constructor(private el: ElementRef) {}

@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
// Allow Backspace, tab, end, and home keys
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}

let current: string = this.el.nativeElement.value;

const position = this.el.nativeElement.selectionStart;

const next: string = [current.slice(0, position), event.key == 'Whole' ? '.' : event.key, current.slice(position)].join('');

if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}

0 comments on commit 17dcd6f

Please sign in to comment.