forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DSC-1570] Fix issue with missing providers for klaro service during SSR
- Loading branch information
Showing
8 changed files
with
112 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../../../environments/environment'; | ||
import { datadogRum } from '@datadog/browser-rum'; | ||
import { CookieConsents, KlaroService } from '../cookies/klaro.service'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { createSelector, Store } from '@ngrx/store'; | ||
import { setDatadogRumStatusAction } from './datadog-rum.actions'; | ||
import { DatadogRumState } from './datadog-rum.reducer'; | ||
import { distinctUntilChanged, take } from 'rxjs/operators'; | ||
import { coreSelector } from '../../core/core.selectors'; | ||
import { CoreState } from '../../core/core-state.model'; | ||
import { DatadogRumService } from './datadog-rum.service'; | ||
|
||
@Injectable() | ||
export class BrowserDatadogRumService extends DatadogRumService { | ||
|
||
consentsUpdates$: BehaviorSubject<CookieConsents>; | ||
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum); | ||
|
||
constructor( | ||
private klaroService: KlaroService, | ||
private store: Store | ||
) { | ||
super(); | ||
} | ||
|
||
initDatadogRum() { | ||
this.klaroService.watchConsentUpdates(); | ||
this.consentsUpdates$ = this.klaroService.consentsUpdates$; | ||
this.consentsUpdates$.subscribe(savedPreferences => { | ||
this.getDatadogRumState().subscribe((state) => { | ||
if (savedPreferences?.datadog && | ||
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId && | ||
environment.datadogRum?.service && environment.datadogRum?.env) { | ||
if (!state.isInitialized) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isInitialized: true, | ||
isRunning: true | ||
})); | ||
datadogRum.init(environment.datadogRum); | ||
} else if (!state.isRunning) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: true | ||
})); | ||
datadogRum.startSessionReplayRecording(); | ||
} | ||
} else { | ||
datadogRum.stopSessionReplayRecording(); | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: false | ||
})); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
getDatadogRumState(): Observable<DatadogRumState> { | ||
return this.store | ||
.select(this.datadogRumStateSelector) | ||
.pipe( | ||
distinctUntilChanged(), | ||
take(1), | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,11 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../../../environments/environment'; | ||
import { datadogRum } from '@datadog/browser-rum'; | ||
import { CookieConsents, KlaroService } from '../cookies/klaro.service'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
import { createSelector, Store } from '@ngrx/store'; | ||
import { setDatadogRumStatusAction } from './datadog-rum.actions'; | ||
import { DatadogRumState } from './datadog-rum.reducer'; | ||
import { distinctUntilChanged, take } from 'rxjs/operators'; | ||
import { coreSelector } from '../../core/core.selectors'; | ||
import { CoreState } from '../../core/core-state.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class DatadogRumService { | ||
@Injectable() | ||
export abstract class DatadogRumService { | ||
|
||
consentsUpdates$: BehaviorSubject<CookieConsents>; | ||
datadogRumStateSelector = createSelector(coreSelector, (state: CoreState) => state.datadogRum); | ||
|
||
constructor( | ||
private klaroService: KlaroService, | ||
private store: Store | ||
) { | ||
} | ||
|
||
initDatadogRum() { | ||
this.klaroService.watchConsentUpdates(); | ||
this.consentsUpdates$ = this.klaroService.consentsUpdates$; | ||
this.consentsUpdates$.subscribe(savedPreferences => { | ||
this.getDatadogRumState().subscribe((state) => { | ||
if (savedPreferences?.datadog && | ||
environment.datadogRum?.clientToken && environment.datadogRum?.applicationId && | ||
environment.datadogRum?.service && environment.datadogRum?.env) { | ||
if (!state.isInitialized) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isInitialized: true, | ||
isRunning: true | ||
})); | ||
datadogRum.init(environment.datadogRum); | ||
} else if (!state.isRunning) { | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: true | ||
})); | ||
datadogRum.startSessionReplayRecording(); | ||
} | ||
} else { | ||
datadogRum.stopSessionReplayRecording(); | ||
this.store.dispatch(new setDatadogRumStatusAction({ | ||
isRunning: false | ||
})); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|
||
getDatadogRumState(): Observable<DatadogRumState> { | ||
return this.store | ||
.select(this.datadogRumStateSelector) | ||
.pipe( | ||
distinctUntilChanged(), | ||
take(1), | ||
); | ||
} | ||
/** | ||
* Initializes the service | ||
*/ | ||
abstract initDatadogRum(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { DatadogRumService } from './datadog-rum.service'; | ||
|
||
@Injectable() | ||
export class ServerDatadogRumService extends DatadogRumService { | ||
|
||
initDatadogRum() { | ||
return; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters