Skip to content

Commit

Permalink
merge from dev
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Aug 7, 2024
2 parents d595435 + 10b08fe commit ee87f5e
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"✅ Do add `devDependencies` below that are `peerDependencies` in the CFL package."
],
"dependencies": {
"codeforlife": "github:ocadotechnology/codeforlife-package-javascript#v2.1.4",
"codeforlife": "github:ocadotechnology/codeforlife-package-javascript#v2.2.0",
"crypto-js": "^4.2.0"
},
"devDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions src/pages/resetPassword/PasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Stack, Typography } from "@mui/material"
import { CheckCircleOutline as CheckCircleOutlineIcon } from "@mui/icons-material"
import { type FC } from "react"
import { LinkButton } from "codeforlife/components/router"
import { submitForm } from "codeforlife/utils/form"

import { NewPasswordField } from "../../components/form"
import { paths } from "../../router"
Expand All @@ -15,9 +16,9 @@ export interface PasswordFormProps {
}

const PasswordForm: FC<PasswordFormProps> = ({ userType, userId, token }) => {
const [resetPassword, result] = useResetPasswordMutation()
const [resetPassword, { isSuccess }] = useResetPasswordMutation()

return result.isSuccess ? (
return isSuccess ? (
<Stack gap={1} alignItems="center">
<Typography textAlign="center" variant="h4">
Your password has been reset
Expand All @@ -44,12 +45,12 @@ const PasswordForm: FC<PasswordFormProps> = ({ userType, userId, token }) => {
</Typography>
<form.Form
initialValues={{
id: userId,
token,
password: "",
password_repeat: "",
}}
onSubmit={({ password }) =>
resetPassword([userId, { token, password }])
}
onSubmit={submitForm(resetPassword)}
>
<NewPasswordField userType={userType} />
<Stack mt={3} direction="row" gap={5} justifyContent="center">
Expand Down
14 changes: 8 additions & 6 deletions src/pages/teacherDashboard/school/School.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ const School: FC<SchoolProps> = ({ authUser, view }) => {
</Grid>
)}
</page.Section>
<page.Section>
<Typography variant="h5">
These teachers are invited to your school but have not joined yet
</Typography>
<TeacherInvitationTable authUser={authUser} />
</page.Section>
{authUser.teacher.is_admin && (
<page.Section>
<Typography variant="h5">
These teachers are invited to your school but have not joined yet
</Typography>
<TeacherInvitationTable authUser={authUser} />
</page.Section>
)}
{authUser.teacher.is_admin && (
<page.Section boxProps={{ bgcolor: "info.main" }}>
<UpdateSchoolForm school={school} />
Expand Down
51 changes: 35 additions & 16 deletions src/pages/teacherDashboard/school/TeacherInvitationTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { Button, Typography } from "@mui/material"
import { type FC } from "react"
import { type SchoolTeacherUser } from "codeforlife/api"
import { TablePagination } from "codeforlife/components"
import type { TypedMutationTrigger } from "@reduxjs/toolkit/query/react"
import { useNavigate } from "codeforlife/hooks"

import * as table from "../../../components/table"
import {
useDestroySchoolTeacherInvitationMutation,
useLazyListSchoolTeacherInvitationsQuery,
useRefreshSchoolTeacherInvitationMutation,
} from "../../../api/schoolTeacherInvitation"
import { type RetrieveUserResult } from "../../../api/user"

Expand All @@ -25,6 +27,8 @@ const TeacherInvitationTable: FC<TeacherInvitationTableProps> = ({
authUser,
}) => {
const navigate = useNavigate()
const [refreshSchoolTeacherInvitation] =
useRefreshSchoolTeacherInvitationMutation()
const [destroySchoolTeacherInvitation] =
useDestroySchoolTeacherInvitationMutation()

Expand All @@ -34,6 +38,25 @@ const TeacherInvitationTable: FC<TeacherInvitationTableProps> = ({
})
}

function handleClickAction(
mutationTrigger: TypedMutationTrigger<any, number, any>,
id: number,
successMessage: string,
errorMessage: string,
) {
return () => {
mutationTrigger(id)
.unwrap()
.then(() => {
showNotification(successMessage)
})
.catch(error => {
if (error) console.error(error)
showNotification(errorMessage, true)
})
}
}

return (
<TablePagination
useLazyListQuery={useLazyListSchoolTeacherInvitationsQuery}
Expand Down Expand Up @@ -103,28 +126,24 @@ const TeacherInvitationTable: FC<TeacherInvitationTableProps> = ({
)}
<Button
endIcon={<EmailOutlinedIcon />}
onClick={() => {
alert("TODO: create action of backend for this")
}}
onClick={handleClickAction(
refreshSchoolTeacherInvitation,
id,
"Successfully resent invitation.",
"Failed to resend invitation.",
)}
>
Resend invite
</Button>
<Button
className="alert"
endIcon={<DeleteOutlineIcon />}
onClick={() => {
destroySchoolTeacherInvitation(id)
.unwrap()
.then(() => {
showNotification("Invitation successfully deleted.")
})
.catch(() => {
showNotification(
"Failed to delete invitation.",
true,
)
})
}}
onClick={handleClickAction(
destroySchoolTeacherInvitation,
id,
"Invitation successfully deleted.",
"Failed to delete invitation.",
)}
>
Delete
</Button>
Expand Down
154 changes: 154 additions & 0 deletions src/pages/teacherDashboard/school/TransferClasses.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import * as form from "codeforlife/components/form"
import * as page from "codeforlife/components/page"
import { Link, LinkButton } from "codeforlife/components/router"
import { type SchoolTeacher, type User } from "codeforlife/api"
import { Stack, Typography } from "@mui/material"
import { type FC } from "react"
import { TablePagination } from "codeforlife/components"
import { submitForm } from "codeforlife/utils/form"
import { useNavigate } from "codeforlife/hooks"

import * as table from "../../../components/table"
import {
useLazyListClassesQuery,
useUpdateClassesMutation,
} from "../../../api/klass"
import { paths } from "../../../router"
import { useLazyListUsersQuery } from "../../../api/user"
import { useRemoveTeacherFromSchoolMutation } from "../../../api/teacher"

export interface TransferClassesProps {
authUserId: User["id"]
user: Pick<User, "id" | "first_name" | "last_name"> & {
teacher: Pick<SchoolTeacher, "id" | "school">
}
}

const TransferClasses: FC<TransferClassesProps> = ({ authUserId, user }) => {
const [updateClasses] = useUpdateClassesMutation()
const [removeTeacherFromSchool] = useRemoveTeacherFromSchoolMutation()
const navigate = useNavigate()

const isSelf = user.id === authUserId

function handleRemoveTeacherFromSchool() {
removeTeacherFromSchool(user.teacher.id)
.unwrap()
.then(() => {
if (isSelf) {
navigate(paths.teacher.onboarding._)
} else {
navigate(paths.teacher.dashboard.tab.school._, {
state: {
notifications: [
{
props: {
children:
"The teacher has been successfully removed from your school or club, and their classes were successfully transferred.",
},
},
],
},
})
}
})
.catch(() => {
// TODO: error handling strategy.
alert("Failed to remove teacher from school")
})
}

return (
<>
<page.Notification>
{isSelf
? "You still have classes, you must first move them to another teacher within your school or club."
: "This teacher still has classes assigned to them. You must first move them to another teacher in your school or club."}
</page.Notification>
<page.Section>
<Typography variant="h4" align="center" marginBottom={5}>
Move all classes for teacher {user.first_name} {user.last_name}
</Typography>
{/* TODO: discuss if this link is needed since cancel button is below */}
<Link className="back-to" to={paths.teacher.dashboard.tab.school._}>
dashboard
</Link>
<Typography marginY={3}>
Please specify which teacher you would like the classes below to be
moved to.
</Typography>
<TablePagination
useLazyListQuery={useLazyListClassesQuery}
filters={{ teacher: user.teacher.id }}
>
{classes => {
if (!classes.length) {
handleRemoveTeacherFromSchool()
return <></>
}

return (
<form.Form
initialValues={classes.reduce(
(values, klass) => ({
...values,
[klass.id]: { teacher: undefined },
}),
{},
)}
onSubmit={submitForm(updateClasses)}
>
<table.Table
className="body"
titles={["Class name", "New teacher"]}
>
{classes.map(klass => (
<table.Body key={klass.id}>
<table.Cell>
<Typography variant="subtitle1">
{klass.name}
</Typography>
</table.Cell>
<table.Cell direction="column" alignItems="flex-start">
<form.ApiAutocompleteField
useLazyListQuery={useLazyListUsersQuery}
filterOptions={{ only_teachers: true, _id: user.id }}
getOptionLabel={({ first_name, last_name }) =>
`${first_name} ${last_name}`
}
getOptionKey={({ teacher }) =>
(teacher as SchoolTeacher).id
}
textFieldProps={{
required: true,
name: `${klass.id}.teacher`,
}}
searchKey="name"
/>
</table.Cell>
</table.Body>
))}
</table.Table>
<Stack direction="row" spacing={2}>
<LinkButton
variant="outlined"
to={paths.teacher.dashboard.tab.school._}
>
Cancel
</LinkButton>
<form.SubmitButton>
{isSelf
? "Move classes and leave"
: "Move classes and remove teacher"}
</form.SubmitButton>
</Stack>
</form.Form>
)
}}
</TablePagination>
</page.Section>
</>
)
}

export default TransferClasses
1 change: 0 additions & 1 deletion src/router/routes/teacher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const teacher = (
<>
{/* <Route path={paths.teacher._} element={<Teacher />} /> */}
{/* <Route path={paths.teacher.onboarding._} element={<TeacherOnboarding />} /> */}

<Route
path={paths.teacher.dashboard.tab.school.leave._}
element={<TeacherDashboard tab="school" view="leave" />}
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2650,9 +2650,9 @@ clsx@^2.1.0, clsx@^2.1.1:
resolved "https://registry.yarnpkg.com/clsx/-/clsx-2.1.1.tgz#eed397c9fd8bd882bfb18deab7102049a2f32999"
integrity sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==

"codeforlife@github:ocadotechnology/codeforlife-package-javascript#v2.1.4":
version "2.1.4"
resolved "https://codeload.github.com/ocadotechnology/codeforlife-package-javascript/tar.gz/c7b9913d5caab1381885b019f9c3470d8269e589"
"codeforlife@github:ocadotechnology/codeforlife-package-javascript#v2.2.0":
version "2.2.0"
resolved "https://codeload.github.com/ocadotechnology/codeforlife-package-javascript/tar.gz/f90ad0fa5206c8d78fc103b140609c2290f52417"
dependencies:
"@emotion/react" "^11.10.6"
"@emotion/styled" "^11.10.6"
Expand Down

0 comments on commit ee87f5e

Please sign in to comment.