Skip to content

Commit

Permalink
feat: Added functionality to add tutor in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisBurger committed Nov 5, 2024
1 parent e264b69 commit fe98f03
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
44 changes: 44 additions & 0 deletions web/app/tutors/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use client';
import useApiServiceClient from "@/hooks/useApiServiceClient";
import useClientQuery from "@/hooks/useClientQuery";
import {User} from "@/service/types/usernator";
import EntityList, {EntityListCol} from "@/components/EntityList";
import {Button, Container, Group, Title} from "@mantine/core";
import {useState} from "react";
import CreateTutorModal from "@/components/CreateTutorModal";


const TutorsPage = () => {

const api = useApiServiceClient();
const [tutors, refetch] = useClientQuery<{tutors: User[]}>(() =>
api.getTutors(),
);
const [createModalOpen, setCreateModalOpen] = useState(false);

const cols: EntityListCol[] = [
{
field: "id",
label: "ID",
},
{
field: "username",
label: "Username",
},
];

return (
<Container fluid>
<Group>
<Title>Tutors</Title>
<Button onClick={() => setCreateModalOpen(true)}>Create tutor</Button>
</Group>
<EntityList cols={cols} rows={tutors?.tutors ?? []} />
{createModalOpen && (
<CreateTutorModal onClose={() => setCreateModalOpen(false)} refetch={refetch} />
)}
</Container>
);
}

export default TutorsPage;
56 changes: 56 additions & 0 deletions web/components/CreateTutorModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {Button, Group, Modal, PasswordInput, TextInput} from "@mantine/core";
import {useForm} from "@mantine/form";
import useApiServiceClient from "@/hooks/useApiServiceClient";
import {showNotification} from "@mantine/notifications";

interface CreateTutorModalProps {
onClose: () => void;
refetch: () => void;
}

const CreateTutorModal = ({onClose, refetch}: CreateTutorModalProps) => {


const api = useApiServiceClient();
const form = useForm({
initialValues: {
username: '',
password: ''
},
validate: {
username: (val) => val.trim() == '' ? 'Username should not be empty' : null,
password: (v) => v === '' ? 'The password should not be empty' : null,
}
});

const submit = form.onSubmit(async (values) => {
try {
await api.createTutor(values.username, values.password);
refetch();
onClose();
} catch (e: any) {
console.error(e);
showNotification({
title: 'Error',
message: e?.message ?? "Failed to create tutor",
});
}
})

return (
<Modal opened onClose={onClose} title="Create Tutor">
<form onSubmit={submit}>
<TextInput label="Username" key={form.key('username')} {...form.getInputProps('username')} />
<PasswordInput label="Password" key={form.key('password')} {...form.getInputProps('password')} />
<Group mt={10}>
<Button type="submit">Create tutor</Button>
<Button onClick={onClose} color="gray">
Cancel
</Button>
</Group>
</form>
</Modal>
);
}

export default CreateTutorModal;
8 changes: 8 additions & 0 deletions web/service/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ class ApiService {
return await this.post<CodeComment>(`/tasky/solutions/${solutionId}/code_comments`, {title, content});
}

public async createTutor(username: string, password: string): Promise<User> {
return await this.post<User>('/usernator/create_tutor', {username, password, email: ""});
}

public async getTutors(): Promise<{tutors: User[]}> {
return await this.get('/usernator/all-tutors');
}

public async createCodeTests(
groupId: number,
assignmentId: number,
Expand Down
7 changes: 7 additions & 0 deletions web/static/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const routes: Route[] = [
icon: <IconSchool />,
authRoles: [UserRoles.Tutor, UserRoles.Admin],
},
{
path: "/tutors",
name: "Tutors",
description: "All tutors in the system",
icon: <IconSchool />,
authRoles: [UserRoles.Admin],
},
{
path: "/my-groups",
name: "My Groups",
Expand Down

0 comments on commit fe98f03

Please sign in to comment.