Revision 58570
Added by Konstantina Galouni almost 4 years ago
subscribe.service.ts | ||
---|---|---|
1 | 1 |
import {Injectable} from '@angular/core'; |
2 | 2 |
import {HttpClient} from '@angular/common/http'; |
3 |
import {map} from "rxjs/operators"; |
|
3 |
import {map, tap} from "rxjs/operators";
|
|
4 | 4 |
import {CustomOptions} from "../../services/servicesUtils/customOptions.class"; |
5 | 5 |
import {EnvProperties} from "../properties/env-properties"; |
6 | 6 |
import {COOKIE} from "../../login/utils/helper.class"; |
7 |
import {BehaviorSubject, Observable} from "rxjs"; |
|
7 | 8 |
|
8 | 9 |
@Injectable() |
9 | 10 |
export class SubscribeService { |
11 |
private isSubscribedSubject: BehaviorSubject<boolean> = new BehaviorSubject(false); |
|
10 | 12 |
|
11 | 13 |
constructor(private http: HttpClient) { |
14 |
console.log("Subscribe service constructor"); |
|
12 | 15 |
} |
13 | 16 |
|
17 |
public initIsSubscribedToCommunity(properties: EnvProperties, pid: string) { |
|
18 |
let url = properties.adminToolsAPIURL + "/community/" + pid + "/is-subscriber/"; |
|
19 |
this.http.get<boolean>(url, CustomOptions.getAuthOptionsWithBody()).subscribe((isSubscribed) => { |
|
20 |
this.isSubscribedSubject.next(isSubscribed); |
|
21 |
}); |
|
22 |
} |
|
23 |
|
|
24 |
public get isSubscribed(): Observable<boolean> { |
|
25 |
return this.isSubscribedSubject.asObservable(); |
|
26 |
} |
|
27 |
|
|
14 | 28 |
getCommunitySubscribers(properties: EnvProperties, pid: string) { |
15 | 29 |
let url = properties.adminToolsAPIURL + "/community/" + pid + "/subscribers"; |
16 | 30 |
return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url); |
... | ... | |
42 | 56 |
} |
43 | 57 |
|
44 | 58 |
subscribeToCommunity(properties: EnvProperties, pid: string) { |
45 |
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody()); |
|
59 |
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber", {}, CustomOptions.getAuthOptionsWithBody()) |
|
60 |
.pipe(tap(isSubscribed => { |
|
61 |
console.log("subscribe servive (subscribeToCommunity): isSubscribed: "+isSubscribed); |
|
62 |
this.isSubscribedSubject.next(isSubscribed);})); |
|
46 | 63 |
} |
47 | 64 |
|
48 | 65 |
unSubscribeToCommunity(properties: EnvProperties, pid: string) { |
49 |
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody()); |
|
66 |
return this.http.post<any>(properties.adminToolsAPIURL + "/community/" + pid + "/subscriber/delete", {}, CustomOptions.getAuthOptionsWithBody()) |
|
67 |
.pipe(tap(unSubscribed => { |
|
68 |
console.log("subscribe servive (unSubscribeToCommunity): isSubscribed: "+!unSubscribed); |
|
69 |
this.isSubscribedSubject.next(!unSubscribed);})); |
|
50 | 70 |
} |
51 | 71 |
|
52 | 72 |
subscribeToCommunityByEmail(properties: EnvProperties, pid: string, email: string) { |
Also available in: Unified diff
[Trunk | Library]: subscribe.service.ts: Connect redesign: Add field 'isSubscribedSubject: BehaviorSubject<boolean>' and methods to initiate and get it (singleton service, to update subscriber status).