-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from rtCamp/feature/demo-user-preference
Demo user preference
- Loading branch information
Showing
8 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<%- include(commonPath + '/header.ejs') %> | ||
<div id="theme-container"> | ||
<%- include(commonPath + '/internal-page/header.ejs') %> | ||
<p class="text-lg mb-4 text-center">Here you can set you prefered theme.</p> | ||
|
||
<div class="dark-mode-toggle"> | ||
<input type="checkbox" id="dark-mode-switch" onclick="toggleTheme()" /> | ||
<label for="dark-mode-switch"> | ||
<span class="bullet"> | ||
<svg xmlns="http://www.w3.org/2000/svg" class="sun" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zM2 13h2c.55 0 1-.45 1-1s-.45-1-1-1H2c-.55 0-1 .45-1 1s.45 1 1 1zm18 0h2c.55 0 1-.45 1-1s-.45-1-1-1h-2c-.55 0-1 .45-1 1s.45 1 1 1zM11 2v2c0 .55.45 1 1 1s1-.45 1-1V2c0-.55-.45-1-1-1s-1 .45-1 1zm0 18v2c0 .55.45 1 1 1s1-.45 1-1v-2c0-.55-.45-1-1-1s-1 .45-1 1zM5.99 4.58a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0s.39-1.03 0-1.41L5.99 4.58zm12.37 12.37a.996.996 0 0 0-1.41 0 .996.996 0 0 0 0 1.41l1.06 1.06c.39.39 1.03.39 1.41 0a.996.996 0 0 0 0-1.41l-1.06-1.06zm1.06-10.96a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06zM7.05 18.36a.996.996 0 0 0 0-1.41.996.996 0 0 0-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06z"/></svg> | ||
<svg xmlns="http://www.w3.org/2000/svg" class="moon" width="24" height="24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 3a9 9 0 1 0 9 9c0-.46-.04-.92-.1-1.36a5.389 5.389 0 0 1-4.4 2.26 5.403 5.403 0 0 1-3.14-9.8c-.44-.06-.9-.1-1.36-.1z"/></svg> | ||
</span> | ||
</label> | ||
</div> | ||
<%- include(commonPath + '/internal-page/footer.ejs') %> | ||
</div> | ||
|
||
<script src="<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/user-preferences/user-preference.js"></script> | ||
|
||
<%- include(commonPath + '/footer.ejs') %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const router = express.Router(); | ||
|
||
router.get('/', (req, res) => { | ||
res.render(path.join(__dirname,'index'), { | ||
title: 'User Preferences' | ||
}); | ||
}); | ||
|
||
router.get( '/get-user-preference', ( req, res ) => { | ||
const currentTheme = req.cookies.theme || 'light'; | ||
res.json( { theme: currentTheme }); | ||
}); | ||
|
||
router.post( '/set-user-preference', ( req, res ) => { | ||
const { theme } = req.body; | ||
|
||
if (!theme) { | ||
res.status(400).send({ message: 'Invalid request' }); | ||
|
||
} | ||
|
||
res.cookie('theme', theme, { | ||
domain: res.locals.domainC, | ||
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days | ||
httpOnly: true, | ||
sameSite: "none", | ||
secure: true | ||
}); | ||
res.status(200).send({ message: 'Success', theme : theme}); | ||
}); | ||
|
||
// Serve the user-preference.js file to the site | ||
router.get('/user-preference.js', (req, res) => { | ||
res.set('Content-Type', 'application/javascript'); | ||
res.render(path.join(__dirname,'user-preference')); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const baseURL = '<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/user-preferences'; | ||
const pageContainer = document.getElementById('theme-container'); | ||
const themeSwitcher = document.getElementById('dark-mode-switch'); | ||
|
||
function updateUserPreference() { | ||
fetch(`${baseURL}/get-user-preference`) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
return response.json(); | ||
}) | ||
.then(data => { | ||
const theme = data.theme; | ||
console.log(data.theme) | ||
pageContainer.className = theme; | ||
themeSwitcher.checked = theme === 'dark'; | ||
}) | ||
.catch(error => { | ||
console.error('There has been a problem with your fetch operation:', error); | ||
}); | ||
} | ||
|
||
function toggleTheme() { | ||
fetch( `${baseURL}/set-user-preference`, { | ||
method: 'POST', | ||
credentials: 'include', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ theme: themeSwitcher?.checked ? 'dark' : 'light' }) | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
pageContainer.className = data.theme; | ||
}); | ||
} | ||
window.toggleTheme = toggleTheme; | ||
updateUserPreference(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,92 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
@tailwind utilities; | ||
|
||
@layer base { | ||
|
||
:root { | ||
--bullet-toggle-size: 46px; | ||
--toogle-width: 100px; | ||
--toogle-height: 54px; | ||
--light-mode-background: #f1c40f; | ||
--dark-mode-background: #025BDF; | ||
--dark-container-background: #3F3F3F; | ||
--dark-container-text: #fff; | ||
} | ||
|
||
.dark-mode-toggle { | ||
position: relative; | ||
display: inline-block; | ||
width: var(--toogle-width); | ||
height: var(--toogle-height); | ||
} | ||
|
||
.dark-mode-toggle input { | ||
opacity: 0; | ||
width: 0; | ||
height: 0; | ||
} | ||
|
||
.dark-mode-toggle label { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
background-color: #EEE; | ||
border-radius: 30px; | ||
cursor: pointer; | ||
transition: all 0.2s ease-in-out; | ||
} | ||
|
||
.dark .dark-mode-toggle label { | ||
background-color: #fff; | ||
} | ||
|
||
.dark-mode-toggle label .bullet { | ||
content: ""; | ||
position: absolute; | ||
top: 50%; | ||
left: 5%; | ||
transform: translate(0, -50%); | ||
width: var(--bullet-toggle-size); | ||
aspect-ratio: 1; | ||
background-color: var(--light-mode-background); | ||
border-radius: 50%; | ||
transition: background-color 0.2s ease-in-out; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.dark-mode-toggle input:checked + label .bullet { | ||
background-color: var(--dark-mode-background); | ||
left: 95%; | ||
transform: translate(-100%, -50%); | ||
} | ||
|
||
.sun, | ||
.moon { | ||
position: relative; | ||
z-index: 100; | ||
fill: #fff; | ||
} | ||
|
||
.moon { | ||
display: none; | ||
} | ||
|
||
.dark-mode-toggle input:checked + label .moon { | ||
display: block; | ||
} | ||
|
||
.dark-mode-toggle input:checked + label .sun { | ||
display: none; | ||
} | ||
|
||
.dark .page-card { | ||
background-color: var(--dark-container-background); | ||
color: var(--dark-container-text); | ||
} | ||
|
||
} |