From 4cf2a8708547369fc577ffcedb8bb0774ae06443 Mon Sep 17 00:00:00 2001 From: Joabesv Date: Thu, 12 Dec 2024 15:12:11 -0300 Subject: [PATCH] feat: student coefficientss --- src/entrypoints/popup/App.vue | 41 ++++++++++++++++++++++++++--------- src/services/next.ts | 5 +++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/entrypoints/popup/App.vue b/src/entrypoints/popup/App.vue index 046745c..d5b6c71 100644 --- a/src/entrypoints/popup/App.vue +++ b/src/entrypoints/popup/App.vue @@ -3,22 +3,43 @@ import { Loader2 } from 'lucide-vue-next' import { useStorage } from '@/composables/useStorage' import { useDateFormat } from '@vueuse/core' import type { Student } from '@/scripts/sig/homepage' -import { getStudent, type MatriculaStudent } from '@/services/next'; +import { type MatriculaStudent, nextURL } from '@/services/next'; const { state: student, isLoading: loading, error } = useStorage('local:student'); -const studentCoefficients = ref() - const formattedDate = useDateFormat(student.value?.lastUpdate, 'DD/MM/YYYY HH:mm', { locales: 'pt-BR' }) -onMounted(async () => { - if (!student.value) { - return; +const studentCoefficients = ref(null) +const isFetching = ref(false) +const fetchError = ref(null) + +// Use watchEffect to trigger fetch when student data is available +watchEffect(() => { + // Only fetch if student data exists and has the required properties + if (student.value?.ra && student.value?.login) { + isFetching.value = true + fetchStudentCoefficients() } - const matriculaStudent = await getStudent(student.value.login, student.value.ra) - studentCoefficients.value = matriculaStudent; }) +async function fetchStudentCoefficients() { + try { + const response = await fetch(`${nextURL}/entities/students/student?ra=${student.value?.ra}&login=${student.value?.login}`) + + if (!response.ok) { + throw new Error('Failed to fetch student coefficients') + } + + studentCoefficients.value = await response.json() + fetchError.value = null + } catch (error) { + console.error('Error fetching student coefficients:', error) + fetchError.value = error instanceof Error ? error : new Error('An unknown error occurred') + studentCoefficients.value = null + } finally { + isFetching.value = false + } +}