header.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  2. <!-- <link rel="stylesheet" href="{{ " /css/main.css" | prepend: site.baseurl }}"> -->
  3. <style>
  4. .user-icon {
  5. display: inline-block;
  6. position: relative;
  7. }
  8. .dropdown_header {
  9. display: inline-block;
  10. position: relative;
  11. z-index: 1000;
  12. background-color: #FFC907;
  13. }
  14. .dropdown-content_header {
  15. display: none;
  16. position: absolute;
  17. width: auto;
  18. overflow: auto;
  19. box-shadow: 0px 10px 10px 0px rgba(0, 0, 0, 0.4);
  20. /* background-color: rgba(255, 255, 255, 0); */
  21. background-color: white;
  22. font-family: Reforma2018, serif;
  23. font-size: 1em;
  24. }
  25. .dropdown_header.active .dropdown-content_header {
  26. display: block;
  27. }
  28. .dropdown-content_header a {
  29. display: block;
  30. color: #000000;
  31. padding: 5px;
  32. text-decoration: none;
  33. }
  34. .dropdown-content_header a:hover {
  35. color: #FFFFFF;
  36. background-color: #FFC907;
  37. }
  38. .dropdown_header:hover {
  39. /* Resetting styles for hover state */
  40. border: none;
  41. box-shadow: none;
  42. display: block;
  43. /* Or any other appropriate display value */
  44. grid-template-columns: initial;
  45. /* Or any other appropriate value */
  46. }
  47. /* Style for the modal */
  48. #navBar.modal {
  49. display: flex;
  50. flex-direction: column;
  51. align-items: center;
  52. }
  53. #emailCode,
  54. #emailCodeVerify {
  55. display: flex;
  56. flex-direction: row;
  57. align-items: center;
  58. margin-bottom: 10px;
  59. }
  60. .modal {
  61. display: none;
  62. position: fixed;
  63. top: 50%;
  64. left: 50%;
  65. transform: translate(-50%, -50%);
  66. padding: 20px;
  67. background-color: white;
  68. border: 2px solid #ccc;
  69. z-index: 2000;
  70. width: 60%;
  71. height: 20%;
  72. text-align: center;
  73. font-family: Reforma2018, serif;
  74. font-size: 1em;
  75. }
  76. /* Additional styling for buttons */
  77. .btn_login_logout {
  78. position: relative;
  79. border-radius: 4px;
  80. background-color: transparent;
  81. border: 1px solid #29335C;
  82. font-family: Reforma2018, serif;
  83. margin: 10px;
  84. padding-left: 2px;
  85. padding-right: 2px;
  86. }
  87. .btn_login_logout.primary_login_logout {
  88. background-color: #FFC907;
  89. color: black;
  90. border: 1px solid #d3a500;
  91. }
  92. .btn_login_logout.is-large_login_logout {
  93. font-size: 1em;
  94. text-align: center;
  95. }
  96. .btn_login_logout:hover {
  97. cursor: pointer;
  98. background-color: #d3a500
  99. }
  100. </style>
  101. <header class="site-header">
  102. <div class="wrapper">
  103. <a class="site-title" href="{{ site.baseurl }}/">{{ site.title }}</a>
  104. <img src="{% link assets/elements/icon_do.svg %}" alt="do-ocracy icon"
  105. style="height:40px; padding: 5px 0 0 20px;" />
  106. <nav class="site-nav">
  107. <a href="#" class="menu-icon">
  108. <svg viewBox="0 0 18 15">
  109. <path fill="#424242"
  110. d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.031C17.335,0,18,0.665,18,1.484L18,1.484z" />
  111. <path fill="#424242"
  112. d="M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0c0-0.82,0.665-1.484,1.484-1.484 h15.031C17.335,6.031,18,6.696,18,7.516L18,7.516z" />
  113. <path fill="#424242"
  114. d="M18,13.516C18,14.335,17.335,15,16.516,15H1.484C0.665,15,0,14.335,0,13.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.031C17.335,12.031,18,12.696,18,13.516L18,13.516z" />
  115. </svg>
  116. </a>
  117. <div class="trigger" style="display: inline-block;">
  118. {% for my_page in site.pages %}
  119. {% if my_page.title %}
  120. <a class="page-link{% if page.url == my_page.url %} page-link-active{% endif %}"
  121. href="{{ my_page.url | prepend: site.baseurl }}">{{ my_page.title }}</a>
  122. {% endif %}
  123. {% endfor %}
  124. <div class="user-icon" id="loginLogoutButton">
  125. <div id="login">
  126. <button class="btn_login_logout primary_login_logout is-large_login_logout" onclick="showLoginModal()"
  127. style="height: 42px; width: 100px; padding: 2px; text-align: center">
  128. <img src="{% link assets/tabler_icons/login.svg %}" alt="Login"> Login
  129. </button>
  130. </div>
  131. <div id="logout" class="dropdown_header">
  132. <button class="btn_login_logout primary_login_logout is-large_login_logout" onclick="toggleDropdown()"
  133. style="height: 40px; width: 40px; padding: 2px; text-align: center">
  134. <img src="{% link assets/tabler_icons/user.svg %}" alt="Login">
  135. </button>
  136. <div class="dropdown-content_header">
  137. <div id="emailText"
  138. style="padding: 10px; padding-top: 20px; margin-top: 1px; margin-bottom: 1px; height: 10 px">
  139. </div>
  140. <a href="#" style="padding: 10px; margin-top: 1px; margin-bottom: 1px; height: 10 px"
  141. @click="logout">Logout</a>
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. </div>
  147. <div id="navBar" class="modal" style="display: none;">
  148. <div id="emailCode" style="display:block; align-items: center;">
  149. <label for="emailInput">Enter your email:</label>
  150. <input type="email" id="emailInput" v-model="loginEmail" required>
  151. <button class="btn_login_logout primary_login_logout is-large_login_logout" @click="sendOTP"
  152. style="text-align:center;">Send Code</button>
  153. </div>
  154. <div id="emailCodeVerify" style="display: none;">
  155. <label for="otpInput">Enter Code to Login:</label>
  156. <input type="text" id="otpInput" v-model="otpCode" required>
  157. <button class="btn_login_logout primary_login_logout is-large_login_logout" @click="verifyOTP">Verify
  158. Code</button>
  159. <button class="btn_login_logout primary_login_logout is-large_login_logout" @click="sendOTPAagin"> Send Code
  160. Again</button>
  161. </div>
  162. <button class="btn_login_logout primary_login_logout is-large_login_logout" @click="closeModal">Close</button>
  163. </div>
  164. </div>
  165. </div>
  166. </nav>
  167. </div>
  168. </header>
  169. <script>
  170. function toggleDropdown() {
  171. var dropdown = document.getElementById('logout');
  172. dropdown.classList.toggle('active');
  173. var myDiv = document.getElementById('emailText'); // Get the div element by ID
  174. myDiv.textContent = localStorage.getItem('userEmail');
  175. }
  176. function showLoginModal() {
  177. // Show the modal and overlay
  178. document.getElementById('navBar').style.display = 'block';
  179. document.getElementById('overlay').style.display = 'block';
  180. }
  181. function logout() {
  182. // Remove email from local storage
  183. localStorage.removeItem('userEmail');
  184. // Hide the dropdown and overlay
  185. document.getElementById('login').style.display = 'block';
  186. document.getElementById('logout').style.display = 'none';
  187. document.getElementById('emailCode').style.display = 'block';
  188. document.getElementById('emailCodeVerify').style.display = 'none';
  189. window.location.reload();
  190. }
  191. function showLoginModal() {
  192. document.getElementById('navBar').style.display = 'block';
  193. // document.getElementById('overlay').style.display = 'block';
  194. }
  195. function hideLoginModal() {
  196. document.getElementById('navBar').style.display = 'none';
  197. document.getElementById('overlay').style.display = 'none';
  198. }
  199. function isValidEmail(email) {
  200. // Regular expression for a basic email validation
  201. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  202. return emailRegex.test(email);
  203. }
  204. new Vue({
  205. el: '#loginLogoutButton',
  206. data: {
  207. email: localStorage.getItem('userEmail') || null,
  208. showDropdown: false,
  209. },
  210. mounted() {
  211. // Retrieve email from local storage on component mount
  212. this.email = localStorage.getItem('userEmail');
  213. if (this.email) {
  214. document.getElementById('login').style.display = 'none';
  215. document.getElementById('logout').style.display = 'block';
  216. } else {
  217. document.getElementById('login').style.display = 'block';
  218. document.getElementById('logout').style.display = 'none';
  219. }
  220. },
  221. });
  222. new Vue({
  223. el: '#navBar',
  224. data: {
  225. loginEmail: '',
  226. email: localStorage.getItem('userEmail'),
  227. otpCode: "",
  228. backendUrlOtp: "http://localhost:3000/api/send_otp",
  229. backendUrlValidateOtp: "http://localhost:3000/api/validate_otp"
  230. },
  231. methods: {
  232. sendOTPAagin() {
  233. document.getElementById('emailCode').style.display = 'block';
  234. document.getElementById('emailCodeVerify').style.display = 'none';
  235. },
  236. sendOTP() {
  237. const requestData = {
  238. email: this.loginEmail
  239. }
  240. if (isValidEmail(this.loginEmail)) {
  241. fetch(this.backendUrlOtp, {
  242. method: 'POST',
  243. headers: {
  244. 'Content-Type': 'application/json',
  245. 'Access-Control-Allow-Methods': 'POST',
  246. 'Access-Control-Allow-Headers': 'Content-Type'
  247. // Add any other headers you may need, such as authorization headers
  248. },
  249. body: JSON.stringify(requestData),
  250. }).then(response => {
  251. if (!response.ok) {
  252. alert('Error while sending OTP');
  253. } else {
  254. alert(`Code sent to: ${this.loginEmail}`);
  255. document.getElementById('emailCode').style.display = 'none';
  256. document.getElementById('emailCodeVerify').style.display = 'block';
  257. }
  258. })
  259. } else {
  260. alert(`Please enter valid email address`);
  261. }
  262. },
  263. verifyOTP() {
  264. const requestData = {
  265. email: this.loginEmail,
  266. otp: this.otpCode
  267. }
  268. fetch(this.backendUrlValidateOtp, {
  269. method: 'POST',
  270. headers: {
  271. 'Content-Type': 'application/json',
  272. 'Access-Control-Allow-Methods': 'POST',
  273. 'Access-Control-Allow-Headers': 'Content-Type'
  274. // Add any other headers you may need, such as authorization headers
  275. },
  276. body: JSON.stringify(requestData),
  277. }).then(response => {
  278. if (response.status == 500) {
  279. alert('Error while validating code');
  280. } else if (response.status == 200) {
  281. alert('Logged in successfully');
  282. localStorage.setItem('userEmail', this.loginEmail)
  283. document.getElementById('login').style.display = 'none';
  284. document.getElementById('logout').style.display = 'block';
  285. document.getElementById('logout').classList.remove('active');
  286. this.closeModal();
  287. } else {
  288. alert('Invalid code, please enter valid code');
  289. }
  290. })
  291. },
  292. closeModal() {
  293. document.getElementById('navBar').style.display = 'none';
  294. },
  295. },
  296. });
  297. </script>