Skip to content

Commit

Permalink
SPF-18: create function to make the watchlists persist in the local s…
Browse files Browse the repository at this point in the history
…torage
  • Loading branch information
carrawao committed Mar 30, 2022
1 parent b1d3943 commit 42fc173
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions Frontend/src/routes/index.jsx
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;

0 comments on commit 42fc173

Please sign in to comment.