-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaborator_report.ts
More file actions
221 lines (207 loc) · 7.34 KB
/
Copy pathcollaborator_report.ts
File metadata and controls
221 lines (207 loc) · 7.34 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* collaborator_report.ts provides the browser side JavaScript for interacting with the collaborator_reports by providing a field search form
* and setting up the command line options to be passed through the reports system after vetting.
*
* The module provides a form that list people from people.ds by clpid and name. Once selected then the report can be formed and sent back
* to the cold middleware which then vets the clpid to make sure it is real before injecting a report request into the report queue where it'll
* get run with the correct parameter. Form elements
*
* - clpid with auto complete
* - email (optional) of who to notify when report is available (example someone in the library)
*
* ~~~HTML
* <div id="collaborator_report"></div>
*
* <noscript>JavaScript required for the collaborator report</noscript>
*
* <script type="module">
* import { CollaboratorReportUI } from "./modules/collaborator_report.js";
* const baseUrl = URL.parse(window.location.href);
* baseUrl.pathname = baseUrl.pathname.replace(/collaborator_report.html$/g, '');
* baseUrl.search = "";
* const reportElement = document.getElementById("collaborator_report");
* window.addEventListener('DOMContentLoaded', (event) => {
* const CollaboratorReprotUI = new RdmReviewQueueUI({
* baseUrl: baseUrl,
* reportElement: reportElement
* });
* });
* </script>
* ~~~
*/
import { ClientAPI } from "./client_api.ts";
export class CollaboratorReportUI {
private cName: string = "people.ds";
private reportElement!: HTMLElement;
private clpidElement: HTMLInputElement;
private baseUrl: URL;
private basePath: string = "";
private buttonSubmit!: HTMLInputElement;
private buttonClear!: HTMLInputElement;
private resultSection!: HTMLElement;
private clientAPI!: ClientAPI;
constructor(
options: {
reportElement: HTMLElement | string;
baseUrl: string;
clientAPI: ClientAPI;
cName?: string;
},
) {
// Build the search form and results box
const formHTML: string = `<form method="get">
<label for="clpid">clpid</label>
<input
name="clpid"
id="clpid"
type="text"
value=""
size="40"
placeholder="Enter a clpid, example Doiel-R-S"
list="clpid-list"
required
autocomplete="on"
>
<datalist id="clpid-list"></datalist>
<input type="submit" value="⚙️">
<input type="reset" value="❌">
</form>
<p>
<section class="collaborator-report-results" id="collaborator-report-results"></section>
</p>
`;
// FIXME: The collaborator report form needs a auto complete for the clpid. This can be done with an input element typed
// to a data list element avoiding allot of JavaScript. Need to update this via the form builder. That means
// to display the form I will see how long it takes to get a list of people.ds keys from the API. Hopefully
// it is fast enough.
this.cName = (options.cName === undefined) ? "people.ds" : options.cName;
this.reportElement = typeof options.reportElement === "string"
? document.getElementById(options.reportElement)!
: options.reportElement;
this.baseUrl = new URL(options.baseUrl);
this.basePath = this.baseUrl.pathname;
this.clientAPI = options.clientAPI;
// set update form elements
this.reportElement.innerHTML = formHTML;
this.clpidElement = this.reportElement.querySelector("#clpid")!;
this.buttonSubmit = this.reportElement.querySelector(
"input[type=submit]",
)!;
this.buttonClear = this.reportElement.querySelector(
"input[type=reset]",
)!;
this.resultSection = this.reportElement.querySelector("section")!;
// Map in query parameters from the URL.
const u = new URL(window.location.href);
const params = u.searchParams!;
let clpid = params.get("clpid") || "";
clpid = clpid.trim();
this.populateAutocomplete();
if (clpid !== "") {
this.clpidElement.value = clpid;
this.setupReport(clpid);
}
// Attach event listener for form submission
this.reportElement.querySelector("form")!.addEventListener(
"submit",
(e) => {
e.preventDefault();
const clpid = this.clpidElement.value.trim();
// Update the URL
this.updateURL(clpid);
// Now setup the query
this.setupReport(clpid);
},
);
// Attach event listener for reset button
this.buttonClear.addEventListener("click", () => {
this.resultSection.innerText = `Enter clpid and press ⚙️`;
});
}
private async populateAutocomplete() {
const peopleList = await this.clientAPI.getList(
"people.ds",
"get_all_clpid",
);
const dataListElement: HTMLDataListElement = document.getElementById(
"clpid-list",
);
if (dataListElement !== undefined && dataListElement != null) {
for (let clpid of peopleList) {
let optElem = document.createElement("option");
optElem.value = clpid;
dataListElement.appendChild(optElem);
}
}
}
private updateURL(clpid: string) {
const newUrl = new URL(window.location.href);
newUrl.search = new URLSearchParams({ clpid: clpid }).toString();
// Update the URL in the address bar
window.history.pushState({}, "", newUrl);
}
private async isValidClpid(clpid: string): Promise<boolean> {
let result: { [key: string]: any }[] = await this.clientAPI.getList(
"people.ds",
"validate_clpid",
new URLSearchParams({ "clpid": clpid }),
);
if (
(result === undefined) || (result.length !== 1) ||
(result[0].clpid !== clpid)
) {
return false;
}
return true;
}
private async postReportRequest(
report_name: string,
clpid: string,
emails?: string,
): Promise<Response> {
const postUrl = "../reports";
const formData = new URLSearchParams();
formData.append("report_name", report_name);
formData.append("clpid", clpid);
formData.append("emails", emails);
const defaultHeaders = {
"Content-Type": "application/x-www-form-urlencoded",
};
const response = await fetch(postUrl, {
method: "POST",
headers: defaultHeaders,
body: formData.toString(),
});
if (!response.ok) {
console.log(`HTTP error! status: ${response.status} ${postUrl}`);
}
return response;
}
private async setupReport(clpid: string): Promise<void> {
if (clpid === "") {
this.resultSection.innerText = `Enter clpid and press ⚙️`;
return;
}
// Make sure clpid is known, then send a post to the middleware.
if (!await this.isValidClpid(clpid)) {
this.resultSection.innerHTML =
`<em>Invalid clpid '${clpid}'</em>, enter clpid and press ⚙️`;
return;
}
// FIXME: We have a valid clpid, let's submit the request to the reports queue for processing.
let resp = await this.postReportRequest(
"run_collaborator_report",
clpid,
"",
);
if (resp.ok) {
this.resultSection.innerHTML =
`The collaborator report for "${clpid}" is <a href="./reports" title="return to reports page for pickup">queued</a>`;
} else {
this.resultSection.innerHTML =
`There was a problem submitting the collaborator report for "${clpid}", ${
JSON.stringify(resp)
}`;
}
}
}