diff --git a/frontend/src/app/chat/chat.html b/frontend/src/app/chat/chat.html index d82157a..0410a41 100644 --- a/frontend/src/app/chat/chat.html +++ b/frontend/src/app/chat/chat.html @@ -64,15 +64,14 @@ diff --git a/frontend/src/app/chat/chat.ts b/frontend/src/app/chat/chat.ts index c819e8e..1d0d5bb 100644 --- a/frontend/src/app/chat/chat.ts +++ b/frontend/src/app/chat/chat.ts @@ -1,4 +1,4 @@ -import { Component, ElementRef, ViewChild, effect, inject } from '@angular/core'; +import { Component, ElementRef, ViewChild, effect, inject, signal } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormControl, ReactiveFormsModule } from '@angular/forms'; import { ChatStore, MessageStore } from './chat.store'; @@ -7,21 +7,26 @@ import { Sidebar } from '../core/layout/sidebar/sidebar'; import { Loader } from '../shared/loader/loader'; import { InfiniteScroll } from '../core/directives/infinite-scroll'; import { environment } from '../../environments/environment.development'; +import { form, FormField, max, min, required } from '@angular/forms/signals'; @Component({ selector: 'app-chat', standalone: true, - imports: [CommonModule, ReactiveFormsModule, Sidebar, Loader, InfiniteScroll], + imports: [CommonModule, Sidebar, Loader, InfiniteScroll, FormField], templateUrl: './chat.html', styleUrl: './chat.css', }) export class Chat { protected readonly messageStore = inject(MessageStore); private readonly route = inject(ActivatedRoute); + private messageModel = signal<{ message: string }>({ message: '' }); @ViewChild('scrollContainer') private scrollContainer!: ElementRef; - messageControl = new FormControl(''); + messageForm = form(this.messageModel, (schema) => { + required(schema.message, { message: 'Message is required' }); + min(schema.message, 3, { message: 'Message must be minimum 3 letters' }); + }); constructor() { // Scroll to bottom when messages change @@ -48,7 +53,7 @@ export class Chat { errorMessage = ''; sendMessage() { - const value = this.messageControl.value; + const value = this.messageForm.message().value(); if (value && value.trim() && !this.messageStore.isLoading()) { const words = value.trim().split(/\s+/).length; if (words > 400) { @@ -57,7 +62,7 @@ export class Chat { } this.errorMessage = ''; this.messageStore.sendMessage(value.trim()); - this.messageControl.setValue(''); + this.messageForm.message().value.set(''); } }