Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(init): add permissions for routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Nov 27, 2019
1 parent d3835d6 commit 7a19200
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum Permissions {
ReadPatients = 'read:patients',
WritePatients = 'write:patients'
}

export default Permissions
10 changes: 10 additions & 0 deletions src/components/PrivateRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import { Route, Redirect } from 'react-router-dom'

const PrivateRoute = ({ component, isAuthenticated, ...rest }: any) => {
const routeComponent = (props: any) =>
isAuthenticated ? React.createElement(component, props) : <Redirect to={{ pathname: '/' }} />
return <Route {...rest} render={routeComponent} />
}

export default PrivateRoute
24 changes: 21 additions & 3 deletions src/containers/HospitalRun.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import React from 'react'
import { Switch, Route } from 'react-router-dom'
import { useSelector } from 'react-redux'
import Sidebar from 'components/Sidebar'
import Permissions from 'Permissions'
import Dashboard from './Dashboard'
import Patients from './Patients'
import NewPatient from './NewPatient'
import ViewPatient from './ViewPatient'
import { RootState } from '../store/store'
import Navbar from '../components/Navbar'
import PrivateRoute from '../components/PrivateRoute'

const HospitalRun = () => {
const { title } = useSelector((state: RootState) => state.title)
const { permissions } = useSelector((state: RootState) => state.user)
return (
<div>
<Navbar />
Expand All @@ -24,9 +27,24 @@ const HospitalRun = () => {
<div>
<Switch>
<Route exact path="/" component={Dashboard} />
<Route exact path="/patients" component={Patients} />
<Route exact path="/patients/new" component={NewPatient} />
<Route exact path="/patients/:id" component={ViewPatient} />
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.ReadPatients)}
exact
path="/patients"
component={Patients}
/>
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.WritePatients)}
exact
path="/patients/new"
component={NewPatient}
/>
<PrivateRoute
isAuthenticated={permissions.includes(Permissions.ReadPatients)}
exact
path="/patients/:id"
component={ViewPatient}
/>
</Switch>
</div>
</main>
Expand Down
24 changes: 24 additions & 0 deletions src/slices/user-slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import Permissions from 'Permissions'

interface UserState {
permissions: Permissions[]
}

const initialState: UserState = {
permissions: [Permissions.ReadPatients, Permissions.WritePatients],
}

const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
fetchPermissions(state, { payload }: PayloadAction<Permissions[]>) {
state.permissions = payload
},
},
})

export const { fetchPermissions } = userSlice.actions

export default userSlice.reducer
2 changes: 2 additions & 0 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { createLogger } from 'redux-logger'
import patient from '../slices/patient-slice'
import patients from '../slices/patients-slice'
import title from '../slices/title-slice'
import user from '../slices/user-slice'

const reducer = combineReducers({
patient,
patients,
title,
user,
})

const store = configureStore({
Expand Down

0 comments on commit 7a19200

Please sign in to comment.