Skip to content

Commit

Permalink
auto save of the user checklist items
Browse files Browse the repository at this point in the history
  • Loading branch information
OchiengPaul442 committed Nov 21, 2023
1 parent b3b5a90 commit 9f0d6d1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions platform/src/common/components/Layout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { fetchUserChecklists } from '@/lib/store/services/checklists/CheckData';
import { updateCards } from '@/lib/store/services/checklists/CheckList';
import Head from 'next/head';
import { updateUserChecklists } from '@/lib/store/services/checklists/CheckData';

const Layout = ({ pageTitle = 'AirQo Analytics', children, topbarTitle, noBorderBottom }) => {
// Constants
Expand All @@ -27,6 +28,7 @@ const Layout = ({ pageTitle = 'AirQo Analytics', children, topbarTitle, noBorder
const [collapsed, setCollapsed] = useState(
() => JSON.parse(localStorage.getItem('collapsed')) || false,
);
const cardCheckList = useSelector((state) => state.cardChecklist.cards);

// Fetching user preferences
useEffect(() => {
Expand Down Expand Up @@ -77,23 +79,36 @@ const Layout = ({ pageTitle = 'AirQo Analytics', children, topbarTitle, noBorder
}, [userInfo, userPreferences, dispatch]);

// Fetching user checklists
useEffect(() => {
const fetchData = () => {
if (userInfo?._id && !localStorage.getItem('dataFetched')) {
dispatch(fetchUserChecklists(userInfo._id)).then((action) => {
if (fetchUserChecklists.fulfilled.match(action)) {
const { payload } = action;
if (payload && payload.length > 0) {
const { items } = payload[0];
dispatch(updateCards(items));
localStorage.setItem('dataFetched', 'true');
} else {
localStorage.setItem('dataFetched', 'true');
return;
}
localStorage.setItem('dataFetched', 'true');
}
});
}
}, [dispatch, userInfo]);
};

useEffect(fetchData, [dispatch, userInfo]);

// update user checklists when ever the cardCheckList changes
const updateUserChecklist = () => {
if (userInfo?._id) {
dispatch(
updateUserChecklists({
user_id: userInfo._id,
items: cardCheckList,
}),
);
}
};

useEffect(updateUserChecklist, [dispatch, userInfo, cardCheckList]);

useEffect(() => {
localStorage.setItem('collapsed', collapsed);
Expand Down

0 comments on commit 9f0d6d1

Please sign in to comment.