|
| 1 | +import { __ } from '@wordpress/i18n'; |
| 2 | + |
| 3 | +import { type MutationState } from '@Core/ts/services/Query'; |
| 4 | +import { wpAjaxInstance } from '@TutorShared/utils/api'; |
| 5 | +import endpoints from '@TutorShared/utils/endpoints'; |
| 6 | +import { convertToErrorMessage } from '@TutorShared/utils/util'; |
| 7 | + |
| 8 | +const REVIEW_STATUSES = ['correct', 'incorrect'] as const; |
| 9 | +const REVIEW_STATUS_FIELD = 'review_statuses' as const; |
| 10 | + |
| 11 | +type ReviewStatus = (typeof REVIEW_STATUSES)[number]; |
| 12 | +type ReviewStatusFieldName = `${typeof REVIEW_STATUS_FIELD}[${string}]`; |
| 13 | +type ReviewStatusMap = Record<string, ReviewStatus>; |
| 14 | +type ReviewStatusesAjaxPayload = Partial<Record<ReviewStatusFieldName, ReviewStatus>>; |
| 15 | + |
| 16 | +interface QuizAttemptFeedbackProps { |
| 17 | + attemptId: number; |
| 18 | + formId: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface QuizAttemptFeedbackPayload { |
| 22 | + attempt_id: number; |
| 23 | + feedback: string; |
| 24 | + review_statuses: ReviewStatusMap; |
| 25 | +} |
| 26 | + |
| 27 | +interface QuizAttemptFeedbackResponse<TData = unknown> { |
| 28 | + success?: boolean; |
| 29 | + message?: string; |
| 30 | + data?: TData; |
| 31 | +} |
| 32 | + |
| 33 | +interface QuizAttemptSubmitResponse { |
| 34 | + reviewResponse: QuizAttemptFeedbackResponse | null; |
| 35 | + feedbackResponse: QuizAttemptFeedbackResponse; |
| 36 | +} |
| 37 | + |
| 38 | +const quizAttemptFeedback = ({ attemptId, formId }: QuizAttemptFeedbackProps) => { |
| 39 | + const query = window.TutorCore.query; |
| 40 | + const toast = window.TutorCore.toast; |
| 41 | + const reviewStatusFieldPattern = new RegExp(`^${REVIEW_STATUS_FIELD}\\[[^\\]]+\\]$`); |
| 42 | + |
| 43 | + const getReviewStatuses = (data: Record<string, unknown>) => { |
| 44 | + return Object.entries(data).reduce<ReviewStatusMap>((acc, [key, value]) => { |
| 45 | + if (!reviewStatusFieldPattern.test(key)) { |
| 46 | + return acc; |
| 47 | + } |
| 48 | + |
| 49 | + if (typeof value !== 'string' || !REVIEW_STATUSES.includes(value as ReviewStatus)) { |
| 50 | + return acc; |
| 51 | + } |
| 52 | + |
| 53 | + const fieldName = key as ReviewStatusFieldName; |
| 54 | + const questionId = fieldName.slice(`${REVIEW_STATUS_FIELD}[`.length, -1); |
| 55 | + const reviewStatus = value as ReviewStatus; |
| 56 | + |
| 57 | + acc[questionId] = reviewStatus; |
| 58 | + return acc; |
| 59 | + }, {}); |
| 60 | + }; |
| 61 | + |
| 62 | + const getReviewStatusesPayload = (reviewStatuses: ReviewStatusMap) => { |
| 63 | + return Object.entries(reviewStatuses).reduce<ReviewStatusesAjaxPayload>((acc, [questionId, status]) => { |
| 64 | + const fieldName: ReviewStatusFieldName = `${REVIEW_STATUS_FIELD}[${questionId}]`; |
| 65 | + acc[fieldName] = status; |
| 66 | + return acc; |
| 67 | + }, {}); |
| 68 | + }; |
| 69 | + |
| 70 | + return { |
| 71 | + formId, |
| 72 | + attemptId, |
| 73 | + feedbackMutation: null as MutationState<QuizAttemptSubmitResponse, QuizAttemptFeedbackPayload> | null, |
| 74 | + |
| 75 | + init() { |
| 76 | + this.feedbackMutation = query.useMutation(this.saveFeedback, { |
| 77 | + onSuccess: () => { |
| 78 | + toast.success(__('Quiz feedback updated successfully.', 'tutor')); |
| 79 | + window.location.reload(); |
| 80 | + }, |
| 81 | + onError: (error: Error) => { |
| 82 | + toast.error(convertToErrorMessage(error)); |
| 83 | + }, |
| 84 | + }); |
| 85 | + }, |
| 86 | + |
| 87 | + async saveFeedback(payload: QuizAttemptFeedbackPayload) { |
| 88 | + const reviewStatusesPayload = getReviewStatusesPayload(payload.review_statuses); |
| 89 | + const reviewRequest = |
| 90 | + Object.keys(reviewStatusesPayload).length > 0 |
| 91 | + ? wpAjaxInstance |
| 92 | + .post<QuizAttemptFeedbackResponse>(endpoints.REVIEW_QUIZ_ANSWERS, { |
| 93 | + attempt_id: payload.attempt_id, |
| 94 | + ...reviewStatusesPayload, |
| 95 | + }) |
| 96 | + .then((res) => res.data) |
| 97 | + : Promise.resolve(null); |
| 98 | + |
| 99 | + const feedbackRequest = wpAjaxInstance |
| 100 | + .post<QuizAttemptFeedbackResponse>(endpoints.INSTRUCTOR_FEEDBACK, { |
| 101 | + attempt_id: payload.attempt_id, |
| 102 | + feedback: payload.feedback, |
| 103 | + }) |
| 104 | + .then((res) => res.data); |
| 105 | + |
| 106 | + const [reviewResponse, feedbackResponse] = await Promise.all([reviewRequest, feedbackRequest]); |
| 107 | + |
| 108 | + return { |
| 109 | + reviewResponse, |
| 110 | + feedbackResponse, |
| 111 | + }; |
| 112 | + }, |
| 113 | + |
| 114 | + async handleSaveFeedback(data: Record<string, unknown>) { |
| 115 | + await this.feedbackMutation?.mutate({ |
| 116 | + attempt_id: this.attemptId, |
| 117 | + feedback: String(data.feedback ?? ''), |
| 118 | + review_statuses: getReviewStatuses(data), |
| 119 | + }); |
| 120 | + }, |
| 121 | + }; |
| 122 | +}; |
| 123 | + |
| 124 | +export const quizAttemptFeedbackMeta = { |
| 125 | + name: 'quizAttemptFeedback', |
| 126 | + component: quizAttemptFeedback, |
| 127 | +}; |
0 commit comments