Skip to content

Commit 8d8dac4

Browse files
committed
[DSC-1053] Show AddThis plugin box only on CSR
1 parent 1d8bd70 commit 8d8dac4

2 files changed

Lines changed: 24 additions & 32 deletions

File tree

src/app/social/social.component.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
Component,
3-
Inject, OnDestroy,
4-
OnInit,
5-
} from '@angular/core';
6-
import { DOCUMENT } from '@angular/common';
1+
import { Component, Inject, OnDestroy, OnInit, PLATFORM_ID, } from '@angular/core';
2+
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
73
import { ActivatedRoute } from '@angular/router';
84
import { SocialService } from './social.service';
95

@@ -13,7 +9,7 @@ import { SocialService } from './social.service';
139
styleUrls: ['./social.component.scss']
1410
})
1511
/**
16-
* Component to render dinamically the social2 buttons using addThis plugin
12+
* Component to render dynamically the social2 buttons using addThis plugin
1713
*/
1814
export class SocialComponent implements OnInit, OnDestroy {
1915

@@ -25,19 +21,22 @@ export class SocialComponent implements OnInit, OnDestroy {
2521
subscription;
2622

2723
constructor(@Inject(DOCUMENT) private _document: Document,
24+
@Inject(PLATFORM_ID) protected platformId: Object,
2825
private socialService: SocialService,
2926
private activatedRoute: ActivatedRoute,
3027
) {
3128
}
3229

3330
ngOnInit() {
34-
this.subscription = this.socialService.showSocialButtons(this.activatedRoute).subscribe((show) => {
35-
if (show) {
36-
this.showSocialButtons();
37-
} else {
38-
this.hideSocialButtons();
39-
}
40-
});
31+
if (isPlatformBrowser(this.platformId)) {
32+
this.subscription = this.socialService.showSocialButtons(this.activatedRoute).subscribe((show) => {
33+
if (show) {
34+
this.showSocialButtons();
35+
} else {
36+
this.hideSocialButtons();
37+
}
38+
});
39+
}
4140
}
4241

4342
showSocialButtons() {

src/app/social/social.service.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Injectable } from '@angular/core';
2+
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
3+
4+
import { BehaviorSubject, combineLatest, Observable, of } from 'rxjs';
5+
import { distinctUntilChanged, filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
6+
27
import { environment } from '../../environments/environment';
38
import { CookieService } from '../core/services/cookie.service';
4-
import { distinctUntilChanged, filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
5-
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
69
import { AuthService } from '../core/auth/auth.service';
7-
import { BehaviorSubject, combineLatest, Observable, of } from 'rxjs';
810

911
export const APP_THIS_ID_SELECTOR = '#at4-share';
1012

@@ -29,30 +31,29 @@ export class SocialService {
2931
}
3032

3133
initializeAddThisScript(_document: Document): any {
32-
// console.log('Initializing the addThisCookie script');
34+
// Initializing the addThisCookie script
3335
const script = _document.createElement('script');
3436
script.type = 'text/javascript';
3537
script.src = environment.addThisPlugin.scriptUrl + environment.addThisPlugin.siteId;
3638
_document.body.appendChild(script);
3739
}
3840

3941
hide(_document: Document) {
40-
// console.log('Hiding social buttons');
42+
// Hiding social buttons
4143
const socialButtons: HTMLElement = _document.querySelector(APP_THIS_ID_SELECTOR);
4244
if (socialButtons) {
43-
// console.log('HTML Element found, setting display to none');
45+
// HTML Element found, setting display to none
4446
socialButtons.style.display = 'none';
4547
}
4648
}
4749

4850
show(_document: Document) {
49-
// console.log('Showing social buttons');
51+
// Showing social buttons
5052
const socialButtons: HTMLElement = _document.querySelector(APP_THIS_ID_SELECTOR);
5153
if (socialButtons) {
52-
// console.log('HTML Element found, setting display to block');
54+
// HTML Element found, setting display to block
5355
socialButtons.style.display = 'block';
5456
}
55-
// console.error('No HTML Elements to show');
5657
}
5758

5859
protected initialize(activatedRoute: ActivatedRoute) {
@@ -89,7 +90,6 @@ export class SocialService {
8990

9091
// Listen to every cookies / user state changes and evaluate sharing state
9192
combineLatest([cookies$, userUUId$]).subscribe(([cookieMap, userUUID]) => {
92-
// console.log('COOKIE/USER change detected. Evaluating.', cookieMap, userUUID);
9393
this.evaluateShowHide(cookieMap, userUUID);
9494
});
9595

@@ -119,17 +119,10 @@ export class SocialService {
119119
}
120120

121121
protected isAddThisCookieEnabled(cookieMap, userUUID) {
122-
// console.log(cookieMap, userUUID);
123-
// if (userUUID) {
124-
// console.log('The user is authenticated, checking klaro-' + userUUID);
125-
// } else {
126-
// console.log('The user is not authenticated, checking klaro-anonymous');
127-
// }
128122
const cookie = userUUID ? this.cookie.get('klaro-' + userUUID) : this.cookie.get('klaro-anonymous');
129123
const addThisCookie = cookie ? cookie['add-this'] : false;
130-
// console.log('AddThisCookie is ' + addThisCookie);
124+
131125
return addThisCookie;
132126
}
133127

134-
135128
}

0 commit comments

Comments
 (0)