update: changes to the average score data update in real time when the user selects an answer
This commit is contained in:
parent
4cff4fd759
commit
a0707d67a9
|
|
@ -94,13 +94,13 @@ export const submitOption = async (form: {
|
|||
);
|
||||
};
|
||||
|
||||
export const submitOptionMutationOptions: () => UseMutationOptions<
|
||||
export const submitOptionMutationOptions: UseMutationOptions<
|
||||
SubmitOptionResponse,
|
||||
Error,
|
||||
Parameters<typeof submitOption>[0]
|
||||
> = () => ({
|
||||
> = {
|
||||
mutationFn: submitOption,
|
||||
});
|
||||
};
|
||||
|
||||
export const submitValidation = async (
|
||||
form: {
|
||||
|
|
|
|||
|
|
@ -180,8 +180,17 @@ export default function AssessmentPage() {
|
|||
},
|
||||
});
|
||||
|
||||
// Mutation function to submit answer
|
||||
const submitOptionMutation = useMutation(submitOptionMutationOptions());
|
||||
// Usage of the mutation in your component
|
||||
const submitOptionMutation = useMutation({
|
||||
...submitOptionMutationOptions, // Spread the mutation options here
|
||||
onSuccess: () => {
|
||||
// Refetch the average scores after a successful submission
|
||||
averageScoreQuery.refetch();
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Error submitting option:", error);
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const assessmentId = getQueryParam("id");
|
||||
|
|
@ -200,19 +209,20 @@ export default function AssessmentPage() {
|
|||
|
||||
const handleAnswerChange = (questionId: string, optionId: string) => {
|
||||
const assessmentId = getQueryParam("id");
|
||||
|
||||
|
||||
if (!assessmentId) {
|
||||
console.error("Assessment ID tidak ditemukan");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Simpan jawaban ke localStorage dengan ID assessment
|
||||
const updatedAnswers = { ...answers, [questionId]: optionId };
|
||||
localStorage.setItem(`assessmentAnswers_${assessmentId}`, JSON.stringify(updatedAnswers));
|
||||
|
||||
|
||||
// Update state
|
||||
setAnswers(updatedAnswers);
|
||||
|
||||
|
||||
// Call the mutation to submit the option
|
||||
submitOptionMutation.mutate({
|
||||
optionId,
|
||||
assessmentId,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user