All files / app/components cookie-consent.component.ts

91.66% Statements 22/24
100% Branches 5/5
75% Functions 3/4
91.3% Lines 21/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              1x 24x 24x   24x 24x 24x               17x 16x   16x     1x 1x 1x                             1x         1x       2x 2x 2x     2x 1x 1x           1x          
import { Component, OnInit, signal, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CookieService, CookiePreferences } from '../services/cookie.service';
import { AnalyticsService } from '../services/analytics.service';
 
@Component({
  selector: 'app-cookie-consent',
  standalone: true,
  imports: [CommonModule, FormsModule],
  template: `
    <div *ngIf="showBanner()" class="cookie-banner">
      <div class="cookie-content">
        <div class="cookie-header">
          <span class="cookie-icon">đŸȘ</span>
          <h3 class="cookie-title">Gestion des cookies</h3>
        </div>
        <p class="cookie-description">
          Nous utilisons des cookies pour améliorer votre expérience, analyser le trafic du site et personnaliser le contenu.
          Vous pouvez accepter tous les cookies ou personnaliser vos préférences.
        </p>
 
        <!-- Options détaillées (affichées si "Personnaliser" est cliqué) -->
        <div *ngIf="showDetails()" class="cookie-options">
          <div class="cookie-option">
            <div class="option-header">
              <label class="option-label">
                <input
                  type="checkbox"
                  [(ngModel)]="preferences.essential"
                  disabled
                  class="option-checkbox"
                />
                <span class="option-title">Cookies essentiels</span>
                <span class="option-required">(Requis)</span>
              </label>
            </div>
            <p class="option-description">
              Ces cookies sont nĂ©cessaires au fonctionnement du site. Ils ne peuvent pas ĂȘtre dĂ©sactivĂ©s.
            </p>
          </div>
 
          <div class="cookie-option">
            <div class="option-header">
              <label class="option-label">
                <input
                  type="checkbox"
                  [(ngModel)]="preferences.analytics"
                  class="option-checkbox"
                />
                <span class="option-title">Cookies analytiques</span>
              </label>
            </div>
            <p class="option-description">
              Ces cookies nous aident Ă  comprendre comment les visiteurs utilisent notre site en collectant des informations anonymes.
            </p>
          </div>
 
          <div class="cookie-option">
            <div class="option-header">
              <label class="option-label">
                <input
                  type="checkbox"
                  [(ngModel)]="preferences.marketing"
                  class="option-checkbox"
                />
                <span class="option-title">Cookies marketing</span>
              </label>
            </div>
            <p class="option-description">
              Ces cookies sont utilisés pour vous montrer des publicités pertinentes et mesurer l'efficacité des campagnes publicitaires.
            </p>
          </div>
        </div>
 
        <div class="cookie-actions">
          <button
            *ngIf="!showDetails()"
            type="button"
            class="cookie-btn customize-btn"
            (click)="showDetails.set(true)"
          >
            Personnaliser
          </button>
          <button
            *ngIf="showDetails()"
            type="button"
            class="cookie-btn reject-btn"
            (click)="rejectAll()"
          >
            Tout refuser
          </button>
          <button
            *ngIf="showDetails()"
            type="button"
            class="cookie-btn save-btn"
            (click)="savePreferences()"
          >
            Enregistrer mes préférences
          </button>
          <button
            type="button"
            class="cookie-btn accept-btn"
            (click)="acceptAll()"
          >
            {{ showDetails() ? 'Tout accepter' : 'Tout accepter' }}
          </button>
        </div>
      </div>
    </div>
  `,
  styles: [`
    .cookie-banner {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      background: white;
      box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
      z-index: 10000;
      padding: 1.5rem;
      animation: slideUp 0.3s ease;
    }
 
    @keyframes slideUp {
      from {
        transform: translateY(100%);
      }
      to {
        transform: translateY(0);
      }
    }
 
    .cookie-content {
      max-width: 1200px;
      margin: 0 auto;
    }
 
    .cookie-header {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      margin-bottom: 1rem;
    }
 
    .cookie-icon {
      font-size: 2rem;
    }
 
    .cookie-title {
      font-size: 1.5rem;
      font-weight: 700;
      color: var(--primary-green);
      margin: 0;
    }
 
    .cookie-description {
      color: var(--dark-gray);
      line-height: 1.6;
      margin-bottom: 1.5rem;
      font-size: 0.95rem;
    }
 
    .cookie-options {
      background: var(--very-light-green);
      border-radius: 12px;
      padding: 1.5rem;
      margin-bottom: 1.5rem;
      display: flex;
      flex-direction: column;
      gap: 1.5rem;
    }
 
    .cookie-option {
      border-bottom: 1px solid var(--light-gray);
      padding-bottom: 1.5rem;
    }
 
    .cookie-option:last-child {
      border-bottom: none;
      padding-bottom: 0;
    }
 
    .option-header {
      margin-bottom: 0.5rem;
    }
 
    .option-label {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      cursor: pointer;
      font-weight: 600;
      color: var(--dark-gray);
    }
 
    .option-checkbox {
      width: 20px;
      height: 20px;
      cursor: pointer;
      accent-color: var(--secondary-green);
    }
 
    .option-checkbox:disabled {
      cursor: not-allowed;
      opacity: 0.6;
    }
 
    .option-title {
      flex: 1;
    }
 
    .option-required {
      font-size: 0.85rem;
      color: var(--gray);
      font-weight: 400;
    }
 
    .option-description {
      color: var(--gray);
      font-size: 0.9rem;
      line-height: 1.5;
      margin: 0;
      padding-left: 2rem;
    }
 
    .cookie-actions {
      display: flex;
      gap: 1rem;
      justify-content: flex-end;
      flex-wrap: wrap;
    }
 
    .cookie-btn {
      padding: 0.75rem 1.5rem;
      border: none;
      border-radius: 8px;
      font-weight: 600;
      font-size: 0.95rem;
      cursor: pointer;
      transition: all 0.3s ease;
    }
 
    .accept-btn {
      background: var(--secondary-green);
      color: white;
    }
 
    .accept-btn:hover {
      background: var(--primary-green);
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
    }
 
    .customize-btn {
      background: white;
      color: var(--primary-green);
      border: 2px solid var(--secondary-green);
    }
 
    .customize-btn:hover {
      background: var(--very-light-green);
    }
 
    .save-btn {
      background: var(--secondary-green);
      color: white;
    }
 
    .save-btn:hover {
      background: var(--primary-green);
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
    }
 
    .reject-btn {
      background: var(--light-gray);
      color: var(--dark-gray);
    }
 
    .reject-btn:hover {
      background: #e0e0e0;
    }
 
    @media (max-width: 768px) {
      .cookie-banner {
        padding: 1rem;
      }
 
      .cookie-title {
        font-size: 1.3rem;
      }
 
      .cookie-description {
        font-size: 0.9rem;
      }
 
      .cookie-options {
        padding: 1rem;
      }
 
      .cookie-actions {
        flex-direction: column;
      }
 
      .cookie-btn {
        width: 100%;
      }
    }
  `]
})
export class CookieConsentComponent implements OnInit {
  private cookieService = inject(CookieService);
  private analyticsService = inject(AnalyticsService);
 
