-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPF-18: create function to make the watchlists persist in the local s…
…torage
- Loading branch information
Showing
1 changed file
with
39 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,53 @@ | ||
import React, {lazy} from "react"; | ||
import { Route, Routes } from 'react-router-dom'; | ||
import React, {lazy, useEffect, useState} from "react"; | ||
import {Route, Routes} from 'react-router-dom'; | ||
|
||
/** | ||
* Optional the component could load lazily, allowing to borrow more | ||
* time for it to completely load | ||
*/ | ||
const Home = lazy(() => import("../components/Home")); | ||
const About = lazy(() => import("../components/About")); | ||
const Home = lazy(() => import('../components/screens/Home')); | ||
const DashboardScreen = lazy(() => import('../components/screens/Dashboard/DashboardScreen')); | ||
const WatchListsScreen = lazy(() => import('../components/screens/WatchLists/WatchListsScreen')); | ||
const SettingsScreen = lazy(() => import('../components/screens/Settings/SettingsScreen')); | ||
|
||
/** | ||
* Get value from key in local storage | ||
* @param keyName | ||
* @param defaultValue | ||
* @returns {any|string} | ||
*/ | ||
const persistState = (keyName, defaultValue) => { | ||
const savedData = localStorage.getItem(keyName); | ||
return savedData !== '' ? JSON.parse(savedData) : defaultValue; | ||
}; | ||
|
||
/** | ||
* Defines the routes for the different pages | ||
* @constructor | ||
*/ | ||
let AppRoutes = () => ( | ||
const AppRoutes = () => { | ||
const [watchListsArray, setWatchListsArray] = useState(() => persistState('watchListsArray', [])); | ||
|
||
useEffect(() => { | ||
localStorage.setItem("watchListsArray", JSON.stringify(watchListsArray)); | ||
}, [watchListsArray]); | ||
|
||
return ( | ||
<Routes> | ||
<Route path="/" element={<Home />} /> | ||
<Route path="/about" element={<About />} /> | ||
<Route path='/' element={<Home/>}/> | ||
<Route path='/dashboard' element={<DashboardScreen/>}/> | ||
<Route | ||
path='/watchlists' | ||
element={ | ||
<WatchListsScreen | ||
watchListsArray={watchListsArray} | ||
setWatchListsArray={setWatchListsArray} | ||
/> | ||
} | ||
/> | ||
<Route path='/settings' element={<SettingsScreen/>}/> | ||
</Routes> | ||
); | ||
); | ||
} | ||
|
||
export default AppRoutes; |