|
| 1 | +import { type MutationState } from '@Core/ts/services/Query'; |
| 2 | +import { wpAjaxInstance } from '@TutorShared/utils/api'; |
| 3 | +import endpoints from '@TutorShared/utils/endpoints'; |
| 4 | +import { convertToErrorMessage } from '@TutorShared/utils/util'; |
| 5 | +import { __ } from '@wordpress/i18n'; |
| 6 | + |
| 7 | +interface CreateQnaPayload { |
| 8 | + course_id: number; |
| 9 | + question: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface ReplyQnaPayload { |
| 13 | + course_id: number; |
| 14 | + question_id: number; |
| 15 | + answer: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface DeleteQnaPayload { |
| 19 | + question_id: number; |
| 20 | + context: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Q&A Page Component |
| 25 | + * Handles Q&A related action in learning area |
| 26 | + */ |
| 27 | +const qnaPage = () => { |
| 28 | + const query = window.TutorCore.query; |
| 29 | + |
| 30 | + return { |
| 31 | + query, |
| 32 | + createQnaMutation: null as MutationState<unknown, CreateQnaPayload> | null, |
| 33 | + replyQnaMutation: null as MutationState<unknown, ReplyQnaPayload> | null, |
| 34 | + deleteQnaMutation: null as MutationState<unknown, unknown> | null, |
| 35 | + focused: false, |
| 36 | + |
| 37 | + init() { |
| 38 | + this.createQnaMutation = this.query.useMutation(this.createQnA, { |
| 39 | + onSuccess: () => { |
| 40 | + window.TutorCore.toast.success(__('Question saved successfully', 'tutor')); |
| 41 | + window.location.reload(); |
| 42 | + }, |
| 43 | + onError: (error: Error) => { |
| 44 | + window.TutorCore.toast.error(convertToErrorMessage(error)); |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + this.replyQnaMutation = this.query.useMutation(this.replyQna, { |
| 49 | + onSuccess: () => { |
| 50 | + window.TutorCore.toast.success(__('Reply saved successfully', 'tutor')); |
| 51 | + window.location.reload(); |
| 52 | + }, |
| 53 | + onError: (error: Error) => { |
| 54 | + window.TutorCore.toast.error(convertToErrorMessage(error)); |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + this.deleteQnaMutation = this.query.useMutation(this.deleteQnA, { |
| 59 | + onSuccess: (result, payload) => { |
| 60 | + if (payload?.context === 'question') { |
| 61 | + window.TutorCore.toast.success(__('Question deleted successfully', 'tutor')); |
| 62 | + const url = new URL(window.location.href); |
| 63 | + url.searchParams.delete('question_id'); |
| 64 | + window.location.href = url.toString(); |
| 65 | + } else if (payload?.context === 'reply') { |
| 66 | + window.TutorCore.toast.success(__('Reply deleted successfully', 'tutor')); |
| 67 | + window.location.reload(); |
| 68 | + } else { |
| 69 | + window.location.reload(); |
| 70 | + } |
| 71 | + }, |
| 72 | + onError: (error: Error) => { |
| 73 | + window.TutorCore.toast.error(convertToErrorMessage(error)); |
| 74 | + }, |
| 75 | + }); |
| 76 | + }, |
| 77 | + |
| 78 | + createQnA(payload: CreateQnaPayload) { |
| 79 | + return wpAjaxInstance.post(endpoints.CREATE_UPDATE_QNA, payload); |
| 80 | + }, |
| 81 | + |
| 82 | + replyQna(payload: ReplyQnaPayload) { |
| 83 | + return wpAjaxInstance.post(endpoints.CREATE_UPDATE_QNA, payload); |
| 84 | + }, |
| 85 | + |
| 86 | + deleteQnA(payload: DeleteQnaPayload) { |
| 87 | + return wpAjaxInstance.post(endpoints.DELETE_DASHBOARD_QNA, payload); |
| 88 | + }, |
| 89 | + }; |
| 90 | +}; |
| 91 | + |
| 92 | +export const initializeQna = () => { |
| 93 | + window.TutorComponentRegistry.register({ |
| 94 | + type: 'component', |
| 95 | + meta: { |
| 96 | + name: 'qna', |
| 97 | + component: qnaPage, |
| 98 | + }, |
| 99 | + }); |
| 100 | + window.TutorComponentRegistry.initWithAlpine(window.Alpine); |
| 101 | +}; |
0 commit comments