File size: 4,446 Bytes
e4ab6d0
 
 
04830fa
 
 
 
e4ab6d0
 
 
 
04830fa
 
 
e4ab6d0
 
 
 
 
 
 
 
 
04830fa
 
 
e4ab6d0
 
 
 
 
 
 
 
 
04830fa
e4ab6d0
 
 
 
 
 
 
 
 
04830fa
e4ab6d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04830fa
e4ab6d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04830fa
e4ab6d0
 
 
 
04830fa
 
 
 
 
 
 
 
 
e4ab6d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Component } from '@angular/core';
import { WritingService } from './writing.service';
import { Router } from '@angular/router';
import { HeaderComponent } from '../shared/header/header.component';
import { ButtonComponent } from '../shared/button/button.component';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-writing',
  templateUrl: './writing.component.html',
  styleUrls: ['./writing.component.css'],
  standalone: true,
  imports: [ButtonComponent, CommonModule, FormsModule, HeaderComponent]
})
export class WritingComponent {
  gradeLevel = 'lower';
  topic: string | null = null;
  errorMessage: string | null = null;
  userAnswer: string = '';
  feedback: string | null = null;
  isFeedbackVisible: boolean = false;
  isSubmitDisabled: boolean = true;
  isTopicFetching: boolean = false;
  isLoading: boolean = false;
  isSubmitInProgress: boolean = false;
  feedbackList: string[] = [];
  showStarAnimation: boolean = false;
  isLoaderVisible: boolean = false;
  wordCount: number = 0;
  showIntro: boolean = true;
  showWriting: boolean = false;
  showFeedback: boolean = false;

  constructor(private writingService: WritingService, private router: Router) { }

  fetchTopic(): void {
    this.isLoaderVisible = true;
    this.isTopicFetching = true;
    this.errorMessage = null;
    this.topic = null;
    this.feedback = null;
    this.showWriting = false;
    this.showIntro = true;
    this.showFeedback = false;
    this.userAnswer = '';

    this.writingService.getWritingTopic(this.gradeLevel).subscribe(
      (response) => {
        this.isLoaderVisible = false;
        this.isTopicFetching = false;

        if (response && response.topic) {
          console.log("Generated Writing Topic:", response.topic);
          this.topic = response.topic;
          this.showIntro = false;
          this.showWriting = true;
        } else {
          console.warn("Unexpected response format:", response);
          this.topic = "No topic generated. Try again.";
        }
      },
      (error) => {
        console.error("Error fetching writing topic:", error);
        this.topic = "Failed to load topic.";
        this.isLoaderVisible = false;
        this.isTopicFetching = false;
      }
    );
  }

  submitAnswer(): void {
    if (this.userAnswer.trim() === '') {
      this.errorMessage = 'Please write an answer before submitting.';
      return;
    }
    if (!this.topic) {
      this.errorMessage = 'Please generate a topic first.';
      return;
    }

    this.errorMessage = null;
    this.isLoading = true;
    this.isSubmitInProgress = true;
    this.isLoaderVisible = true;

    this.showIntro = false;
    this.showWriting = true;
    this.showFeedback = false;

    this.writingService.validateResponse(this.topic, this.userAnswer).subscribe({
      next: (response) => {
        this.feedback = response.feedback;
        this.feedbackList = response.feedback.split(/(?=\d+\.)/g).map(item => item.trim());
        this.isFeedbackVisible = true;

        this.isLoading = false;
        this.isSubmitInProgress = false;
        this.isLoaderVisible = false;

        this.showWriting = false;
        this.showFeedback = true;
      },
      error: (error) => {
        this.errorMessage = error.message;

        this.isSubmitInProgress = false;
        this.isLoaderVisible = false;

        this.showWriting = false;
        this.showFeedback = true;
      }
    });
  }

  checkWordCount(): void {
    setTimeout(() => {
      const wordCount = this.userAnswer.trim().split(/\s+/).length;
      this.isSubmitDisabled = wordCount < 10;
    });
  }

  resetForm(): void {
    this.gradeLevel = 'lower';
    this.topic = null;
    this.userAnswer = '';
    this.feedback = null;
    this.errorMessage = null;
    this.isFeedbackVisible = false;
    this.isSubmitDisabled = true;
    this.isTopicFetching = false;
    this.isSubmitInProgress = false;
    this.showIntro = true;
    this.showWriting = false;
    this.showFeedback = false;
  }

  goBack(from: string): void {
    if (from === 'writing') {
      this.showWriting = false;
      this.showIntro = true;
    } else if (from === 'feedback') {
      this.showFeedback = false;
      this.showWriting = true;
    }
  }

  goToHome(): void {
    this.router.navigate(['/home']);
  }

  goToListening(): void {
    this.router.navigate(['/listening']);
  }
}