In my application, I've implemented a cookie checking service to monitor the status of my cookie consent popup:
@Injectable()
export class CookieCheckService implements OnInit, OnDestroy {
public hasConsented = false;
private cookieStatusChangeSubscription: Subscription;
constructor(
private ccService: NgcCookieConsentService
) {
}
ngOnInit() {
if (this.ccService.hasConsented()) {
this.hasConsented = true;
}
console.log(this.hasConsented);
this.cookieStatusChangeSubscription = this.ccService.statusChange$.subscribe(
(event: NgcStatusChangeEvent) => {
this.hasConsented = event.status === this.ccService.getStatus().allow;
});
}
ngOnDestroy() {
this.cookieStatusChangeSubscription.unsubscribe();
}
}
My plan was to call this service from any component where I need to check the status, like in my footer where I have Google Maps displayed:
@Component({
selector : 'app-footer',
templateUrl: './footer.component.html',
styleUrls : ['./footer.component.css']
})
export class FooterComponent {
hasConsented = false;
constructor(
private cookieCheckService: CookieCheckService
) {
this.hasConsented = cookieCheckService.hasConsented;
}
}
When I click on allow, I want to show my Google Maps widget using ngIf
, but I'm not getting any value from my service - even initially. What am I doing wrong here?
Update
Just to clarify: this.ccService.getStatus()
is an interface that provides the following options:
export interface NgcCookieConsentStatus {
allow?: 'allow';
deny?: 'deny';
dismiss?: 'dismiss';
}