1- import { ModalWindowStateService } from './. ./../services/states/modalWindowState.service' ;
1+ import { ModalWindowStateService } from '../../services/states/modalWindowState.service' ;
22import { Component , OnDestroy , OnInit } from '@angular/core' ;
33import { TokensService } from '../../services/messenger/tokens.service' ;
44import { Chat } from '../../types/models/Chat' ;
@@ -12,16 +12,15 @@ import { CommunityType } from '../../types/enums/CommunityType';
1212import { RoutingConstants } from '../../types/constants/RoutingConstants' ;
1313import { UserChatsService } from '../../services/api/user-chats.service' ;
1414import { ValidationService } from '../../services/messenger/validation.service' ;
15- import { SendMessageCommand } from '../../types/requests/SendMessageCommand' ;
1615import * as signalR from '@microsoft/signalr' ;
1716import { EditMessageNotification } from '../../types/models/EditMessageNotification' ;
1817import { DeleteMessageNotification } from '../../types/models/DeleteMessageNotification' ;
19- import { Subject , takeUntil } from 'rxjs' ;
18+ import { firstValueFrom , Subject , takeUntil } from 'rxjs' ;
2019import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours' ;
21- import { UsersService } from 'src/app/services/api/users.service' ;
2220import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand' ;
2321import { DocumentsService } from 'src/app/services/api/documents.service' ;
2422import ApiBaseService from 'src/app/services/api/apiBase.service' ;
23+ import { SendMessageResponse } from '../../types/responses/SendMessageResponse' ;
2524
2625@Component ( {
2726 selector : 'app-chats' ,
@@ -152,25 +151,13 @@ export class ChatsComponent implements OnInit, OnDestroy {
152151 }
153152
154153 onOpenImageClick ( imageLink : string ) : void {
155- this . _modalWindowStateService . setIsModalWindowShowing ( true )
156- this . _modalWindowStateService . setPicture ( imageLink )
154+ this . _modalWindowStateService . setIsModalWindowShowing ( true ) ;
155+ this . _modalWindowStateService . setPicture ( imageLink ) ;
157156 }
158157
159- closeModalWindowrClick ( ) : void {
160- this . _modalWindowStateService . setIsModalWindowShowing ( false )
161- this . _modalWindowStateService . setPictureNull ( )
162- }
163-
164- async uploadFile ( file : File ) {
165- let fileName = null ;
166- if ( file ) {
167- const formData = new FormData ( ) ;
168- formData . append ( 'formFile' , file , file . name ) ;
169- const response = await this . _documentsService . uploadDocument ( formData ) . toPromise ( ) ;
170- this . messageAttachmentUrl = response ?. fileUrl ?? '' ;
171- fileName = response ?. fileName ?? '' ;
172- }
173- return fileName ;
158+ closeModalWindowClick ( ) : void {
159+ this . _modalWindowStateService . setIsModalWindowShowing ( false ) ;
160+ this . _modalWindowStateService . setPictureNull ( ) ;
174161 }
175162
176163 setSignalRMethods ( ) : void {
@@ -285,6 +272,10 @@ export class ChatsComponent implements OnInit, OnDestroy {
285272 }
286273
287274 loadChat ( chatId : string ) : void {
275+ if ( this . activeChatId === chatId ) {
276+ return ;
277+ }
278+
288279 this . activeChatId = chatId ;
289280 this . activeChat = this . chats . filter ( ( x ) => x . chatId === this . activeChatId ) [ 0 ] ;
290281 this . getChatMessages ( this . activeChatId ) ;
@@ -411,7 +402,7 @@ export class ChatsComponent implements OnInit, OnDestroy {
411402 } ) ;
412403 }
413404
414- async onSendMessageClick ( ) {
405+ public async onSendMessageClick ( ) {
415406 const newMessageText = this . messageText . repeat ( 1 ) ; // deep copy
416407
417408 const messageTextValidationResult = this . _validationService . validateField (
@@ -432,7 +423,16 @@ export class ChatsComponent implements OnInit, OnDestroy {
432423
433424 const isoString = new Date ( ) . toISOString ( ) ;
434425 const messageId = crypto . randomUUID ( ) ;
435- const sendMessageCommand = new SendMessageCommand ( this . messageText , this . activeChatId ) ;
426+ const sendMessageFormData = new FormData ( ) ;
427+
428+ sendMessageFormData . append ( 'messageText' , newMessageText ) ;
429+ sendMessageFormData . append ( 'chatId' , this . activeChatId ) ;
430+ sendMessageFormData . append ( 'messageId' , messageId ) ;
431+
432+ if ( this . messageAttachment ) {
433+ sendMessageFormData . append ( 'Attachment' , this . messageAttachment ) ;
434+ }
435+
436436 const newMessage = new Message (
437437 messageId ,
438438 tokens . userId ,
@@ -447,35 +447,22 @@ export class ChatsComponent implements OnInit, OnDestroy {
447447
448448 this . clearMessageInput ( ) ;
449449
450- if ( this . messageAttachment ) {
451- const fileName = await this . uploadFile ( this . messageAttachment ) ;
452- this . messageAttachment = null ;
453- sendMessageCommand . setAttachmentUrl ( fileName ) ;
454- newMessage . setMessageAttachmentUrl ( this . messageAttachmentUrl ) ;
455- }
456-
457450 this . messages . push ( newMessage ) ;
458451
459- sendMessageCommand . setMessageId ( messageId ) ;
460- sendMessageCommand . setCreatedAt ( isoString ) ;
452+ const sendMessage$ = this . _messagesService . sendMessage ( sendMessageFormData ) ;
461453
462- this . _messagesService
463- . sendMessage ( sendMessageCommand )
464- . pipe ( takeUntil ( this . componentDestroyed$ ) )
465- . subscribe ( {
466- next : ( data ) => {
467- newMessage . messageId = data . messageId ;
468- this . scrollToEnd ( ) ;
469- } ,
470- error : ( error ) => {
471- this . _errorNotificationService . notifyOnError ( error ) ;
472- }
473- } ) ;
454+ const response = await firstValueFrom < SendMessageResponse > ( sendMessage$ ) ;
455+
456+ newMessage . messageId = response . messageId ;
457+ newMessage . messageAttachmentUrl = response . attachmentUrl ;
458+
459+ this . clearAttachmentInput ( ) ;
460+ this . scrollToEnd ( ) ;
474461 }
475462
476- onEnterClick ( event : any ) : void {
463+ async onEnterClick ( event : any ) {
477464 event . preventDefault ( ) ;
478- this . onSendMessageClick ( ) . then ( ( r ) => r ) ;
465+ await this . onSendMessageClick ( ) . then ( ( r ) => r ) ;
479466 }
480467
481468 private clearMessageInput ( ) : void {
@@ -543,4 +530,15 @@ export class ChatsComponent implements OnInit, OnDestroy {
543530 this . componentDestroyed$ . next ( true ) ;
544531 this . componentDestroyed$ . complete ( ) ;
545532 }
533+
534+ clearAttachmentInput ( ) {
535+ const fileInput = document . getElementById ( 'attachment' ) as HTMLInputElement ;
536+
537+ if ( ! fileInput ) {
538+ return ;
539+ }
540+
541+ fileInput . value = '' ;
542+ this . messageAttachment = null ;
543+ }
546544}
0 commit comments