Skip to content

Commit

Permalink
show user roles in Users table
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbinning committed Nov 6, 2024
1 parent c2f840c commit 9f9fdf0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions backend/managers/UsersManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def retrieve_user(self, id):
async with db_session_context() as session:
result = await session.execute(select(User).filter(User.id == id))
user = result.scalar_one_or_none()
return UserSchema(id=user.id, name=user.name, email=user.email) if user else None
return UserSchema(id=user.id, name=user.name, email=user.email, role=user.role) if user else None

async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='asc', filters=None):
async with db_session_context() as session:
Expand All @@ -59,7 +59,7 @@ async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='as
else:
query = query.filter(getattr(User, key) == value)

if sort_by and sort_by in ['id', 'name', 'email']:
if sort_by and sort_by in ['id', 'name', 'email', 'role']:
order_column = getattr(User, sort_by)
query = query.order_by(order_column.desc() if sort_order.lower() == 'desc' else order_column)

Expand All @@ -69,7 +69,8 @@ async def retrieve_users(self, offset=0, limit=100, sort_by=None, sort_order='as
users = [UserSchema(
id=user.id,
name=user.name,
email=user.email
email=user.email,
role=user.role
) for user in result.scalars().all()]

# Get total count
Expand Down
3 changes: 3 additions & 0 deletions backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class UserCreateSchema(UserBaseSchema):

class UserSchema(UserBaseSchema):
id: str
name: str
email: str
role: str

# Asset schemas
class AssetBaseSchema(BaseModel):
Expand Down
20 changes: 12 additions & 8 deletions frontend/src/users.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { useMediaQuery, Theme } from "@mui/material";
import { Create, Edit, EditButton, DeleteButton, List, SimpleList, Show, ShowButton, SimpleForm, SimpleShowLayout, Datagrid, TextField, TextInput, EmailField, useRecordContext } from "react-admin";
import { Create, Edit, EditButton, DeleteButton, List, SimpleList, Show, ShowButton, SimpleForm, SimpleShowLayout, Datagrid, TextField, TextInput, EmailField, SelectInput, useRecordContext } from "react-admin";

const UserTitle = () => {
const record = useRecordContext();
return <span>Users {record ? `- ${record.name}` : ""}</span>;
};

interface UserRecord {
id: string;
name: string;
email: string;
}
const roleChoices = [
{ id: 'user', name: 'User' },
{ id: 'admin', name: 'Admin' },
];

export const UserList = () => {
const isSmall = useMediaQuery<Theme>((theme) => theme.breakpoints.down("sm"));
return (
<List>
{isSmall ? (
<SimpleList
primaryText={(record: UserRecord) => record.name}
secondaryText={(record: UserRecord) => record.email}
primaryText={(record) => record.name}
secondaryText={(record) => record.email}
tertiaryText={(record) => record.role}
/>
) : (
<Datagrid rowClick="edit">
<TextField source="name" />
<EmailField source="email" />
<TextField source="role" />
<ShowButton />
<EditButton />
<DeleteButton />
Expand All @@ -40,6 +41,7 @@ export const UserShow = () => (
<TextField source="id" />
<TextField source="name" />
<EmailField source="email" />
<TextField source="role" />
</SimpleShowLayout>
</Show>
);
Expand All @@ -49,6 +51,7 @@ export const UserEdit = () => (
<SimpleForm>
<TextInput source="name" />
<TextInput source="email" />
<SelectInput source="role" choices={roleChoices} />
</SimpleForm>
</Edit>
);
Expand All @@ -58,6 +61,7 @@ export const UserCreate = () => (
<SimpleForm>
<TextInput source="name" />
<TextInput source="email" />
<SelectInput source="role" choices={roleChoices} defaultValue="user" />
</SimpleForm>
</Create>
);

0 comments on commit 9f9fdf0

Please sign in to comment.