Skip to content

Commit

Permalink
fix subjects query
Browse files Browse the repository at this point in the history
  • Loading branch information
reyniersbram committed May 13, 2024
1 parent 7c818d7 commit bec85c2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/home/cards/SubjectsCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- TODO: Add error handling -->
<h1 v-else-if="isSubjectsError">Error loading subjects</h1>
<HomeScreenCard v-else :title="'homescreen.subjects'">
<SubjectItem v-for="subject in subjects" :subject="subject" :key="subject.id" />
<SubjectItem v-for="subject in [...subjects!.as_student, ...subjects!.as_instructor]" :subject="subject" :key="subject.id" />
</HomeScreenCard>
</template>

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/queries/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { getSubjectProjects } from "@/services/project";
import type User from "@/models/User";
import type Subject from "@/models/Subject";
import type { UserSubjectList } from "@/models/Subject";
import type Project from "@/models/Project";

function SUBJECT_QUERY_KEY(subjectId: number | string): (string | number)[] {
Expand Down Expand Up @@ -65,8 +66,8 @@ export function useSubjectUuidQuery(
/**
* Query composable for fetching all subjects of the current user
*/
export function useSubjectsQuery(): UseQueryReturnType<Subject[], Error> {
return useQuery<Subject[], Error>({
export function useSubjectsQuery(): UseQueryReturnType<UserSubjectList, Error> {
return useQuery<UserSubjectList, Error>({
queryKey: SUBJECTS_QUERY_KEY(),
queryFn: getSubjects,
});
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/services/subject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type User from "@/models/User";
import type Subject from "@/models/Subject";
import type { UserSubjectList } from "@/models/Subject";
import { authorized_fetch } from "@/services";

/**
Expand All @@ -19,14 +20,10 @@ export async function getSubjectByUuid(subjectUuid: string): Promise<Subject> {
/**
* Fetches all subjects the current user is enrolled in.
*/
export async function getSubjects(): Promise<Subject[]> {
const result = await authorized_fetch<{ as_instructor: Subject[]; as_student: Subject[] }>(
"/api/users/me/subjects",
{
method: "GET",
}
);
return [...result.as_instructor, ...result.as_student];
export async function getSubjects(): Promise<UserSubjectList> {
return await authorized_fetch<UserSubjectList>("/api/users/me/subjects", {
method: "GET",
});
}

/**
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/views/CreateProjectView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ const assistants = computed(
);
const subjects = computed(
() =>
// mySubjectsData.value?.as_instructor.map(({ name, id }) => ({ text: name, value: id })) || []
subjectsData.value?.map(({ name, id }) => ({ text: name, value: id })) || []
subjectsData.value?.as_instructor.map(({ name, id }) => ({ text: name, value: id })) || []
);
const groupProjectOptions = [
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/views/subject/SubjectsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<div v-if="isError" class="v-container">
<p>Error: {{ error }}</p>
</div>
<div v-if="isLoading">
Loading...
</div>

<BackgroundContainer v-else>
<v-row>
Expand All @@ -12,7 +15,7 @@
<v-row>
<v-col>
<SubjectCard
v-for="(subject, index) in data"
v-for="(subject, index) in subjectsList"
:subject="subject"
:is-loading="isLoading"
:key="index"
Expand All @@ -28,8 +31,12 @@ import { useSubjectsQuery } from "@/queries/Subject";
import BackgroundContainer from "@/components/BackgroundContainer.vue";
import SubjectsHeaderContainer from "@/components/subjects/SubjectsHeaderContainer.vue";
import SubjectCard from "@/components/subjects/SubjectCard.vue";
import {computed} from "vue";
const { data, error, isLoading, isError } = useSubjectsQuery();
const { data: subjects, error, isLoading, isError } = useSubjectsQuery();
const subjectsList = computed(() => {
return [...subjects.value!.as_student, ...subjects.value!.as_instructor] || [];
});
</script>

<style scoped></style>

0 comments on commit bec85c2

Please sign in to comment.