diff --git a/src/app/materilModule.ts b/src/app/materilModule.ts index beebe9f..9ba7a88 100644 --- a/src/app/materilModule.ts +++ b/src/app/materilModule.ts @@ -11,6 +11,7 @@ import { MatTableModule } from "@angular/material/table"; import { MatSelectModule } from "@angular/material/select"; import { MatAutocompleteModule } from '@angular/material/autocomplete'; import { MatPaginatorModule } from '@angular/material/paginator'; +import {MatSnackBarModule} from '@angular/material/snack-bar'; @NgModule({ imports: [ @@ -25,7 +26,8 @@ import { MatPaginatorModule } from '@angular/material/paginator'; MatSelectModule, MatOptionModule, MatAutocompleteModule, - MatPaginatorModule + MatPaginatorModule , + MatSnackBarModule ], exports: [ MatToolbarModule, @@ -39,7 +41,8 @@ import { MatPaginatorModule } from '@angular/material/paginator'; MatSelectModule, MatOptionModule, MatAutocompleteModule, - MatPaginatorModule + MatPaginatorModule, + MatSnackBarModule ] }) export class MaterialModule { } diff --git a/src/app/rooms/room-add/room-add.component.css b/src/app/rooms/room-add/room-add.component.css index e5d0fb8..4a1fe6c 100644 --- a/src/app/rooms/room-add/room-add.component.css +++ b/src/app/rooms/room-add/room-add.component.css @@ -1,4 +1,69 @@ -.form-group .form-control { - margin-top: 5px; - margin-bottom: 5px; -} \ No newline at end of file +/* Form Container */ +form { + max-width: 600px; + margin: 20px auto; + padding: 20px; + border: 1px solid #ccc; + border-radius: 8px; + background-color: #f9f9f9; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} + +/* Form Group */ +form div { + margin-bottom: 15px; +} + +/* Labels */ +label { + display: block; + margin-bottom: 5px; + font-weight: bold; + color: #333; +} + +/* Input Fields */ +input { + width: 100%; + padding: 8px; + margin-bottom: 5px; + border: 1px solid #ccc; + border-radius: 4px; + box-sizing: border-box; + font-size: 1em; + color: black; +} + +input:focus { + border-color: #007BFF; + outline: none; +} + +/* Error Messages */ +.error-message { + color: red; + font-size: 0.875em; + margin-top: 5px; +} + +/* Submit Button */ +button[type="submit"] { + width: 100%; + padding: 10px; + background-color: #007BFF; + color: white; + border: none; + border-radius: 4px; + cursor: pointer; + font-size: 1em; + transition: background-color 0.3s ease; +} + +button[type="submit"]:disabled { + background-color: #ccc; + cursor: not-allowed; +} + +button[type="submit"]:hover:not(:disabled) { + background-color: #0056b3; +} diff --git a/src/app/rooms/room-add/room-add.component.html b/src/app/rooms/room-add/room-add.component.html index fc35ad4..6fe8f26 100644 --- a/src/app/rooms/room-add/room-add.component.html +++ b/src/app/rooms/room-add/room-add.component.html @@ -1,50 +1,69 @@ -
-
- - -
-

Please fill name!

-
+ +
+ + +
+
First Name is required
+
First Name must be 4-15 characters long and contain only letters
+
-
- - -
-

Please fill isOn

-
+
+ + +
+
Last Name is required
+
Last Name must be 4-15 characters long and contain only letters
+
-
- - -
-

Please fill temperature

-
+
+ + +
+
Email is required
+
Email must be a valid email address
+
- +
+ + +
+
NIC is required
+
NIC must be 9 digits followed by 'V' or 'v'
+
+
-

{{ f.value | json }}

+
+ + +
+
Number of Days is required
+
Number of Days must be 1-2 digits
+
+
- \ No newline at end of file + + + diff --git a/src/app/rooms/room-add/room-add.component.ts b/src/app/rooms/room-add/room-add.component.ts index cd67e36..d31150f 100644 --- a/src/app/rooms/room-add/room-add.component.ts +++ b/src/app/rooms/room-add/room-add.component.ts @@ -1,28 +1,78 @@ -import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { Component, OnInit } from '@angular/core'; import { room } from '../../shared/interface/room'; @Component({ selector: 'app-room-add', templateUrl: './room-add.component.html', - styleUrl: './room-add.component.css' + styleUrls: ['./room-add.component.css'] }) -export class RoomAddComponent implements OnInit{ - constructor(private ref: ElementRef) { } - @ViewChild('f') form: any; - b: boolean = false; - roomModel: room = {}; - ngOnInit(): void {} +export class RoomAddComponent implements OnInit { + + roomDetailsForm!: FormGroup; + submitted: boolean = false; + isLoading: boolean = false; + + get f() { + return this.roomDetailsForm.controls; + } + + roomModel: room = {}; + + constructor(private formBuilder: FormBuilder) { } + + ngOnInit(): void { + this.initForm(); + } + + initForm(): void { + + this.roomDetailsForm = this.formBuilder.group({ + firstName: [ + '', + [Validators.required, Validators.pattern('[A-Za-z]{4,15}$')] + ], + lastName: [ + '', + [Validators.required, Validators.pattern('[A-Za-z]{4,15}$')] + ], + email: [ + '', + [Validators.required, Validators.pattern('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')] + ], + nic: [ + '', + [Validators.required, Validators.pattern('[0-9]{9}[V|v]$')] + ], + numOfDays: [ + '', + [Validators.required, Validators.pattern('[0-9]{1,2}$')] + ] + }); + } onSubmit(): void { - if (this.form.invalid) { - alert('please enter valid data!'); - return; - } - //pass the form object to backend via the service - alert('success'); - console.log(this.form.controls); + this.submitted = true; + + //console.log(this.roomDetailsForm.firstName); - //reset form value after hit the submit button - this.form.reset(); + + if (this.roomDetailsForm.valid) { + this.isLoading = true; + + // Simulating an API call with a timeout + setTimeout(() => { + console.log('Response'); + this.isLoading = false; + this.clearForm(); + }, 3000); + } } + + clearForm(): void { + this.submitted = false; + this.roomDetailsForm.reset(); + } + + } diff --git a/src/app/shared/interface/room.ts b/src/app/shared/interface/room.ts index b1211e2..1cb5d6e 100644 --- a/src/app/shared/interface/room.ts +++ b/src/app/shared/interface/room.ts @@ -1,8 +1,10 @@ export interface room{ id?:number, - name?: string, - isOn?: boolean, - temperature?: number, + firstName?: string, + lastName?:string, + nic?: string, + numOfDays?: number, + email?:string }