Refactor frontend

This commit is contained in:
Christian Cueni 2024-04-24 21:17:33 +02:00
parent 5d7898d415
commit 552746182f
5 changed files with 83 additions and 99 deletions

View File

@ -96,7 +96,9 @@ function hasActionButton(): boolean {
</a> </a>
</div> </div>
<p> <p>
<span class="rounded bg-gray-300 px-2 py-1">{{ courseConfig.role_key }}</span> <span class="rounded bg-gray-300 px-2 py-1">
{{ $t(courseConfig.role_key) }}
</span>
<router-link <router-link
v-if="courseConfig.has_preview" v-if="courseConfig.has_preview"
:to="getLearningPathUrl(courseConfig.course_slug)" :to="getLearningPathUrl(courseConfig.course_slug)"

View File

@ -1,7 +1,12 @@
import { useCSRFFetch } from "@/fetchHelpers"; import { useCSRFFetch } from "@/fetchHelpers";
import type { CourseStatisticsType } from "@/gql/graphql"; import type { CourseStatisticsType } from "@/gql/graphql";
import { graphqlClient } from "@/graphql/client"; import { graphqlClient } from "@/graphql/client";
import { COURSE_QUERY, COURSE_SESSION_DETAIL_QUERY } from "@/graphql/queries"; import {
COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY,
COMPETENCE_NAVI_CERTIFICATE_QUERY,
COURSE_QUERY,
COURSE_SESSION_DETAIL_QUERY,
} from "@/graphql/queries";
import { import {
circleFlatChildren, circleFlatChildren,
circleFlatLearningContents, circleFlatLearningContents,
@ -656,3 +661,29 @@ export function useCourseStatisticsv2(courseSlug: string) {
circleMeta, circleMeta,
}; };
} }
export function useCertificateQuery(userId: string | undefined, courseSlug: string) {
const certificatesQuery = (() => {
const courseSession = useCurrentCourseSession();
if (userId) {
return useQuery({
query: COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY,
variables: {
courseSlug: courseSlug,
courseSessionId: courseSession.value.id,
userId: userId,
},
});
} else {
return useQuery({
query: COMPETENCE_NAVI_CERTIFICATE_QUERY,
variables: {
courseSlug: courseSlug,
courseSessionId: courseSession.value.id,
},
});
}
})();
return { certificatesQuery };
}

View File

