-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from nattvara/feature/list-admin-rooms
Show courses user administrates in navbar
- Loading branch information
Showing
9 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { HttpError, getSessionId, makeUrl } from "@/api/http"; | ||
|
||
export interface Course { | ||
canvas_id: string; | ||
language: string; | ||
name: string; | ||
} | ||
|
||
export interface Courses { | ||
courses: Course[]; | ||
} | ||
|
||
export async function fetchCourses(): Promise<Courses> { | ||
const sessionCookie = getSessionId() as string; | ||
|
||
const response = await fetch(makeUrl(`/admin/courses`), { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Session-ID": sessionCookie, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.json(); | ||
throw new HttpError(response, errorBody, response.status); | ||
} | ||
|
||
const data = (await response.json()) as Courses; | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Button, SimpleGrid, Title } from "@mantine/core"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
import { fetchCourses } from "@/api/admin"; | ||
|
||
import styles from "./styles.module.css"; | ||
|
||
export default function CourseList() { | ||
const coursesQuery = useQuery({ | ||
queryKey: ["courses"], | ||
queryFn: () => fetchCourses(), | ||
}); | ||
|
||
if (!coursesQuery.data) { | ||
return <></>; | ||
} | ||
|
||
if (coursesQuery.data.courses.length === 0) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<SimpleGrid cols={1} className={styles.root}> | ||
<Title order={4}>Courses</Title> | ||
{coursesQuery.data.courses.map((course) => ( | ||
<Button | ||
key={course.canvas_id} | ||
variant="light" | ||
size="xs" | ||
component="a" | ||
href={`/course/${course.canvas_id}/chats`} | ||
target="_blank" | ||
> | ||
{course.name} | ||
</Button> | ||
))} | ||
</SimpleGrid> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
.root { | ||
padding-top: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import { useTranslation } from "next-i18next"; | |
import { useRouter } from "next/router"; | ||
import { MouseEventHandler, useEffect } from "react"; | ||
|
||
import { CourseList } from "@/components/navigation"; | ||
|
||
import { getSession } from "@/api/session"; | ||
|
||
const ContactEmail = "[email protected]"; | ||
|
@@ -68,6 +70,8 @@ export default function HeaderNavbar(props: HeaderNavbarProps) { | |
<Button leftSection={<IconMail size={14} />} variant="default" component="a" href={`mailto:${ContactEmail}`}> | ||
{t("header.contact_researcher")} | ||
</Button> | ||
|
||
<CourseList /> | ||
</AppShell.Navbar> | ||
</> | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import CourseList from "./CourseList/CourseList"; | ||
import HeaderNavbar from "./HeaderNavbar/HeaderNavbar"; | ||
|
||
export { HeaderNavbar }; | ||
export { HeaderNavbar, CourseList }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter, Depends | ||
from pydantic import BaseModel | ||
|
||
from db.actions.course import find_course_by_canvas_id | ||
from http_api.auth import get_current_session | ||
from config.logger import log | ||
from db.models import Session | ||
|
||
router = APIRouter() | ||
|
||
|
||
class Course(BaseModel): | ||
canvas_id: str | ||
language: str | ||
name: str | ||
|
||
|
||
class CourseResponse(BaseModel): | ||
courses: List[Course] | ||
|
||
|
||
@router.get( | ||
'/admin/courses', | ||
dependencies=[Depends(get_current_session)], | ||
response_model=CourseResponse | ||
) | ||
async def get_courses_user_can_administrate(session: Session = Depends(get_current_session)) -> CourseResponse: | ||
courses = [] | ||
for canvas_id in session.admin_courses: | ||
course = find_course_by_canvas_id(canvas_id) | ||
if course is None: | ||
log().warning(f"found course id that doesn't exist: {canvas_id}, in session: {session.public_id}") | ||
continue | ||
courses.append(Course( | ||
canvas_id=course.canvas_id, | ||
language=course.language, | ||
name=course.name | ||
)) | ||
return CourseResponse(courses=courses) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
def test_user_courses_user_can_administrate_are_returned_in_list_of_courses( | ||
api_client, | ||
valid_course, | ||
authenticated_session, | ||
): | ||
authenticated_session.session.admin_courses.append(valid_course.canvas_id) | ||
authenticated_session.session.save() | ||
|
||
response = api_client.get('/admin/courses', headers=authenticated_session.headers) | ||
assert response.status_code == 200 | ||
assert response.json()['courses'][0]['canvas_id'] == valid_course.canvas_id |