Moved completed tutorial states to session and local storage.

This commit is contained in:
Lucid Kobold
2022-06-21 16:16:17 -05:00
parent 181c399899
commit 9506e5b43c
9 changed files with 182 additions and 35 deletions

View File

@@ -1,16 +1,27 @@
import React, { Fragment, useEffect, useRef, useState } from "react";
import { Box } from "@chakra-ui/react";
import { addMonths, format } from "date-fns";
import { format } from "date-fns";
import Calender from "../components/calender";
import Tutorial from "../components/tutorial";
import LoadingOverlay from "../components/loading/LoadingOverlay";
import { Provider } from "react-redux";
import { store } from "../app/store";
import { useAppDispatch, useAppSelector } from "../app/hooks";
import { updateLoading } from "../features/calender/calender";
import { updateLoading } from "../features/calender";
import {
getAndSetTutorial,
setTempTutorialComplete,
setTutorialCompleted
} from "../features/tutorial";
const IndexPage = (): JSX.Element => {
const isLoading = useAppSelector((state) => state.calender.isLoading);
const completedTutorial = useAppSelector(
(state) => state.tutorial.completedTutorial
);
const tutorialCompletionInfo = useAppSelector(
(state) => state.tutorial.storageState
);
const dispatch = useAppDispatch();
const currDate = useRef<UpdateCalenderPropsDateLayout>({
@@ -19,25 +30,26 @@ const IndexPage = (): JSX.Element => {
day: parseInt(format(new Date(), "d"))
});
const [completedTutorial, setCompletedTutorial] = useState<boolean | null>(
null
);
const getTutorialCookie = (): boolean => {
return JSON.parse(localStorage.getItem("tutorialCompleted")) || false;
const handleTempTutorialCompleted = (): void => {
dispatch(setTempTutorialComplete());
};
const setTutorialCookie = (bool: boolean): void => {
localStorage.setItem("tutorialCompleted", `${bool}`);
setCompletedTutorial(true);
const handleTutorialCompleted = (): void => {
dispatch(setTutorialCompleted());
};
useEffect(() => {
if (completedTutorial === null || tutorialCompletionInfo === null) {
dispatch(getAndSetTutorial());
dispatch(updateLoading(false));
}
if (completedTutorial !== null) {
dispatch(updateLoading(false));
}
setCompletedTutorial(getTutorialCookie());
}, [completedTutorial, dispatch]);
console.info("use effect", completedTutorial, tutorialCompletionInfo);
}, [completedTutorial, dispatch, tutorialCompletionInfo]);
return (
<Box
@@ -49,7 +61,7 @@ const IndexPage = (): JSX.Element => {
minWidth="min-content"
>
<Provider store={store}>
{completedTutorial === null ? (
{isLoading === true ? (
<Fragment>
<LoadingOverlay />
<Calender date={currDate.current} isLoading={isLoading} />
@@ -57,7 +69,10 @@ const IndexPage = (): JSX.Element => {
) : completedTutorial ? (
<Calender date={currDate.current} isLoading={isLoading} />
) : (
<Tutorial setTutorialCookie={setTutorialCookie} />
<Tutorial
setTutorialComplete={handleTutorialCompleted}
setTempTutorialComplete={handleTempTutorialCompleted}
/>
)}
</Provider>
</Box>