Skip to content

Commit

Permalink
Merge pull request #42 from avantifellows/feat/fetch-teacher-using-su…
Browse files Browse the repository at this point in the history
…bject-id

Feat: API route changes based on new schema
  • Loading branch information
Bahugunajii authored Mar 22, 2024
2 parents 9f59bd8 + b971bb5 commit fad1825
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions api/afdb/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export const getResourcesWithSource = async (topicIds: number[]): Promise<Resour
return resourceResponses.flat();
};

export const getTeachers = async (id?: number, subject?: string): Promise<Teacher[]> => {
export const getTeachers = async (id?: number, subject_id?: number): Promise<Teacher[]> => {
const queryParams = new URLSearchParams();
if (id !== undefined) queryParams.append('id', id.toString());
if (subject !== undefined) queryParams.append('subject', subject.toString());
if (subject_id !== undefined) queryParams.append('subject_id', subject_id.toString());

return fetchWithParams('teacher', queryParams);
};
Expand Down
14 changes: 7 additions & 7 deletions api/afdb/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export const getGroupUser = async (userDbId: number) => {
};


export const getGroupSessions = async (groupTypeId: number) => {
export const getGroupSessions = async (groupId: number) => {
try {
const queryParams = new URLSearchParams({
group_type_id: groupTypeId.toString(),
group_id: groupId.toString(),
});

const urlWithParams = `${url}/group-session?${queryParams.toString()}`;
Expand Down Expand Up @@ -85,23 +85,23 @@ export const getSessionSchedule = async (sessionId: number, batchId?: number) =>
}
};

export const getGroupTypes = async (groupTypeId: number) => {
export const getGroup = async (groupId: number) => {
try {
const queryParams = new URLSearchParams({
id: groupTypeId.toString(),
id: groupId.toString(),
type: "batch"
});
const urlWithParams = `${url}/group-type?${queryParams.toString()}`;
const urlWithParams = `${url}/group?${queryParams.toString()}`;
const response = await fetch(urlWithParams, getFetchConfig(bearerToken));

if (!response.ok) {
throw new Error(`Error in fetching Group Type Details: ${response.statusText}`);
throw new Error(`Error in fetching Group Details: ${response.statusText}`);
}

const data = await response.json();
return data;
} catch (error) {
console.error("Error in fetching Group Type Details:", error);
console.error("Error in fetching Group Details:", error);
throw error;
}
};
Expand Down
2 changes: 1 addition & 1 deletion app/library/class/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ClassLibrary = () => {
const actualTabName = tabName.toLowerCase();
const subjectData = await getSubjects(actualTabName);
const gradeData = await getGrades(selectedGrade);
const teacherData = await getTeachers(undefined, actualTabName);
const teacherData = await getTeachers(undefined, subjectData[0].id);
setTeachers(teacherData);
if (teacherData.length > 0 && selectedTeacher === undefined) {
setSelectedTeacher(teacherData[0].id);
Expand Down
12 changes: 6 additions & 6 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useAuth } from "@/services/AuthContext";
import TopBar from "@/components/TopBar";
import BottomNavigationBar from "@/components/BottomNavigationBar";
import { getGroupUser, getGroupSessions, getGroupTypes, getQuizBatchData, getSessionSchedule } from "@/api/afdb/session";
import { getGroupUser, getGroupSessions, getGroup, getQuizBatchData, getSessionSchedule } from "@/api/afdb/session";
import { useState, useEffect } from "react";
import { GroupUser, GroupSession, QuizSession, SessionSchedule, MessageDisplayProps } from "./types";
import Link from "next/link";
Expand All @@ -30,16 +30,16 @@ export default function Home() {
const groupUserData = await getGroupUser(userDbId!);

const groupSessions = await Promise.all(groupUserData.map(async (userData: GroupUser) => {
const groupType = await getGroupTypes(userData.group_type_id);
const group = await getGroup(userData.group_id);

const groupTypeIds = groupType.map((type: any) => type.id);
const groupIds = group.map((type: any) => type.id);

const quizIds = groupType.map((quiz: any) => quiz.child_id.parent_id)
const quizIds = group.map((quiz: any) => quiz.child_id.parent_id)

const batchId = groupType.map((groupTypeData: any) => groupTypeData.child_id.id)
const batchId = group.map((groupData: any) => groupData.child_id.id)
setBatchId(batchId[0])

const groupSessionData = await Promise.all(groupTypeIds.map(async (groupId: number) => {
const groupSessionData = await Promise.all(groupIds.map(async (groupId: number) => {
return await getGroupSessions(groupId);
}));

Expand Down
4 changes: 2 additions & 2 deletions app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ export interface User {

export interface GroupSession {
session_id: number;
group_type_id: number
group_id: number
}

export interface GroupUser {
user_id: number;
group_type_id: number
group_id: number
}

export interface Student {
Expand Down

0 comments on commit fad1825

Please sign in to comment.