Add tests and edit works
This commit is contained in:
parent
bfbba7b5ab
commit
bc4b0e2df7
12 changed files with 2415 additions and 20 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue