update: change function to implement queries toggle flag answers
This commit is contained in:
parent
9445690238
commit
ab29d0c1a1
|
|
@ -64,6 +64,14 @@ interface ToggleFlagResponse {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SubmitOptionResponse {
|
||||||
|
message: string;
|
||||||
|
answer: {
|
||||||
|
id: string;
|
||||||
|
isFlagged: boolean | null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function AssessmentPage() {
|
export default function AssessmentPage() {
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const limit = 10;
|
const limit = 10;
|
||||||
|
|
@ -275,32 +283,44 @@ export default function AssessmentPage() {
|
||||||
|
|
||||||
// Mutation function to toggle flag
|
// Mutation function to toggle flag
|
||||||
const { mutate: toggleFlag } = useMutation({
|
const { mutate: toggleFlag } = useMutation({
|
||||||
mutationFn: (questionId: string) => toggleFlagAnswer(questionId),
|
mutationFn: (formData: { assessmentId: string; questionId: string; isFlagged: boolean }) =>
|
||||||
onSuccess: (response) => {
|
toggleFlagAnswer(formData),
|
||||||
if (response && response.answer) {
|
onSuccess: (response: SubmitOptionResponse) => {
|
||||||
const { answer } = response;
|
if (response.answer) {
|
||||||
setFlaggedQuestions((prevFlags) => ({
|
const { answer } = response;
|
||||||
...prevFlags,
|
setFlaggedQuestions((prevFlags) => ({
|
||||||
[answer.id]: answer.isFlagged !== null ? answer.isFlagged : false,
|
...prevFlags,
|
||||||
}));
|
[answer.id]: answer.isFlagged ?? false,
|
||||||
}
|
}));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error("Error toggling flag:", error);
|
console.error("Error toggling flag:", error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fungsi untuk toggle flag
|
// Fungsi untuk toggle flag
|
||||||
const handleToggleFlag = (questionId: string) => {
|
const handleToggleFlag = (questionId: string) => {
|
||||||
const newFlagState = !flaggedQuestions[questionId];
|
const newFlagState = !flaggedQuestions[questionId];
|
||||||
|
const assessmentId = getQueryParam("id");
|
||||||
|
|
||||||
// Update flaggedQuestions dan kirim ke server
|
if (!assessmentId) {
|
||||||
|
console.error("Assessment ID tidak ditemukan");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update flaggedQuestions di state
|
||||||
setFlaggedQuestions((prevFlags) => ({
|
setFlaggedQuestions((prevFlags) => ({
|
||||||
...prevFlags,
|
...prevFlags,
|
||||||
[questionId]: newFlagState,
|
[questionId]: newFlagState,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
toggleFlag(questionId);
|
// Kirim perubahan flag ke server
|
||||||
|
toggleFlag({
|
||||||
|
assessmentId,
|
||||||
|
questionId,
|
||||||
|
isFlagged: newFlagState,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Usage of the mutation in your component
|
// Usage of the mutation in your component
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user