Skip to content

Commit 3f69dfb

Browse files
Reduce excessive nesting in newProblem$ observable
1 parent c666067 commit 3f69dfb

1 file changed

Lines changed: 21 additions & 30 deletions

File tree

frontend/src/app/services/problem.service.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ParseInput } from "@/annotate/annotation-input/annotation-input.component";
22
import extractBaseParam from "@/shared/extractBaseParam";
3-
import { ProblemResponse, SaveProblemResponse, Dataset, EntailmentLabel } from "@/types";
3+
import { ProblemResponse, SaveProblemResponse, Dataset, EntailmentLabel, Problem } from "@/types";
44
import { HttpClient, HttpParams } from "@angular/common/http";
55
import { Injectable, inject } from "@angular/core";
66
import { ParamMap } from "@angular/router";
@@ -79,6 +79,16 @@ export class ProblemService {
7979
);
8080

8181
private newProblem$(baseParam: number | null): Observable<ProblemResponse> {
82+
if (baseParam !== null) {
83+
return this.existingProblem$(baseParam.toString())
84+
.pipe(
85+
map(response => this.createNewProblemResponse(baseParam, response?.problem))
86+
);
87+
}
88+
return of<ProblemResponse>(this.createNewProblemResponse());
89+
}
90+
91+
private createNewProblemResponse(baseParam?: number, existingProblem?: Problem | null): ProblemResponse {
8292
const sharedProblemResponse: Omit<ProblemResponse, "problem"> = {
8393
index: null,
8494
first: null,
@@ -89,42 +99,23 @@ export class ProblemService {
8999
error: null,
90100
};
91101

92-
if (baseParam !== null) {
93-
return this.existingProblem$(baseParam.toString()).pipe(map(response => {
94-
const problem = response?.problem;
95-
return {
96-
...sharedProblemResponse,
97-
problem: {
98-
id: null,
99-
base: baseParam,
100-
hypothesis: problem?.hypothesis ?? "",
101-
dataset: Dataset.USER,
102-
premises: problem?.premises ?? [],
103-
entailmentLabel: EntailmentLabel.UNKNOWN,
104-
extraData: null,
105-
// KB items are not shared across problems.
106-
kbItems: problem?.kbItems.map(kbItem => ({
107-
...kbItem,
108-
id: null,
109-
})) ?? []
110-
}
111-
};
112-
}));
113-
}
114-
return of<ProblemResponse>({
102+
return {
115103
...sharedProblemResponse,
116104
problem: {
117105
id: null,
118-
base: baseParam,
119-
hypothesis: "",
106+
base: baseParam ?? null,
107+
hypothesis: existingProblem?.hypothesis ?? "",
120108
dataset: Dataset.USER,
121-
premises: [],
109+
premises: existingProblem?.premises ?? [],
122110
entailmentLabel: EntailmentLabel.UNKNOWN,
123111
extraData: null,
124-
kbItems: []
112+
kbItems: existingProblem?.kbItems.map(kbItem => ({
113+
...kbItem,
114+
id: null,
115+
})) ?? []
116+
}
125117

126-
},
127-
});
118+
};
128119
}
129120

130121
private existingProblem$(problemId?: string, queryParams?: ParamMap): Observable<ProblemResponse | null> {

0 commit comments

Comments
 (0)