ichec-angular-core 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BehaviorSubject, map, catchError, mergeMap, throwError, tap, of, debounceTime, distinctUntilChanged, merge, Subscription, finalize } from 'rxjs';
|
|
2
2
|
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
|
|
3
3
|
import * as i0 from '@angular/core';
|
|
4
|
-
import { InjectionToken, inject,
|
|
4
|
+
import { InjectionToken, inject, DOCUMENT, signal, Injectable, Component, computed, input, output, viewChild } from '@angular/core';
|
|
5
5
|
import { CookieService } from 'ngx-cookie-service';
|
|
6
6
|
import * as i1$2 from '@angular/forms';
|
|
7
7
|
import { FormControl, Validators, FormGroup, ReactiveFormsModule, FormBuilder } from '@angular/forms';
|
|
@@ -183,9 +183,11 @@ class FileRecord {
|
|
|
183
183
|
const REST_SERVICE_CONFIG = new InjectionToken("Config for the rest service");
|
|
184
184
|
class RestService {
|
|
185
185
|
manifest = new BehaviorSubject(null);
|
|
186
|
+
loggedInUser = new BehaviorSubject(null);
|
|
186
187
|
_url = "";
|
|
187
188
|
userQuery = "user";
|
|
188
189
|
config = inject(REST_SERVICE_CONFIG);
|
|
190
|
+
document = inject(DOCUMENT);
|
|
189
191
|
_http = inject(HttpClient);
|
|
190
192
|
cookieService = inject(CookieService);
|
|
191
193
|
deleteItem(id) {
|
|
@@ -223,16 +225,16 @@ class RestService {
|
|
|
223
225
|
if (query.filter) {
|
|
224
226
|
params.set('search', query.filter);
|
|
225
227
|
}
|
|
226
|
-
return this._http.get(this.baseUrl, this.getRequestBase("json", new HttpParams({ fromObject: Object.fromEntries(params) }))).pipe(catchError(this.handleError));
|
|
228
|
+
return this._http.get(this.baseUrl, this.getRequestBase("json", new HttpParams({ fromObject: Object.fromEntries(params) }), "", true)).pipe(catchError(this.handleError));
|
|
227
229
|
}
|
|
228
230
|
getItem(id) {
|
|
229
|
-
return this._http.get(this.baseUrl + id + "/", this.getRequestBase()).pipe(catchError(this.handleError));
|
|
231
|
+
return this._http.get(this.baseUrl + id + "/", this.getRequestBase("json", new HttpParams(), "", true)).pipe(catchError(this.handleError));
|
|
230
232
|
}
|
|
231
233
|
getUrl(url) {
|
|
232
|
-
return this._http.get(url, this.getRequestBase()).pipe(catchError(this.handleError));
|
|
234
|
+
return this._http.get(url, this.getRequestBase("json", new HttpParams(), "", true)).pipe(catchError(this.handleError));
|
|
233
235
|
}
|
|
234
236
|
getPaginatedUrl(url) {
|
|
235
|
-
return this._http.get(url, this.getRequestBase()).pipe(catchError(this.handleError));
|
|
237
|
+
return this._http.get(url, this.getRequestBase("json", new HttpParams(), "", true)).pipe(catchError(this.handleError));
|
|
236
238
|
}
|
|
237
239
|
getOptions() {
|
|
238
240
|
return this._http.options(this.baseUrl, this.getRequestBase()).pipe(catchError(this.handleError));
|
|
@@ -271,7 +273,7 @@ class RestService {
|
|
|
271
273
|
postItem(item) {
|
|
272
274
|
return this._http.post(this.baseUrl, item, this.getRequestBase()).pipe(catchError(this.handleError));
|
|
273
275
|
}
|
|
274
|
-
getHeaders(content_type = "json", file = "") {
|
|
276
|
+
getHeaders(content_type = "json", file = "", xhr = false) {
|
|
275
277
|
let headers = new HttpHeaders({ 'Accept': 'application/json' });
|
|
276
278
|
if (this.config.auth_type === "token") {
|
|
277
279
|
headers = headers.set('Authorization', "Token " + localStorage.getItem("api_token"));
|
|
@@ -288,6 +290,9 @@ class RestService {
|
|
|
288
290
|
else if (content_type) {
|
|
289
291
|
headers = headers.set('Content-Type', content_type);
|
|
290
292
|
}
|
|
293
|
+
if (xhr) {
|
|
294
|
+
headers = headers.set("x-requested-with", "XMLHttpRequest");
|
|
295
|
+
}
|
|
291
296
|
return headers;
|
|
292
297
|
}
|
|
293
298
|
get baseUrl() {
|
|
@@ -296,12 +301,12 @@ class RestService {
|
|
|
296
301
|
get mediaUrl() {
|
|
297
302
|
return this.config.endpoint_url + "/api/" + this._url.slice(0, -1) + "_media/";
|
|
298
303
|
}
|
|
299
|
-
getRequestBase(content_type = "json", params = new HttpParams(), file = "") {
|
|
304
|
+
getRequestBase(content_type = "json", params = new HttpParams(), file = "", xhr = false) {
|
|
300
305
|
let supports_cors = "same-origin";
|
|
301
306
|
if (this.manifest.value && this.manifest.value.supports_cors) {
|
|
302
307
|
supports_cors = 'include';
|
|
303
308
|
}
|
|
304
|
-
return { headers: this.getHeaders(content_type, file),
|
|
309
|
+
return { headers: this.getHeaders(content_type, file, xhr),
|
|
305
310
|
credentials: supports_cors ? 'include' : 'same-origin',
|
|
306
311
|
params: params };
|
|
307
312
|
}
|
|
@@ -310,6 +315,11 @@ class RestService {
|
|
|
310
315
|
// A client-side or network error occurred. Handle it accordingly.
|
|
311
316
|
console.error('Client side or network error:', error.error);
|
|
312
317
|
}
|
|
318
|
+
else if (error.status === 403 && "refresh_url" in error.error) {
|
|
319
|
+
// This is a special case in the backend - our session has timed out
|
|
320
|
+
// so we need to reload that page to reflect the user's current status
|
|
321
|
+
window.location.reload();
|
|
322
|
+
}
|
|
313
323
|
else {
|
|
314
324
|
console.error(`Backend returned code ${error.status}, body was: `, error.error);
|
|
315
325
|
}
|
|
@@ -348,9 +358,7 @@ class UserService extends ItemService {
|
|
|
348
358
|
/**
|
|
349
359
|
Service to handle IPortalMember via REST and also handle logins.
|
|
350
360
|
*/
|
|
351
|
-
loggedInUser = new BehaviorSubject(null);
|
|
352
361
|
canInvite = signal(false, ...(ngDevMode ? [{ debugName: "canInvite" }] : []));
|
|
353
|
-
document = inject(DOCUMENT);
|
|
354
362
|
_url = PortalMember.plural;
|
|
355
363
|
typename = PortalMember.typename;
|
|
356
364
|
permissions = new Map();
|
|
@@ -376,22 +384,25 @@ class UserService extends ItemService {
|
|
|
376
384
|
if (next) {
|
|
377
385
|
suffix = "?next=" + next;
|
|
378
386
|
}
|
|
379
|
-
|
|
380
|
-
|
|
387
|
+
else {
|
|
388
|
+
suffix = "?next=/home";
|
|
389
|
+
}
|
|
390
|
+
const logout_url = prefix + this.manifest.value.logout_url + "/" + suffix;
|
|
391
|
+
console.log("posting logout to: ", logout_url);
|
|
392
|
+
this._http.post(logout_url, "", {
|
|
381
393
|
headers: new HttpHeaders({
|
|
382
394
|
'X-CSRFToken': this.cookieService.get("csrftoken")
|
|
383
395
|
}),
|
|
384
396
|
responseType: 'text',
|
|
385
397
|
credentials: this.manifest.value.supports_cors ? 'include' : 'same-origin'
|
|
386
|
-
}).pipe(
|
|
387
|
-
// The backend can return an error as it tries to redirect to a non-existing url.
|
|
388
|
-
catchError(_ => this.document.location.href = prefix + "/" + suffix)).subscribe(_ => {
|
|
398
|
+
}).pipe(map(m => this._logout_redirect(JSON.parse(m)))).subscribe(_ => {
|
|
389
399
|
console.log("Logged Out");
|
|
390
|
-
// Force a page reload
|
|
391
|
-
this.document.location.href = prefix + "/" + suffix;
|
|
392
|
-
//this.login(next)
|
|
393
400
|
});
|
|
394
401
|
}
|
|
402
|
+
_logout_redirect(manifest) {
|
|
403
|
+
console.log("Redirecting logout to: ", manifest.url);
|
|
404
|
+
this.document.location.href = manifest.url;
|
|
405
|
+
}
|
|
395
406
|
canCreate() {
|
|
396
407
|
return this.canInvite();
|
|
397
408
|
}
|
|
@@ -424,7 +435,8 @@ class UserService extends ItemService {
|
|
|
424
435
|
if (this.config.endpoint_url) {
|
|
425
436
|
prefix = this.config.endpoint_url + "/";
|
|
426
437
|
}
|
|
427
|
-
|
|
438
|
+
const suffix = "/?next=" + next;
|
|
439
|
+
this.document.location.href = prefix + this.manifest.value.login_url + suffix;
|
|
428
440
|
}
|
|
429
441
|
hasAddPermission(feature) {
|
|
430
442
|
const perm = this.permissions.get(feature);
|