Add tests and edit works

This commit is contained in:
Joey Yakimowich-Payne 2026-01-13 23:52:04 -07:00
commit bc4b0e2df7
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
12 changed files with 2415 additions and 20 deletions

View file

@ -13,6 +13,7 @@ interface UseQuizLibraryReturn {
fetchQuizzes: () => Promise<void>;
loadQuiz: (id: string) => Promise<SavedQuiz>;
saveQuiz: (quiz: Quiz, source: QuizSource, aiTopic?: string) => Promise<string>;
updateQuiz: (id: string, quiz: Quiz) => Promise<void>;
deleteQuiz: (id: string) => Promise<void>;
retry: () => Promise<void>;
clearError: () => void;
@ -160,6 +161,48 @@ export const useQuizLibrary = (): UseQuizLibraryReturn => {
}
}, [authFetch]);
const updateQuiz = useCallback(async (id: string, quiz: Quiz): Promise<void> => {
setSaving(true);
setError(null);
try {
const response = await authFetch(`/api/quizzes/${id}`, {
method: 'PUT',
body: JSON.stringify({
title: quiz.title,
questions: quiz.questions.map(q => ({
text: q.text,
timeLimit: q.timeLimit,
options: q.options.map(o => ({
text: o.text,
isCorrect: o.isCorrect,
shape: o.shape,
color: o.color,
reason: o.reason,
})),
})),
}),
});
if (!response.ok) {
const errorText = response.status === 404
? 'Quiz not found.'
: 'Failed to update quiz.';
throw new Error(errorText);
}
toast.success('Quiz updated!');
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to update quiz';
if (!message.includes('redirecting')) {
toast.error(message);
}
throw err;
} finally {
setSaving(false);
}
}, [authFetch]);
const deleteQuiz = useCallback(async (id: string): Promise<void> => {
setDeletingQuizId(id);
setError(null);
@ -209,6 +252,7 @@ export const useQuizLibrary = (): UseQuizLibraryReturn => {
fetchQuizzes,
loadQuiz,
saveQuiz,
updateQuiz,
deleteQuiz,
retry,
clearError,