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,
|
SubmitOptionResponse,
|
||||||
Error,
|
Error,
|
||||||
Parameters<typeof submitOption>[0]
|
Parameters<typeof submitOption>[0]
|
||||||
> = () => ({
|
> = {
|
||||||
mutationFn: submitOption,
|
mutationFn: submitOption,
|
||||||
});
|
};
|
||||||
|
|
||||||
export const submitValidation = async (
|
export const submitValidation = async (
|
||||||
form: {
|
form: {
|
||||||
|
|
|
||||||
|
|
@ -180,8 +180,17 @@ export default function AssessmentPage() {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mutation function to submit answer
|
// Usage of the mutation in your component
|
||||||
const submitOptionMutation = useMutation(submitOptionMutationOptions());
|
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(() => {
|
useEffect(() => {
|
||||||
const assessmentId = getQueryParam("id");
|
const assessmentId = getQueryParam("id");
|
||||||
|
|
@ -200,19 +209,20 @@ export default function AssessmentPage() {
|
||||||
|
|
||||||
const handleAnswerChange = (questionId: string, optionId: string) => {
|
const handleAnswerChange = (questionId: string, optionId: string) => {
|
||||||
const assessmentId = getQueryParam("id");
|
const assessmentId = getQueryParam("id");
|
||||||
|
|
||||||
if (!assessmentId) {
|
if (!assessmentId) {
|
||||||
console.error("Assessment ID tidak ditemukan");
|
console.error("Assessment ID tidak ditemukan");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simpan jawaban ke localStorage dengan ID assessment
|
// Simpan jawaban ke localStorage dengan ID assessment
|
||||||
const updatedAnswers = { ...answers, [questionId]: optionId };
|
const updatedAnswers = { ...answers, [questionId]: optionId };
|
||||||
localStorage.setItem(`assessmentAnswers_${assessmentId}`, JSON.stringify(updatedAnswers));
|
localStorage.setItem(`assessmentAnswers_${assessmentId}`, JSON.stringify(updatedAnswers));
|
||||||
|
|
||||||
// Update state
|
// Update state
|
||||||
setAnswers(updatedAnswers);
|
setAnswers(updatedAnswers);
|
||||||
|
|
||||||
|
// Call the mutation to submit the option
|
||||||
submitOptionMutation.mutate({
|
submitOptionMutation.mutate({
|
||||||
optionId,
|
optionId,
|
||||||
assessmentId,
|
assessmentId,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user