const header = document.querySelector("#main-header") const cookieName = "environment" const privateCookie = "" const publicCookie = "" async function RenderHeader() { const response = await fetch(`/api/v1/user`); userData = await response.json(); let environment = document.cookie .split("; ") .find((row) => row.startsWith("environment=")) ?.split("=")[1]; if (!environment || (environment !== privateCookie && environment !== publicCookie)) { environment = privateCookie setCookie(cookieName, privateCookie, 365) } let text = environment === privateCookie ? "Go to public" : "Go to private" header.innerHTML = ` User avatar User avatar
${userData.login}
${text}
` let button = header.querySelector(".header__env_button") button.addEventListener('click', async (e) => { setCookie(cookieName, environment === privateCookie ? publicCookie : privateCookie, 365) window.location.href = `/` }) console.log(environment) } function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; }