add HOC for role restriction

This commit is contained in:
memeriau 2022-04-12 23:51:16 +02:00
parent b67cb0afb1
commit 089af3880a
3 changed files with 15 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import { useSelector } from "react-redux"
import { selectTeamList } from "../../store/teamList"
import TeamWithCandidates from "./TeamWithCandidates"
import styles from "./styles.module.scss"
import withUserRole from "../../utils/withUserRole"
const TeamsWithCandidates: FC = (): JSX.Element => {
const teams = useSelector(selectTeamList)
@ -25,4 +26,4 @@ const TeamsWithCandidates: FC = (): JSX.Element => {
)
}
export default memo(TeamsWithCandidates)
export default withUserRole("répartiteur", memo(TeamsWithCandidates))

View File

@ -86,7 +86,7 @@ export default [
loadData: loadAnnouncementsData,
},
{
path: "/team-assign-e26as",
path: "/team-assign",
component: AsyncTeamAssignment,
loadData: loadTeamAssignmentData,
},

View File

@ -0,0 +1,12 @@
import React from "react"
import { useSelector } from "react-redux"
import { selectUserRoles } from "../store/auth"
function withUserRole<T>(requiredRole: string, Component: React.ComponentType<T>) {
return (props: T): JSX.Element | null => {
const roles = useSelector(selectUserRoles)
return roles.includes(requiredRole) ? <Component {...props} /> : null
}
}
export default withUserRole