Skip to content

Commit

Permalink
feat: check for exsisting downloads on download page mount (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwisecodes authored Sep 11, 2024
1 parent df73b6d commit 33d2846
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ export function useExportAdditionalMaterials({
}
}, [lesson, setParseResult, active]);

const checkForSnapShotAndPreloadQuery =
trpc.exports.checkIfAdditionalMaterialsDownloadExists.useMutation();
const [checked, setChecked] = useState(false);
const check = useCallback(async () => {
if (
debouncedParseResult?.success &&
debouncedParseResult.data &&
!checked
) {
try {
checkForSnapShotAndPreloadQuery.mutate({
chatId,
data: debouncedParseResult.data,
});
setChecked(true);
} catch (error) {
console.error("Error during check:", error);
}
}
}, [
debouncedParseResult?.success,
debouncedParseResult?.data,
chatId,
checkForSnapShotAndPreloadQuery,
checked,
]);

useEffect(() => {
check();
}, [check]);

const start = useCallback(() => {
if (!active) {
return;
Expand Down Expand Up @@ -77,9 +108,10 @@ export function useExportAdditionalMaterials({
dialogOpen,
closeDialog,
status: query.status,
data: query.data,
data: checkForSnapShotAndPreloadQuery.data || query.data,
}),
[
checkForSnapShotAndPreloadQuery.data,
active,
debouncedParseResult,
dialogOpen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ export function useExportLessonPlanDoc({
setParseResult(res);
}
}, [lesson, setParseResult, active]);

const checkForSnapShotAndPreloadQuery =
trpc.exports.checkIfLessonPlanDownloadExists.useMutation();
const [checked, setChecked] = useState(false);
const check = useCallback(async () => {
if (
debouncedParseResult?.success &&
debouncedParseResult.data &&
!checked
) {
try {
checkForSnapShotAndPreloadQuery.mutate({
chatId,
data: debouncedParseResult.data,
});
setChecked(true);
} catch (error) {
console.error("Error during check:", error);
}
}
}, [
debouncedParseResult?.success,
debouncedParseResult?.data,
chatId,
checkForSnapShotAndPreloadQuery,
checked,
]);

useEffect(() => {
check();
}, [check]);

const start = useCallback(() => {
if (!active) {
return;
Expand Down Expand Up @@ -74,9 +106,10 @@ export function useExportLessonPlanDoc({
dialogOpen,
closeDialog,
status: query.status,
data: query.data,
data: checkForSnapShotAndPreloadQuery.data || query.data,
}),
[
checkForSnapShotAndPreloadQuery.data,
active,
debouncedParseResult,
dialogOpen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ export function useExportLessonSlides({
setParseResult(res);
}
}, [lesson, setParseResult, active]);

const checkForSnapShotAndPreloadQuery =
trpc.exports.checkIfSlideDownloadExists.useMutation();
const [checked, setChecked] = useState(false);
const check = useCallback(async () => {
if (
debouncedParseResult?.success &&
debouncedParseResult.data &&
!checked
) {
try {
checkForSnapShotAndPreloadQuery.mutate({
chatId,
data: debouncedParseResult.data,
});
setChecked(true);
} catch (error) {
console.error("Error during check:", error);
}
}
}, [
debouncedParseResult?.success,
debouncedParseResult?.data,
chatId,
checkForSnapShotAndPreloadQuery,
checked,
]);

useEffect(() => {
check();
}, [check]);

const start = useCallback(() => {
if (!active) {
return;
Expand Down Expand Up @@ -76,14 +108,15 @@ export function useExportLessonSlides({
dialogOpen,
closeDialog,
status: query.status,
data: query.data,
data: checkForSnapShotAndPreloadQuery.data || query.data,
}),
[
active,
debouncedParseResult,
dialogOpen,
query.status,
query.data,
checkForSnapShotAndPreloadQuery.data,
start,
closeDialog,
],
Expand Down
36 changes: 35 additions & 1 deletion apps/nextjs/src/components/ExportsDialogs/useExportQuizDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ export function useExportQuizDoc({
}
}, [setParseResult, active, lesson, quizType, quiz]);

const checkForSnapShotAndPreloadQuery =
trpc.exports.checkIfQuizDownloadExists.useMutation();
const [checked, setChecked] = useState(false);
const check = useCallback(async () => {
if (
debouncedParseResult?.success &&
debouncedParseResult.data &&
!checked
) {
try {
checkForSnapShotAndPreloadQuery.mutate({
chatId,
lessonSnapshot: lesson,
data: debouncedParseResult.data,
});
setChecked(true);
} catch (error) {
console.error("Error during check:", error);
}
}
}, [
lesson,
debouncedParseResult?.success,
debouncedParseResult?.data,
chatId,
checkForSnapShotAndPreloadQuery,
checked,
]);

useEffect(() => {
check();
}, [check]);

const start = useCallback(() => {
if (!active) {
return;
Expand Down Expand Up @@ -80,9 +113,10 @@ export function useExportQuizDoc({
dialogOpen,
closeDialog,
status: query.status,
data: query.data,
data: checkForSnapShotAndPreloadQuery.data || query.data,
}),
[
checkForSnapShotAndPreloadQuery.data,
active,
debouncedParseResult,
dialogOpen,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ export function useExportWorksheetSlides({
}
}, [lesson, setParseResult, active]);

const checkForSnapShotAndPreloadQuery =
trpc.exports.checkIfWorksheetDownloadExists.useMutation();
const [checked, setChecked] = useState(false);
const check = useCallback(async () => {
if (
debouncedParseResult?.success &&
debouncedParseResult.data &&
!checked
) {
try {
checkForSnapShotAndPreloadQuery.mutate({
chatId,
data: debouncedParseResult.data,
});
setChecked(true);
} catch (error) {
console.error("Error during check:", error);
}
}
}, [
debouncedParseResult?.success,
debouncedParseResult?.data,
chatId,
checkForSnapShotAndPreloadQuery,
checked,
]);

useEffect(() => {
check();
}, [check]);

const start = useCallback(() => {
if (!active) {
return;
Expand Down Expand Up @@ -75,9 +106,10 @@ export function useExportWorksheetSlides({
dialogOpen,
closeDialog,
status: query.status,
data: query.data,
data: checkForSnapShotAndPreloadQuery.data || query.data,
}),
[
checkForSnapShotAndPreloadQuery.data,
active,
debouncedParseResult,
dialogOpen,
Expand Down
Loading

0 comments on commit 33d2846

Please sign in to comment.