ciyidogan commited on
Commit
650485d
·
verified ·
1 Parent(s): 85ef17d

Update flare-ui/src/app/components/chat/chat.component.ts

Browse files
flare-ui/src/app/components/chat/chat.component.ts CHANGED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Component, OnDestroy } from '@angular/core';
2
+ import { CommonModule } from '@angular/common';
3
+ import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
4
+ import { MatButtonModule } from '@angular/material/button';
5
+ import { MatIconModule } from '@angular/material/icon';
6
+ import { MatFormFieldModule } from '@angular/material/form-field';
7
+ import { MatInputModule } from '@angular/material/input';
8
+ import { MatCardModule } from '@angular/material/card';
9
+ import { Subscription } from 'rxjs';
10
+
11
+ import { ApiService } from '../services/api.service';
12
+
13
+ interface ChatMessage {
14
+ author: 'user' | 'assistant';
15
+ text: string;
16
+ }
17
+
18
+ @Component({
19
+ selector: 'app-chat',
20
+ standalone: true,
21
+ imports: [
22
+ CommonModule,
23
+ ReactiveFormsModule,
24
+ MatButtonModule,
25
+ MatIconModule,
26
+ MatFormFieldModule,
27
+ MatInputModule,
28
+ MatCardModule
29
+ ],
30
+ templateUrl: './chat.component.html',
31
+ styleUrls: ['./chat.component.scss']
32
+ })
33
+ export class ChatComponent implements OnDestroy {
34
+ /** Backend’ten dönen oturum kimliği */
35
+ sessionId: string | null = null;
36
+
37
+ /** Sohbet geçmişi */
38
+ messages: ChatMessage[] = [];
39
+
40
+ /** Kullanıcı metni */
41
+ input = this.fb.control('', Validators.required);
42
+
43
+ /** Arkaplan istekleri için abonelikleri tutuyoruz */
44
+ private subs = new Subscription();
45
+
46
+ constructor(private fb: FormBuilder, private api: ApiService) {}
47
+
48
+ /** <Start Chat> butonu */
49
+ startChat(): void {
50
+ const sub = this.api.startChat().subscribe({
51
+ next: (res: any) => {
52
+ this.sessionId = res.session_id;
53
+ },
54
+ error: () => {
55
+ alert('Chat başlatılamadı - tekrar deneyin.');
56
+ }
57
+ });
58
+ this.subs.add(sub);
59
+ }
60
+
61
+ /** <Send> butonu */
62
+ send(): void {
63
+ if (!this.sessionId || this.input.invalid) return;
64
+
65
+ const text = this.input.value!.trim();
66
+ if (!text) return;
67
+
68
+ // Önce kullanıcı mesajını ekranda göster
69
+ this.messages.push({ author: 'user', text });
70
+ this.input.reset();
71
+
72
+ const sub = this.api.chat(this.sessionId, text).subscribe({
73
+ next: (res: any) => {
74
+ this.messages.push({ author: 'assistant', text: res.text });
75
+ },
76
+ error: () => {
77
+ this.messages.push({
78
+ author: 'assistant',
79
+ text: '❗️ Mesaj iletilemedi, lütfen tekrar deneyin.'
80
+ });
81
+ }
82
+ });
83
+ this.subs.add(sub);
84
+ }
85
+
86
+ ngOnDestroy(): void {
87
+ this.subs.unsubscribe();
88
+ }
89
+ }