-
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.
feat: Added functionality to add tutor in frontend
- Loading branch information
1 parent
e264b69
commit fe98f03
Showing
4 changed files
with
115 additions
and
0 deletions.
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,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; |
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,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; |
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