all files / src/shared/ GeoManager.js

84.62% Statements 22/26
84.62% Branches 11/13
80% Functions 8/10
83.33% Lines 20/24
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                                                                                                                                                      50× 50× 10× 10× 10×         40×     40×                                                   950× 26× 26×       26×                          
import Cookies from 'js-cookie';
import { getCookieDomain } from './utils';
 
export const COUNTRY_COOKIE_NAME = 'Geo';
const MISSING_COOKIE_NAME = 'no-cookie';
 
// client.geo.country_code https://docs.fastly.com/guides/vcl/geolocation-related-vcl-features
const COUNTRIES_REQUIRING_PROMPT = [
    'ad', // Andorra
    'ai', // Anguilla
    'aw', // Aruba
    'ax', // Åland Islands
    'at', // Austria
    'be', // Belgium
    'bg', // Bulgaria
    'bm', // Bermuda
    'vg', // British Virgin Islands
    'bq', // Bonaire
    'br', // Brazil
    'ky', // Cayman Islands
    'cn', // China
    'hr', // Croatia
    'cw', // Curaçao
    'cy', // Cyprus
    'cz', // Czech Republic
    'dk', // Denmark
    'ee', // Estonia
    'fk', // Falkland Islands
    'fo', // Faroe Islands
    'fi', // Finland
    'fr', // France
    'gf', // French Guiana
    'pf', // French Polynesia
    'tf', // French Southern and Antarctic Lands
    'de', // Germany
    'gi', // Gibraltar
    'gr', // Greece
    'gl', // Greenland
    'gp', // Guadeloupe
    'hu', // Hungary
    'is', // Iceland
    'ie', // Ireland
    'im', // Isle of Man
    'it', // Italy
    're', // La Réunion
    'lv', // Latvia
    'li', // Liechtenstein
    'lt', // Lithuania
    'lu', // Luxembourg
    'mq', // Martinique
    'mt', // Malta
    'yt', // Mayotte
    'ms', // Montserrat
    'nl', // Netherlands
    'nc', // New Caledonia
    'no', // Norway
    'pn', // Pitcairn Islands
    'pl', // Poland
    'pt', // Portugal
    'ro', // Romania
    'bl', // Saint Barthélemy
    'sh', // Saint Helena
    'mf', // Saint Martin
    'pm', // Saint-Pierre-et-Miquelon
    'bq', // Sint Eustatius
    'sx', // Sint Maarten
    'sk', // Slovakia
    'si', // Slovenia
    'es', // Spain
    'se', // Sweden
    'ch', // Switzerland
    'tc', // Turks and Caicos Islands
    'gb', // United Kingdom of Great Britain and Northern Ireland
    'uk', // United Kingdom of Great Britain and Northern Ireland
    'wf', // Wallis-et-Futuna
];
const COUNTRIES_WITH_REJECT_ALL_FUNCTIONALITY = ['fr'];
 
function getGeoDataFromCookie(type = 'country') {
    const cookie = Cookies.get(COUNTRY_COOKIE_NAME);
    if (cookie) {
        try {
            const obj = JSON.parse(cookie);
            return obj[type];
        } catch (e) {
            console.error('error parsing geo cookie', cookie);
        }
    } else {
        console.warn('no geo cookie found');
    }
 
    return false;
}
 
export function ensureGeoCookie() {
    Iif (Cookies.get(COUNTRY_COOKIE_NAME)) {
        return Promise.resolve();
    }
 
    const GEO_SERVICE_URL = 'https://services.fandom.com/geoip/location';
 
    return fetch(GEO_SERVICE_URL).then(response => response.json())
        .then(geoResponse => {
            return {
                continent: geoResponse.continent_code,
                country: geoResponse.country_code,
                region: geoResponse.region,
                city: geoResponse.city,
                country_name: geoResponse.country_name
            };
        })
        .then((geoData) => {
            Cookies.set('Geo', JSON.stringify(geoData), {
                domain: getCookieDomain(window.location.hostname),
                expires: 365,
                path: '/',
            });
        });
}
 
class GeoManager {
    constructor(country, region, countriesRequiringPrompt) {
        this.geosRequiringPrompt = (countriesRequiringPrompt || COUNTRIES_REQUIRING_PROMPT).map(country => country.toLowerCase());
        this.country = (country || getGeoDataFromCookie('country') || MISSING_COOKIE_NAME).toLowerCase();
        this.region = (region || getGeoDataFromCookie('region') || MISSING_COOKIE_NAME).toLowerCase();
    }
 
    hasSpecialPrivacyLaw() {
        return this.geosRequiringPrompt.indexOf(this.country) !== -1;
    }
 
    hasGeoCookie() {
        return this.country !== MISSING_COOKIE_NAME;
    }
 
    hasRejectAllFunctionality() {
        return COUNTRIES_WITH_REJECT_ALL_FUNCTIONALITY.includes(this.country);
    }
}
 
export default GeoManager;