Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions components/Dark-Theme/useDarkMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ export const useDarkMode = () => {
const [theme, setTheme] = useState('light');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move 'light' to constant file

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names are not clear. It should be 'mode'. Which mode we are setting? Light and dark. We are not getting theme here

const [themeData, setThemeData] = useState(lightTheme);
const [mountedComponent, setMountedComponent] = useState(false);
const setCookie = (name, value, days) => {
const domain = '.realdevsquad.com';
const expires = new Date(Date.now() + 24 * days * 60 * 60 * 1000);
const cookieStr = `${name}=${value}; expires=${expires}; domain=${domain}; path=/`;
document.cookie = cookieStr;
};
const accessCookie = (cookieName) => {
const name = `${cookieName}=`;
const allCookieArray = document.cookie.split(';');
for (let i = 0; i < allCookieArray.length; i += 1) {
const temp = allCookieArray[i].trim();
if (temp.indexOf(name) === 0)
return temp.substring(name.length, temp.length);
}
return '';
};
const setMode = (mode) => {
window.localStorage.setItem('theme', mode);
setCookie('theme', mode, 30);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move theme, and 30 to constant

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for using cookie and not localStorage here?

setTheme(mode);
const themeMode = mode === 'light' ? lightTheme : darkTheme;
setThemeData(themeMode);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should be change to setThemeStyle

Expand All @@ -18,14 +34,13 @@ export const useDarkMode = () => {
};

useEffect(() => {
const localTheme = window.localStorage.getItem('theme');
const localTheme = accessCookie('theme');
const themeMode = localTheme === 'light' ? lightTheme : darkTheme;
if (localTheme) {
setTheme(localTheme);
setThemeData(themeMode);
}
setMountedComponent(true);
console.log('here');
}, []);
return [theme, themeData, themeToggler, mountedComponent];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return the Object from here. This will help us to destruct values based on keys. Then we can avoid getting all values (even if we don't need them) and we can get them in any order

};