diff --git a/css/main.css b/css/main.css index 82650248..ee6ffc8b 100644 --- a/css/main.css +++ b/css/main.css @@ -11,6 +11,11 @@ --color-events: #e30162; --color-button: #87d870; --color-button-hover: #6dc66d; + --role-button-bg: #008000; + --role-button-text: #ffffff; + --loader-gray-color: #f3f3f3; + --loader-blue-color: #1d1283; + --role-button-disabled: #ccc; } .dark-theme { @@ -539,6 +544,98 @@ footer { } } +.roles-container { + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color: rgba(0, 0, 0, 0.5); + justify-content: center; + align-items: center; + z-index: 200; +} + +.role-details { + background-color: var(--color-bg); + border-radius: 5px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); + padding: 2rem; + max-width: 50%; + margin: auto; +} + +.role-heading { + font-size: 2rem; + margin-bottom: 0; + text-align: center; +} + +.role-details-field { + display: flex; + flex-direction: column; + justify-content: center; + padding: 2rem 4rem; +} + +.checkbox-label { + display: flex; + width: 12rem; + margin: 0 auto; + font-size: 1.3rem; +} + +.checkbox-label .checkbox-input { + margin-right: 0.4rem; +} + +.role-button { + position: relative; +} + +.role-button button { + padding: 0.75rem 2rem; + border-radius: 10px; + outline: none; + background-color: var(--role-button-bg); + color: var(--role-button-text); + text-decoration: none; + cursor: pointer; + width: 75%; + display: block; + margin: auto; + font-size: 2rem; + border: none; +} + +.spinner { + position: absolute; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + display: none; + width: 20px; + height: 20px; + border: 3px solid var(--loader-gray-color); + border-top: 3px solid var(--loader-blue-color); + border-radius: 50%; + animation: spin 1s linear infinite; +} + +.role-button button:disabled { + background-color: var(--role-button-disabled); + cursor: not-allowed; +} + +@keyframes spin { + 0% { + transform: translate(-50%, -50%) rotate(0deg); + } + 100% { + transform: translate(-50%, -50%) rotate(360deg); + } +} + /* Media Queries */ @media (min-width: 420px) { @@ -585,6 +682,10 @@ footer { .card { padding: 20px 10px; } + .role-details { + max-width: 100%; + border-radius: 0; + } } @media screen and (max-width: 800px) { diff --git a/js/goto.js b/js/goto.js index 0c1c6544..35e4c238 100644 --- a/js/goto.js +++ b/js/goto.js @@ -7,8 +7,20 @@ function redirectUserToPage(page) { } function redirectionHandler(data) { + const isDeveloper = data.roles.developer; + const isDesigner = data.roles.designer; + const isMaven = data.roles.maven; + const isProductManager = data.roles.productmanager; if (data.incompleteUserDetails) { redirectUserToPage(SIGNUP_URL); + } else if ( + data.incompleteUserDetails === false && + !isDeveloper && + !isDesigner && + !isMaven && + !isProductManager + ) { + redirectUserToPage(HOME_URL); } else if (hasVisitedJoin == 'true' || hasVisitedJoin == null) { redirectUserToPage(`${HOME_URL}/join`); } else { diff --git a/js/index.js b/js/index.js index efce494e..391a5278 100644 --- a/js/index.js +++ b/js/index.js @@ -1,3 +1,5 @@ +import { SELF_URL } from './constants.js'; + const selectRandom = (memberImgArr, n) => { const result = new Set(); const len = memberImgArr.length; @@ -101,3 +103,165 @@ modalTriggers.forEach((trigger) => { }); }); }); + +function popup(data) { + const isDeveloper = data.roles.developer; + const isDesigner = data.roles.designer; + const isMaven = data.roles.maven; + const isProductManager = data.roles.productmanager; + if ( + data.incompleteUserDetails === false && + !isDeveloper && + !isDesigner && + !isMaven && + !isProductManager + ) { + const popupContainer = document.createElement('div'); + popupContainer.className = 'roles-container'; + + const roleDetails = document.createElement('div'); + roleDetails.className = 'role-details'; + + const roleHeading = document.createElement('h2'); + roleHeading.className = 'role-heading'; + roleHeading.textContent = 'Select your role'; + + const roleDetailsField = document.createElement('div'); + roleDetailsField.className = 'role-details-field'; + + const roleButton = document.createElement('div'); + roleButton.className = 'role-button'; + + const submitButton = document.createElement('button'); + submitButton.textContent = 'Submit'; + + const spinner = document.createElement('span'); + spinner.className = 'spinner'; + submitButton.appendChild(spinner); + + roleButton.appendChild(submitButton); + + roleDetails.appendChild(roleHeading); + roleDetails.appendChild(roleDetailsField); + roleDetails.appendChild(roleButton); + + popupContainer.appendChild(roleDetails); + popupContainer.style.display = 'none'; + const mainElement = document.querySelector('main'); + const firstChild = mainElement.firstChild; + mainElement.insertBefore(popupContainer, firstChild); + + const roles = {}; + + const labels = [ + { name: 'Developer', inputName: 'developer' }, + { name: 'Designer', inputName: 'designer' }, + { name: 'Maven', inputName: 'maven' }, + { name: 'Product Manager', inputName: 'productmanager' }, + ]; + + function createCheckbox(labelInfo) { + const label = document.createElement('label'); + label.className = 'checkbox-label'; + + const input = document.createElement('input'); + input.type = 'checkbox'; + input.name = labelInfo.inputName; + input.className = 'checkbox-input'; + + const labelText = document.createTextNode(labelInfo.name); + + label.appendChild(input); + label.appendChild(labelText); + return label; + } + + labels.forEach((labelInfo) => { + const checkbox = createCheckbox(labelInfo); + roleDetailsField.appendChild(checkbox); + }); + + const registerUserRoles = async (roles) => { + spinner.style.display = 'inline-block'; + submitButton.style.backgroundColor = '#ccc'; + const updateRoles = { + roles: { + ...data.roles, + ...roles, + }, + }; + const res = await fetch(`${SELF_URL}`, { + method: 'PATCH', + body: JSON.stringify(updateRoles), + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + }); + return res; + }; + + async function hidePopup() { + const response = await registerUserRoles(roles); + if (response.status === 204) { + spinner.style.display = 'none'; + submitButton.style.backgroundColor = '#008000'; + submitButton.disabled = false; + popupContainer.style.display = 'none'; + } else if (response.status === 401) { + alert('You are not logged in! Redirecting you to login.'); + location.href = GITHUB_OAUTH; + } else { + alert('Something went wrong please contact admin'); + } + } + + function updateRoles(event) { + const checkbox = event.target; + const name = checkbox.name; + + if (checkbox.checked) { + roles[name] = true; + } else { + delete roles[name]; + } + const anyCheckboxChecked = Object.keys(roles).length > 0; + submitButton.disabled = !anyCheckboxChecked; + } + + window.addEventListener('load', function () { + popupContainer.style.display = 'flex'; + submitButton.disabled = true; + const checkboxes = document.querySelectorAll('.checkbox-input'); + checkboxes.forEach((checkbox) => { + checkbox.addEventListener('change', updateRoles); + }); + }); + + submitButton.addEventListener('click', hidePopup); + } else { + const popupContainer = document.querySelector('.roles-container'); + popupContainer.style.display = 'none'; + } +} + +function userData() { + fetch(`${SELF_URL}`, { + headers: { 'content-type': 'application/json' }, + credentials: 'include', + }) + .then((res) => res.json()) + .then((data) => { + popup(data); + }) + .catch((err) => { + console.error(err); + }); +} + +const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); + +if (urlParams.get('dev') === 'true') { + userData(); +}