Skip to content

Commit

Permalink
Improve admin user list
Browse files Browse the repository at this point in the history
  • Loading branch information
blazer82 committed Jul 28, 2023
1 parent db64131 commit 90ee4e2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
15 changes: 12 additions & 3 deletions components/UsersPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,32 @@ const UsersPage: React.FunctionComponent = () => {
<Table sx={{minWidth: 650}} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell></TableCell>
<TableCell>Email</TableCell>
<TableCell>Role</TableCell>
<TableCell>Accounts</TableCell>
<TableCell>Last Activity</TableCell>
<TableCell>Active</TableCell>
</TableRow>
</TableHead>
<TableBody>
{list?.map((user) => (
{list?.map((user, index) => (
<TableRow key={user._id}>
<TableCell>{index + 1}</TableCell>
<TableCell>
<NextLink href={`/admin/users/${user._id}`} passHref legacyBehavior>
<Link>{user.email}</Link>
</NextLink>
</TableCell>
<TableCell>{user.role}</TableCell>
<TableCell>
{user.accounts?.map(({_id, accountName}) => (
<React.Fragment key={_id}>
{accountName}
<br />
</React.Fragment>
))}
</TableCell>
<TableCell>{user.credentials?.updatedAt && formatDate(user.credentials?.updatedAt)}</TableCell>
<TableCell>{user.isActive ? 'active' : 'inactive'}</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
27 changes: 24 additions & 3 deletions pages/admin/users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,37 @@ export const getServerSideProps = wrapper.getServerSideProps((store) => async ({
});

await dbConnect();
const users = await UserModel.find({}).populate('credentials');

const users = await UserModel.aggregate([
{
$lookup: {
from: 'accounts',
localField: 'accounts',
foreignField: '_id',
as: 'accounts',
},
},
{
$lookup: {
from: 'usercredentials',
localField: 'credentials',
foreignField: '_id',
as: 'credentials',
},
},
{$sort: {'credentials.updatedAt': -1}},
]);

console.log('USERS', users);

store.dispatch(
listLoadSuccessful(
users.map((item) =>
JSON.parse(
JSON.stringify({
...item.toObject({virtuals: true}),
...item,
credentials: {
updatedAt: item.credentials?.updatedAt,
updatedAt: item.credentials && item.credentials[0].updatedAt,
},
}),
),
Expand Down

0 comments on commit 90ee4e2

Please sign in to comment.