Skip to content

Commit

Permalink
feat(core): add initialized$ and initializationError$ subscriptions
Browse files Browse the repository at this point in the history
These events can be subscribed to in order to be notified when:
*  the `cookieconsent` library **has been successfully initialized**
*  the `cookieconsent` library **failed to initialize** due to an error
  • Loading branch information
tinesoft committed Jul 7, 2021
1 parent 4907c35 commit a994f5e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 8 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export class AppComponent implements OnInit, OnDestroy {
private popupOpenSubscription: Subscription;
private popupCloseSubscription: Subscription;
private initializeSubscription: Subscription;
private initializedSubscription!: Subscription;
private initializationErrorSubscription!: Subscription;
private statusChangeSubscription: Subscription;
private revokeChoiceSubscription: Subscription;
private noCookieLawSubscription: Subscription;
Expand All @@ -166,7 +168,21 @@ export class AppComponent implements OnInit, OnDestroy {

this.initializeSubscription = this.ccService.initialize$.subscribe(
(event: NgcInitializeEvent) => {
// you can use this.ccService.getConfig() to do stuff...
// the cookieconsent is initilializing... Not yet safe to call methods like `NgcCookieConsentService.hasAnswered()`
console.log(`initialize: ${JSON.stringify(event)}`);
});

this.initializedSubscription = this.ccService.initialized$.subscribe(
() => {
// the cookieconsent has been successfully initialized.
// It's now safe to use methods on NgcCookieConsentService that require it, like `hasAnswered()` for eg...
console.log(`initialized: ${JSON.stringify(event)}`);
});

this.initializationErrorSubscription = this.ccService.initializationError$.subscribe(
(event: NgcInitializationErrorEvent) => {
// the cookieconsent has failed to initialize...
console.log(`initializationError: ${JSON.stringify(event.error?.message)}`);
});

this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
Expand Down
20 changes: 17 additions & 3 deletions apps/ngx-cookieconsent-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { Component, OnInit, OnDestroy, Inject, PLATFORM_ID } from '@angular/core
import { Router, NavigationEnd, Event } from '@angular/router';
import { isPlatformBrowser } from '@angular/common';

import { NgcCookieConsentService, NgcInitializeEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from 'ngx-cookieconsent';
import { NgcCookieConsentService, NgcInitializeEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from 'ngx-cookieconsent';

import { filter } from 'rxjs/operators';
import { Subscription } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';

@Component({
selector: 'ngc-demo-root',
templateUrl: './app.component.html',
Expand All @@ -19,6 +18,8 @@ export class AppComponent implements OnInit, OnDestroy{
private popupOpenSubscription!: Subscription;
private popupCloseSubscription!: Subscription;
private initializeSubscription!: Subscription;
private initializedSubscription!: Subscription;
private initializationErrorSubscription!: Subscription;
private statusChangeSubscription!: Subscription;
private revokeChoiceSubscription!: Subscription;
private noCookieLawSubscription!: Subscription;
Expand Down Expand Up @@ -49,9 +50,22 @@ export class AppComponent implements OnInit, OnDestroy{

this.initializeSubscription = this.ccService.initialize$.subscribe(
(event: NgcInitializeEvent) => {
// you can use this.ccService.getConfig() to do stuff...
// the cookieconsent is initilializing... Not yet safe to call methods like `NgcCookieConsentService.hasAnswered()`
console.log(`initialize: ${JSON.stringify(event)}`);
});

this.initializedSubscription = this.ccService.initialized$.subscribe(
() => {
// the cookieconsent has been successfully initialized.
// It's now safe to use methods on NgcCookieConsentService that require it, like `hasAnswered()` for eg...
console.log(`initialized: ${JSON.stringify(event)}`);
});

this.initializationErrorSubscription = this.ccService.initializationError$.subscribe(
(event: NgcInitializationErrorEvent) => {
// the cookieconsent has failed to initialize...
console.log(`initializationError: ${JSON.stringify(event.error?.message)}`);
});

this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
(event: NgcStatusChangeEvent) => {
Expand Down
2 changes: 1 addition & 1 deletion libs/ngx-cookieconsent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export {
NgcCookiePosition, NgcCookieLayout, NgcCookieType, NgcCookieTheme, NgcCookieCompliance, NgcCookieOptions
} from './lib/model/index';

export { NgcInitializeEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from './lib/event/index';
export { NgcInitializeEvent, NgcInitializationErrorEvent, NgcStatusChangeEvent, NgcNoCookieLawEvent } from './lib/event/index';
1 change: 1 addition & 0 deletions libs/ngx-cookieconsent/src/lib/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './status-change.event';
export * from './initialize.event';
export * from './initialization-error.event';
export * from './no-cookie-law.event';

10 changes: 10 additions & 0 deletions libs/ngx-cookieconsent/src/lib/event/initialization-error.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

/**
* Event fired when an error occured during Cookie Consent initialization.
*/
export interface NgcInitializationErrorEvent {
/**
* The error that occured during initialization.
*/
error: Error;
}
21 changes: 21 additions & 0 deletions libs/ngx-cookieconsent/src/lib/service/cookieconsent-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,32 @@ export class NgcCookieConsentConfig {
ignoreClicksFrom?: string[];

// these callback hooks are called at certain points in the program execution

/**
* This is called when the popup is opened. It can be used to trigger an animation, or to attach extra handlers, etc.
*/
onPopupOpen?: () => void;

/**
* This is called when the popup is closed. It can be used to clean up commands from onPopupOpen.
*/
onPopupClose?: () => void;

/**
* This is called on start up, with the current chosen compliance. It can be used to tell you if the user has already consented or not as soon as you initialise the tool.
*/
onInitialise?: (status: 'allow' | 'deny' | 'dismiss') => void;

/**
* This is called any time the status is changed. This can be used to react to changes that are made to the compliance level. You can use the popup instance functions from within these callbacks too. I.E. `this.hasAnswered()` and `this.hasConsented()`.
*/
onStatusChange?: (status: 'allow' | 'deny' | 'dismiss', chosenBefore: boolean) => void;

/**
* This is called when the user clicks the `revoke` button. This means that their current choice has been invalidated.
*/
onRevokeChoice?: () => void;

onNoCookieLaw?: (countryCode: string, country: string) => void;

}
29 changes: 26 additions & 3 deletions libs/ngx-cookieconsent/src/lib/service/cookieconsent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NgcCookieConsentStatus } from '../model/common-interfaces';
import { NgcStatusChangeEvent } from '../event/status-change.event';
import { NgcNoCookieLawEvent } from '../event/no-cookie-law.event';
import { NgcInitializeEvent } from '../event/initialize.event';
import { NgcInitializationErrorEvent } from '../event';
import { NgcCookieConsentConfig } from './cookieconsent-config';
import { WindowService } from './window.service';

Expand All @@ -16,7 +17,7 @@ export interface NgcCookieConsent {
hasTransition: boolean;
status: NgcCookieConsentStatus;
getStatus(): NgcCookieConsentStatus;
initialise(config: NgcCookieConsentConfig, callback?: (popup: NgcCookieConsentPopup) => void): void;
initialise(config: NgcCookieConsentConfig, successCallback?: (popup: NgcCookieConsentPopup) => void, errorCallback?: (error: Error, popup: NgcCookieConsentPopup) => void): void;

}

Expand Down Expand Up @@ -69,6 +70,8 @@ export class NgcCookieConsentService {
private popupOpenSource: Subject<void>;
private popupCloseSource: Subject<void>;
private initializeSource: Subject<NgcInitializeEvent>;
private initializedSource: Subject<void>;
private initializationErrorSource: Subject<NgcInitializationErrorEvent>;
private statusChangeSource: Subject<NgcStatusChangeEvent>;
private revokeChoiceSource: Subject<void>;
private noCookieLawSource: Subject<NgcNoCookieLawEvent>;
Expand All @@ -82,9 +85,17 @@ export class NgcCookieConsentService {
*/
popupClose$: Observable<void>;
/**
* Observable to subscribe to and get notified when Cookie Consent initializes.
* Observable to subscribe to and get notified when Cookie Consent is initializing.
*/
initialize$: Observable<NgcInitializeEvent>;
/**
* Observable to subscribe to and get notified when Cookie Consent has been successfully initialized.
*/
initialized$: Observable<void>;
/**
* Observable to subscribe to and get notified when Cookie Consent has failed to initialize.
*/
initializationError$: Observable<NgcInitializationErrorEvent>;
/**
* Observable to subscribe to and get notified when Cookie Consent status changes.
*/
Expand All @@ -103,6 +114,8 @@ export class NgcCookieConsentService {
this.popupOpenSource = new Subject<void>();
this.popupCloseSource = new Subject<void>();
this.initializeSource = new Subject<NgcInitializeEvent>();
this.initializedSource = new Subject<void>();
this.initializationErrorSource = new Subject<NgcInitializationErrorEvent>();
this.statusChangeSource = new Subject<NgcStatusChangeEvent>();
this.revokeChoiceSource = new Subject<void>();
this.noCookieLawSource = new Subject<NgcNoCookieLawEvent>();
Expand All @@ -111,6 +124,8 @@ export class NgcCookieConsentService {
this.popupOpen$ = this.popupOpenSource.asObservable();
this.popupClose$ = this.popupCloseSource.asObservable();
this.initialize$ = this.initializeSource.asObservable();
this.initialized$ = this.initializedSource.asObservable();
this.initializationError$ = this.initializationErrorSource.asObservable();
this.statusChange$ = this.statusChangeSource.asObservable();
this.revokeChoice$ = this.revokeChoiceSource.asObservable();
this.noCookieLaw$ = this.noCookieLawSource.asObservable();
Expand Down Expand Up @@ -159,7 +174,15 @@ export class NgcCookieConsentService {
};

// Init the cookieconsent library with injected config
this.cookieconsent.initialise(this.config, (popup: NgcCookieConsentPopup) => this.popupInstance = popup);
this.cookieconsent.initialise(this.config,
(popup: NgcCookieConsentPopup) => {
this.popupInstance = popup;
this.initializedSource.next();//notify of successful initialization
},
(error: Error, popup: NgcCookieConsentPopup) => {
this.initializationErrorSource.next({error: error});//notify of failed initialization
}
);
}
}

Expand Down

0 comments on commit a994f5e

Please sign in to comment.