@ -1,18 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import log from "loglevel"; import log from "loglevel";
import {
COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY,
COMPETENCE_NAVI_CERTIFICATE_QUERY,
} from "@/graphql/queries";
import { useQuery } from "@urql/vue";
import { computed, onMounted } from "vue"; import { computed, onMounted } from "vue";
import type { CompetenceCertificate } from "@/types"; import type { CompetenceCertificate } from "@/types";
import { useCurrentCourseSession } from "@/composables"; import { useCertificateQuery } from "@/composables";
import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue"; import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue";
import { import { getCertificates } from "@/services/competence";
isCompetenceCertificateForUserQueryQuery,
isCompetenceCertificateQueryQuery,
} from "@/services/competence";
const props = defineProps<{ const props = defineProps<{
courseSlug: string; courseSlug: string;
@ -22,53 +14,23 @@ const props = defineProps<{
log.debug("CompetenceCertificateDetailPage setup", props); log.debug("CompetenceCertificateDetailPage setup", props);
const courseSession = useCurrentCourseSession(); const certificatesQuery = useCertificateQuery(
props.userId,
const certificatesQuery = (() => { props.courseSlug
if (props.userId) { ).certificatesQuery;
return useQuery({
query: COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY,
variables: {
courseSlug: props.courseSlug,
courseSessionId: courseSession.value.id,
userId: props.userId,
},
});
} else {
return useQuery({
query: COMPETENCE_NAVI_CERTIFICATE_QUERY,
variables: {
courseSlug: props.courseSlug,
courseSessionId: courseSession.value.id,
},
});
}
})();
const certificate = computed(() => { const certificate = computed(() => {
const data = certificatesQuery.data.value; const certificates = getCertificates(
if (!data) { certificatesQuery.data.value,
return null; props.userId ?? null
} );
let certificate; if (!certificates) {
if (props.userId && isCompetenceCertificateForUserQueryQuery(data)) {
certificate = data.competence_certificate_list_for_user;
} else if (isCompetenceCertificateQueryQuery(data)) {
certificate = data.competence_certificate_list;
} else {
// Handle case where data does not match expected types
console.error("Data structure is not recognized!");
return null;
}
if (!certificate) {
return null; return null;
} }
return ( return (
(certificate.competence_certificates as unknown as CompetenceCertificate[]) ?? [] (certificates.competence_certificates as unknown as CompetenceCertificate[]) ?? []
).find((cc) => cc.slug.endsWith(props.certificateSlug)); ).find((cc) => cc.slug.endsWith(props.certificateSlug));
}); });

View File

@ -1,23 +1,15 @@
<script setup lang="ts"> <script setup lang="ts">
import log from "loglevel"; import log from "loglevel";
import {
COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY,
COMPETENCE_NAVI_CERTIFICATE_QUERY,
} from "@/graphql/queries";
import { useQuery } from "@urql/vue";
import { computed, onMounted } from "vue"; import { computed, onMounted } from "vue";
import type { CompetenceCertificate } from "@/types"; import type { CompetenceCertificate } from "@/types";
import { useCurrentCourseSession } from "@/composables"; import { useCertificateQuery } from "@/composables";
import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue"; import CompetenceCertificateComponent from "@/pages/competence/CompetenceCertificateComponent.vue";
import { import {
assignmentsMaxEvaluationPoints, assignmentsMaxEvaluationPoints,
assignmentsUserPoints, assignmentsUserPoints,
} from "@/pages/competence/utils"; } from "@/pages/competence/utils";
import { useRoute } from "vue-router"; import { useRoute } from "vue-router";
import { import { getCertificates } from "@/services/competence";
isCompetenceCertificateForUserQueryQuery,
isCompetenceCertificateQueryQuery,
} from "@/services/competence";
const props = defineProps<{ const props = defineProps<{
courseSlug: string; courseSlug: string;
@ -26,50 +18,25 @@ const props = defineProps<{
log.debug("CompetenceCertificateListPage setup", props); log.debug("CompetenceCertificateListPage setup", props);
const courseSession = useCurrentCourseSession();
const route = useRoute(); const route = useRoute();
const certificatesQuery = (() => { const certificatesQuery = useCertificateQuery(
if (props.userId) { props.userId,
return useQuery({ props.courseSlug
query: COMPETENCE_NAVI_CERTIFICATE_FOR_USER_QUERY, ).certificatesQuery;
variables: {
courseSlug: props.courseSlug,
courseSessionId: courseSession.value.id,
userId: props.userId,
},
});
} else {
return useQuery({
query: COMPETENCE_NAVI_CERTIFICATE_QUERY,
variables: {
courseSlug: props.courseSlug,
courseSessionId: courseSession.value.id,
},
});
}
})();
const competenceCertificates = computed(() => { const competenceCertificates = computed(() => {
const data = certificatesQuery.data.value; const certificates = getCertificates(
if (!data) { certificatesQuery.data.value,
return []; props.userId ?? null
} );
let certificate; if (!certificates) {
return null;
if (props.userId && isCompetenceCertificateForUserQueryQuery(data)) {
certificate = data.competence_certificate_list_for_user;
} else if (isCompetenceCertificateQueryQuery(data)) {
certificate = data.competence_certificate_list;
} else {
// Handle case where data does not match expected types
console.error("Data structure is not recognized!");
return [];
} }
return ( return (
(certificate?.competence_certificates as unknown as CompetenceCertificate[]) ?? [] (certificates?.competence_certificates as unknown as CompetenceCertificate[]) ?? []
); );
}); });
@ -108,7 +75,7 @@ onMounted(async () => {
</script> </script>
<template> <template>
<div class="container-large"> <div v-if="assignments" class="container-large">
<h2 class="mb-4 lg:py-4">{{ $t("a.Kompetenznachweise") }}</h2> <h2 class="mb-4 lg:py-4">{{ $t("a.Kompetenznachweise") }}</h2>
<div class="mb-4 bg-white p-8" data-cy="certificate-total-points-text"> <div class="mb-4 bg-white p-8" data-cy="certificate-total-points-text">

View File

@ -1,5 +1,6 @@
import type { import type {
CompetenceCertificateForUserQueryQuery, CompetenceCertificateForUserQueryQuery,
CompetenceCertificateListObjectType,
CompetenceCertificateQueryQuery, CompetenceCertificateQueryQuery,
} from "@/gql/graphql"; } from "@/gql/graphql";
import type { PerformanceCriteria } from "@/types"; import type { PerformanceCriteria } from "@/types";
@ -39,3 +40,24 @@ export function isCompetenceCertificateQueryQuery(
(data as CompetenceCertificateQueryQuery).competence_certificate_list !== undefined (data as CompetenceCertificateQueryQuery).competence_certificate_list !== undefined
); );
} }
export function getCertificates(
data: any,
userId: string | null
): CompetenceCertificateListObjectType | null {
if (!data) {
return null;
}
let certificates = null;
if (userId && isCompetenceCertificateForUserQueryQuery(data)) {
certificates = data.competence_certificate_list_for_user;
} else if (isCompetenceCertificateQueryQuery(data)) {
certificates = data.competence_certificate_list;
} else {
// Handle case where data does not match expected types
console.error("Data structure is not recognized!");
return null;
}
return certificates ?? null;
}