  showBanner = signal(false);
  showDetails = signal(false);
  preferences: CookiePreferences = {
    essential: true,
    analytics: false,
    marketing: false
  };
 
  ngOnInit() {
    // Afficher la banniÚre seulement si l'utilisateur n'a pas encore donné son consentement
    if (!this.cookieService.hasConsent()) {
      this.showBanner.set(true);
      // Charger les préférences par défaut
      this.preferences = this.cookieService.getPreferences();
    } else {
      // Si le consentement existe déjà, initialiser Analytics si nécessaire
      const existingConsent = this.cookieService.getPreferences();
      if (existingConsent.analytics) {
        this.analyticsService.initialize();
      }
    }
  }
 
  acceptAll() {
    this.preferences = {
      essential: true,
      analytics: true,
      marketing: true
    };
    this.savePreferences();
  }
 
  rejectAll() {
    this.preferences = {
      essential: true,
      analytics: false,
      marketing: false
    };
    this.savePreferences();
  }
 
  savePreferences() {
    this.cookieService.setConsent(this.preferences);
    this.showBanner.set(false);
    this.showDetails.set(false);
    
    // Initialiser Google Analytics si l'utilisateur a accepté les cookies analytiques
    if (this.preferences.analytics) {
      this.analyticsService.initialize();
      this.analyticsService.trackEvent('cookie_consent', {
        event_category: 'engagement',
        event_label: 'analytics_accepted'
      });
    } else {
      // Réinitialiser si l'utilisateur a retiré son consentement
      this.analyticsService.reinitialize();
    }
  }
}