HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/resources/assets/js/site/Announcements.js
export default class Announcements {
    constructor(element) {
        this.element = element;
        this.fadeInDelay = 600;
        this.country = null;
        this.announcements = [];

        this.getAllAnnouncements();
        this.getCountryByIp().then(() => {
            this.showAnnouncements();
        });
    }

    // PHP equivalent: app/Komma/Announcements/AnnouncementCountrySetting.php
    static CountrySetting = {
        'ALL': 0,
        'SPECIFIC': 1,
        'EXCEPT': 2
    }

    getAllAnnouncements = () => this.announcements = [...this.element.querySelectorAll('.js-announcements-item')];
    getCountryByIp = async () => {
        try {
            const response = await fetch('/api/country');
            this.country = await response.json();
        } catch (error) {
            console.log(error);
        }
    }

    showAnnouncements = () => {
        if (this.country) {
            for (const announcement of this.announcements) {
                const countrySetting = parseInt(announcement.dataset.countrySetting);

                const countryIds = announcement.dataset.countries
                    ?.split(',')
                    .map(id => parseInt(id));

                switch (countrySetting) {
                    case Announcements.CountrySetting.ALL:
                        announcement.shouldFadeIn = true;
                        break;
                    case Announcements.CountrySetting.SPECIFIC:
                        announcement.shouldFadeIn = countryIds.includes(this.country.id);
                        break;
                    case Announcements.CountrySetting.EXCEPT:
                        debugger
                        announcement.shouldFadeIn = !countryIds.includes(this.country.id);
                        break;
                    default:
                        announcement.shouldFadeIn = false;
                        break;
                }
            }
        }

        this.announcements.forEach((announcement, index) => {
            if (!announcement.shouldFadeIn) return;
            setTimeout(() => {
                announcement.classList.remove('hidden');
            }, index * this.fadeInDelay);
        });
    }

}