-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotation-input.component.ts
More file actions
125 lines (111 loc) · 4.21 KB
/
Copy pathannotation-input.component.ts
File metadata and controls
125 lines (111 loc) · 4.21 KB
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
import { Component, DestroyRef, inject, OnInit } from "@angular/core";
import { CommonModule } from "@angular/common";
import {
FormArray,
FormControl,
FormGroup,
FormsModule,
ReactiveFormsModule,
Validators,
} from "@angular/forms";
import { PremisesFormComponent } from "./premises-form/premises-form.component";
import {
KnowledgeBaseFormComponent,
KnowledgeBaseRelationship,
} from "./knowledge-base-form/knowledge-base-form.component";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { ProblemResponse } from "../../types";
import { faExclamationCircle } from "@fortawesome/free-solid-svg-icons";
import { ProblemDetailsComponent } from "./problem-details/problem-details.component";
import { combineLatest, Subject } from "rxjs";
import { ActivatedRoute, Router } from "@angular/router";
import { ProblemService } from "@/services/problem.service";
import { FontAwesomeModule } from "@fortawesome/angular-fontawesome";
export type ParseInputForm = FormGroup<{
premises: FormArray<FormControl<string>>;
hypothesis: FormControl<string>;
kbItems: FormArray<KnowledgeBaseItemsForm>;
}>;
type KnowledgeBaseItemsForm = FormGroup<{
entity1: FormControl<string>;
relationship: FormControl<KnowledgeBaseRelationship>;
entity2: FormControl<string>;
}>;
export type ParseInput = ReturnType<ParseInputForm["getRawValue"]>;
@Component({
selector: "la-annotation-input",
standalone: true,
imports: [
CommonModule,
PremisesFormComponent,
KnowledgeBaseFormComponent,
FormsModule,
ReactiveFormsModule,
ProblemDetailsComponent,
FontAwesomeModule
],
templateUrl: "./annotation-input.component.html",
styleUrl: "./annotation-input.component.scss",
})
export class AnnotationInputComponent implements OnInit {
private route = inject(ActivatedRoute);
private router = inject(Router);
private destroyRef = inject(DestroyRef);
private problemService = inject(ProblemService);
public form: ParseInputForm | null = null;
public problem: ProblemResponse | null = null;
public submit$ = new Subject<void>();
public faExclamationCircle = faExclamationCircle;
ngOnInit(): void {
this.problemService.problem$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((problem) => {
// Navigate away if the backend provides a new Problem ID.
this.navigateToNewProblem(problem);
// Otherwise, update local state and form.
this.problem = problem;
this.form = problem ? this.buildForm(problem) : null;
});
combineLatest([
this.route.paramMap,
this.route.queryParamMap])
.pipe(
takeUntilDestroyed(this.destroyRef)
)
.subscribe(([params, queryParams]) => {
this.problemService.allParams$.next({ params, queryParams });
});
}
private navigateToNewProblem(problem: ProblemResponse | null): void {
if (!problem?.problem) {
return;
}
const incomingProblemId = problem?.id?.toString();
const currentProblemId = this.route.snapshot.paramMap.get("problemId");
if (incomingProblemId !== currentProblemId) {
this.router.navigate(['/annotate', problem.id], {
queryParamsHandling: "preserve",
});
}
}
private buildForm(response: ProblemResponse): ParseInputForm {
const premises = response.problem?.premises || [];
const hypothesis = response.problem?.hypothesis || "";
return new FormGroup({
premises: new FormArray(
premises.map(
(premise) =>
new FormControl<string>(premise, {
validators: [Validators.required],
nonNullable: true,
})
)
),
hypothesis: new FormControl<string>(hypothesis, {
validators: [Validators.required],
nonNullable: true,
}),
kbItems: new FormArray<KnowledgeBaseItemsForm>([]),
});
}
}