fix(frontend): foutieve queries in QuestionBox.vue
This commit is contained in:
parent
23d29f4a3c
commit
1d9141bab7
1 changed files with 10 additions and 56 deletions
|
@ -1,16 +1,11 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import authService from "@/services/auth/auth-service.ts";
|
import authService from "@/services/auth/auth-service.ts";
|
||||||
import { Language } from "@/data-objects/language.ts";
|
import { Language } from "@/data-objects/language.ts";
|
||||||
import { computed, type ComputedRef, onUpdated, ref } from "vue";
|
import { computed, type ComputedRef, ref } from "vue";
|
||||||
import type { AssignmentDTO } from "@dwengo-1/common/interfaces/assignment";
|
import type { GroupDTOId } from "@dwengo-1/common/interfaces/group";
|
||||||
import { useStudentAssignmentsQuery, useStudentGroupsQuery } from "@/queries/students.ts";
|
|
||||||
import type { GroupDTO, GroupDTOId } from "@dwengo-1/common/interfaces/group";
|
|
||||||
import type { QuestionData } from "@dwengo-1/common/interfaces/question";
|
import type { QuestionData } from "@dwengo-1/common/interfaces/question";
|
||||||
import type { LearningObjectIdentifierDTO } from "@dwengo-1/interfaces/learning-content";
|
import type { LearningObjectIdentifierDTO } from "@dwengo-1/interfaces/learning-content";
|
||||||
import { useCreateQuestionMutation, useQuestionsQuery } from "@/queries/questions.ts";
|
import { useCreateQuestionMutation } from "@/queries/questions.ts";
|
||||||
import { LearningPathNode } from "@/data-objects/learning-paths/learning-path-node.ts";
|
|
||||||
import { useGetLearningPathQuery } from "@/queries/learning-paths.ts";
|
|
||||||
import { useLearningObjectListForPathQuery } from "@/queries/learning-objects.ts";
|
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
import { AccountType } from "@dwengo-1/common/util/account-types.ts";
|
import { AccountType } from "@dwengo-1/common/util/account-types.ts";
|
||||||
|
|
||||||
|
@ -26,39 +21,6 @@
|
||||||
|
|
||||||
const emit = defineEmits(["updated"]);
|
const emit = defineEmits(["updated"]);
|
||||||
|
|
||||||
const studentAssignmentsQueryResult = useStudentAssignmentsQuery(
|
|
||||||
authService.authState.user?.profile.preferred_username,
|
|
||||||
);
|
|
||||||
const learningPathQueryResult = useGetLearningPathQuery(props.hruid, props.language, props.forGroup);
|
|
||||||
const learningObjectListQueryResult = useLearningObjectListForPathQuery(learningPathQueryResult.data);
|
|
||||||
|
|
||||||
const pathIsAssignment = computed(() => {
|
|
||||||
const assignments = (studentAssignmentsQueryResult.data.value?.assignments as AssignmentDTO[]) || [];
|
|
||||||
return assignments.some(
|
|
||||||
(assignment) => assignment.learningPath === props.hruid && assignment.language === props.language,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
const nodesList: ComputedRef<LearningPathNode[] | null> = computed(
|
|
||||||
() => learningPathQueryResult.data.value?.nodesAsList ?? null,
|
|
||||||
);
|
|
||||||
|
|
||||||
const currentNode = computed(() => {
|
|
||||||
const currentHruid = props.learningObjectHruid;
|
|
||||||
return nodesList.value?.find((it) => it.learningobjectHruid === currentHruid);
|
|
||||||
});
|
|
||||||
|
|
||||||
const getQuestionsQuery = useQuestionsQuery(
|
|
||||||
computed(
|
|
||||||
() =>
|
|
||||||
({
|
|
||||||
language: currentNode.value?.language,
|
|
||||||
hruid: currentNode.value?.learningobjectHruid,
|
|
||||||
version: currentNode.value?.version,
|
|
||||||
}) as LearningObjectIdentifierDTO,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
const questionInput = ref("");
|
const questionInput = ref("");
|
||||||
|
|
||||||
const loID: ComputedRef<LearningObjectIdentifierDTO> = computed(() => ({
|
const loID: ComputedRef<LearningObjectIdentifierDTO> = computed(() => ({
|
||||||
|
@ -66,29 +28,21 @@
|
||||||
language: props.language,
|
language: props.language,
|
||||||
}));
|
}));
|
||||||
const createQuestionMutation = useCreateQuestionMutation(loID);
|
const createQuestionMutation = useCreateQuestionMutation(loID);
|
||||||
const groupsQueryResult = useStudentGroupsQuery(authService.authState.user?.profile.preferred_username);
|
|
||||||
|
|
||||||
const showQuestionBox = computed(
|
const showQuestionBox = computed(
|
||||||
() => authService.authState.activeRole === AccountType.Student && pathIsAssignment.value,
|
() => authService.authState.activeRole === AccountType.Student && props.forGroup,
|
||||||
);
|
);
|
||||||
|
|
||||||
function submitQuestion(): void {
|
function submitQuestion(): void {
|
||||||
const assignments = studentAssignmentsQueryResult.data.value?.assignments as AssignmentDTO[];
|
if (props.forGroup && questionInput.value !== "") {
|
||||||
const assignment = assignments.find(
|
const questionData: QuestionData = {
|
||||||
(assignment) => assignment.learningPath === props.hruid && assignment.language === props.language,
|
author: authService.authState.user?.profile.preferred_username,
|
||||||
);
|
content: questionInput.value,
|
||||||
const groups = groupsQueryResult.data.value?.groups as GroupDTO[];
|
inGroup: props.forGroup,
|
||||||
const group = groups?.find((group) => group.assignment === assignment?.id) as GroupDTO;
|
};
|
||||||
const questionData: QuestionData = {
|
|
||||||
author: authService.authState.user?.profile.preferred_username,
|
|
||||||
content: questionInput.value,
|
|
||||||
inGroup: group,
|
|
||||||
};
|
|
||||||
if (questionInput.value !== "") {
|
|
||||||
createQuestionMutation.mutate(questionData, {
|
createQuestionMutation.mutate(questionData, {
|
||||||
onSuccess: async () => {
|
onSuccess: async () => {
|
||||||
questionInput.value = ""; // Clear the input field after submission
|
questionInput.value = ""; // Clear the input field after submission
|
||||||
await getQuestionsQuery.refetch(); // Reload the questions
|
|
||||||
emit("updated");
|
emit("updated");
|
||||||
},
|
},
|
||||||
onError: (_) => {
|
onError: (_) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue