Skip to content

Commit

Permalink
add client-side API + hook to get courses by filter
Browse files Browse the repository at this point in the history
  • Loading branch information
benny-n committed Feb 6, 2022
1 parent f0d64c2 commit 83bdcc7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/sogrim-app/src/hooks/apiHooks/useCoursesByFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useQuery } from "react-query";
import { getCourseByFilter } from "../../services/api";

export default function useCoursesByFilter(
authToken: any,
trigger: boolean,
filterName: string,
filter: string
) {
return useQuery(
"coursesByFilter",
() => getCourseByFilter(authToken, filterName, filter),
{
retry: false,
enabled: !!authToken && trigger,
}
);
}
26 changes: 25 additions & 1 deletion packages/sogrim-app/src/services/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { UserDetails, UserState, Catalog } from "../types/data-types";
import { UserDetails, UserState, Catalog, Course } from "../types/data-types";
import { API_URL } from "./api-url";

export const getCatalogs = async (authToken: any): Promise<Catalog[]> => {
Expand All @@ -19,6 +19,30 @@ export const getCatalogs = async (authToken: any): Promise<Catalog[]> => {
return res.data;
};

export const getCourseByFilter = async (
authToken: any,
filterName: string,
filter: string
): Promise<Course[]> => {
let fallback: any;
let res: any;
if (!filter) {
return [];
}
try {
res =
(await axios.get(`${API_URL}/students/courses?${filterName}=${filter}`, {
headers: {
authorization: `${authToken}`,
},
})) || fallback;
} catch (e) {
res = fallback;
throw e;
}
return res.data;
};

export const putUserCatalog = async (
authToken: any,
userCatalogId: string
Expand Down

0 comments on commit 83bdcc7

Please sign in to comment.