Skip to content

Commit 7914f2c

Browse files
authored
Merge pull request #247 from Markkos89/staging
feat: small fixes and refactoring (to staging)
2 parents 9a02162 + 68cfc9e commit 7914f2c

File tree

5 files changed

+53
-39
lines changed

5 files changed

+53
-39
lines changed

src/components/NavBar.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ function NavBar() {
3434
const router = useRouter();
3535
// Hooks
3636
const { data: sessionData } = useSession();
37-
// const { data: secretMessage } = api.example.getSecretMessage.useQuery(
38-
// undefined, // no input
39-
// { enabled: sessionData?.user !== undefined }
40-
// );
37+
4138
// State
4239
const [showConnection, setShowConnection] = useState(false);
4340

src/components/mdx/Quiz.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
haveSameElements,
2020
} from "@/utils/QuizHelpers";
2121
import { api } from "@/utils/api";
22+
import { useAppContext } from "@/contexts/AppContext";
2223

2324
interface QuizProps {
2425
quiz: string;
@@ -51,6 +52,7 @@ const Quiz = (props: QuizProps): JSX.Element => {
5152
const [answers, setAnswers] = useState<Answers>({});
5253
const [correctAnswers, setCorrectAnswers] = useState<number[] | null>(null);
5354
const toast = useToast();
55+
const { refetchCompletedQuizzesAll, allLessonsData } = useAppContext();
5456

5557
const nextQuestion = () => {
5658
setCurrentQuestionIndex(currentQuestionIndex + 1);
@@ -123,18 +125,17 @@ const Quiz = (props: QuizProps): JSX.Element => {
123125
});
124126
};
125127

126-
// - Get All lessons to get the Id's
127-
const { data: allLessons } = api.lessons.getAll.useQuery();
128-
129128
// - Add
130129
const { mutate: quizzesAddMutate, isLoading: quizzesAddIsLoading } =
131130
api.completedQuizzes.add.useMutation({
132-
onSuccess: () => {
131+
onSuccess: async () => {
132+
refetchCompletedQuizzesAll && (await refetchCompletedQuizzesAll());
133133
return quizSuccessToast();
134134
},
135135
});
136136

137137
const quizSuccessToast = () => {
138+
cancelQuiz();
138139
toast({
139140
title: "Amazing!",
140141
description: "You have passed the lesson!",
@@ -172,7 +173,9 @@ const Quiz = (props: QuizProps): JSX.Element => {
172173

173174
// return quizSuccessToast();
174175

175-
const lessonIdToSave = allLessons?.find(
176+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
177+
const lessonIdToSave: string = allLessonsData?.find(
178+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
176179
(lesson) => lesson.quizFileName === `${props.quiz}.json`,
177180
)?.id;
178181

src/components/mdx/QuizStatusChecker.tsx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,34 @@
55
import { Badge, Center, Text } from "@chakra-ui/react";
66
import Quiz from "./Quiz";
77
import { useMemo, useState } from "react";
8-
import { useSession } from "next-auth/react";
9-
import { api } from "@/utils/api";
108
import { useAccount } from "wagmi";
119
import { ConnectButton } from "@rainbow-me/rainbowkit";
10+
import { useAppContext } from "@/contexts/AppContext";
1211

1312
type QuizStatusCheckerTye = {
1413
quiz: string;
1514
};
1615

1716
const QuizStatusChecker = ({ quiz }: QuizStatusCheckerTye) => {
18-
const [fetchNow, setFetchNow] = useState<boolean>(true);
1917
const [quizCompleted, setQuizCompleted] = useState<boolean>(false);
20-
const { data: sessionData } = useSession();
2118
const { address, isDisconnected } = useAccount();
19+
const { completedQuizzesIds, allLessonsData } = useAppContext();
2220

2321
// Requests
2422

25-
// - Get All lessons to get the Id's
26-
const { data: allLessons } = api.lessons.getAll.useQuery();
27-
28-
// - All completed
29-
const { data: completedQuizzesAllData } = api.completedQuizzes.all.useQuery(
30-
undefined, // no input
31-
{
32-
// Disable request if no session data
33-
enabled: sessionData?.user !== undefined && fetchNow,
34-
},
35-
);
36-
3723
useMemo(() => {
38-
if (allLessons?.length && completedQuizzesAllData?.length && fetchNow) {
39-
const completedIds = completedQuizzesAllData.map((quiz) => quiz.lesson);
40-
41-
const actualLessonId = allLessons?.find(
24+
if (allLessonsData?.length && completedQuizzesIds?.length) {
25+
const actualLessonId: string = allLessonsData?.find(
4226
(lesson) => lesson.quizFileName === `${quiz}.json`,
4327
)?.id;
4428

4529
if (actualLessonId === undefined) return;
4630

47-
if (completedIds.includes(actualLessonId)) {
31+
if (completedQuizzesIds.includes(actualLessonId)) {
4832
setQuizCompleted(true);
4933
}
50-
51-
setFetchNow(false);
5234
}
53-
}, [allLessons, completedQuizzesAllData, fetchNow, quiz]);
35+
}, [allLessonsData, completedQuizzesIds, quiz]);
5436

5537
return isDisconnected || !address ? (
5638
<>

src/contexts/AppContext.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ interface IAppContext {
55
completedQuizzesIds: string[];
66
projects: Project[];
77
fundamentals: Fundamental[];
8+
allLessonsData: any[];
9+
refetchCompletedQuizzesAll?: () => Promise<any>;
810
}
911

1012
export const AppContext = createContext<IAppContext>({
1113
completedQuizzesIds: [],
1214
projects: [],
1315
fundamentals: [],
16+
allLessonsData: [],
17+
refetchCompletedQuizzesAll: () => Promise.resolve(),
1418
});
1519

1620
AppContext.displayName = "AcademyAppContext";
1721

18-
export const useAppContext = () => useContext(AppContext);
22+
// export const useAppContext = () => useContext(AppContext);
23+
24+
export function useAppContext() {
25+
const context = useContext(AppContext);
26+
if (!context) {
27+
throw new Error("useAppContext must be used within the AppContextProvider");
28+
}
29+
return context;
30+
}

src/contexts/AppContextProvider.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ export function AppContextProvider({ children }: IProps) {
2424
const [completedQuizzesIds, setCompletedQuizzesIds] = useState<string[]>([]);
2525
const [sessionDataUser, setSessionDataUser] = useState<any>(null);
2626

27-
const { data: sessionData } = useSession();
28-
const { address } = useAccount();
27+
const { data: sessionData, status: sessionStatus } = useSession();
28+
const { address, status: walletStatus } = useAccount();
2929

3030
useEffect(() => {
3131
if (sessionData?.user && sessionData.user !== sessionDataUser) {
@@ -39,16 +39,27 @@ export function AppContextProvider({ children }: IProps) {
3939
const {
4040
data: completedQuizzesAllData,
4141
// isLoading: completedQuizzesAllIsLoading,
42-
// refetch: refetchCompletedQuizzesAll,
42+
refetch: refetchCompletedQuizzesAll,
4343
} = api.completedQuizzes.all.useQuery(
4444
undefined, // no input
4545
{
4646
// Disable request if no session data
47-
enabled: !!sessionDataUser && !!address,
47+
enabled: sessionDataUser !== null && address !== undefined,
4848
refetchOnWindowFocus: false,
4949
},
5050
);
5151

52+
useEffect(() => {
53+
if (
54+
walletStatus === "disconnected" ||
55+
sessionStatus === "unauthenticated"
56+
) {
57+
setSessionDataUser(null);
58+
setCompletedQuizzesIds([]);
59+
}
60+
// eslint-disable-next-line react-hooks/exhaustive-deps
61+
}, [sessionStatus, walletStatus]);
62+
5263
useEffect(() => {
5364
if (completedQuizzesAllData) {
5465
const completedIds = completedQuizzesAllData.map(
@@ -82,7 +93,9 @@ export function AppContextProvider({ children }: IProps) {
8293
}, []);
8394

8495
// - Get All lessons to get the Id's
85-
const { data: allLessonsData } = api.lessons.getAll.useQuery();
96+
const { data: allLessonsData } = api.lessons.getAll.useQuery(undefined, {
97+
refetchOnWindowFocus: false,
98+
});
8699

87100
useEffect(() => {
88101
if (allLessonsData && projects && completedQuizzesIds.length !== 0) {
@@ -98,6 +111,11 @@ export function AppContextProvider({ children }: IProps) {
98111
return { ...lesson, completed };
99112
});
100113

114+
setProjects(projectsWithCompleteStatus);
115+
} else if (allLessonsData && projects && completedQuizzesIds.length === 0) {
116+
const projectsWithCompleteStatus = projects.map((lesson) => {
117+
return { ...lesson, completed: false };
118+
});
101119
setProjects(projectsWithCompleteStatus);
102120
}
103121
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -109,6 +127,8 @@ export function AppContextProvider({ children }: IProps) {
109127
completedQuizzesIds,
110128
projects,
111129
fundamentals,
130+
allLessonsData: allLessonsData || [],
131+
refetchCompletedQuizzesAll,
112132
}}
113133
>
114134
{children}

0 commit comments

Comments
 (0)