Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class CookieLocale {
  2.   /**
  3.    * @param {HTMLelement} container
  4.    */
  5.   constructor(container) {
  6.     this.container = container;
  7.     this.cookieId = 'cookie-origin';
  8.     this.countries = [...this.container.getElementsByClassName('js-country')];
  9.     this.countries.forEach((country) => {
  10.       country.addEventListener('click', this.handleClick);
  11.     });
  12.   }
  13.  
  14.   setCookie = (countryLocale) => {
  15.     const cookieObject = window.globals.cookies[this.cookieId];
  16.     const cookieExpiration = cookieObject.expiration;
  17.     const cookieName = cookieObject.name;
  18.     const dayInMs = 86400;
  19.     const expiration = dayInMs * cookieExpiration;
  20.     document.cookie = `${cookieName}=${countryLocale} ; max-age=${expiration}; path=/`;
  21.   };
  22.  
  23.   handleClick = (event) => {
  24.     event.preventDefault();
  25.  
  26.     if (event.currentTarget.dataset.locale !== undefined) {
  27.       this.countryLocale = event.currentTarget.dataset.locale;
  28.       this.setCookie(this.countryLocale);
  29.     }
  30.   };
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement