Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b45ddaf

Browse files
committed
Start adding table
1 parent 201b34a commit b45ddaf

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

site/src/api/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface UserResponse {
1515
readonly username: string
1616
readonly email: string
1717
readonly created_at: string
18+
siteRole?: "Admin" | "Member"
1819
}
1920

2021
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ComponentMeta, Story } from "@storybook/react"
2+
import React from "react"
3+
import { MockUser, MockUser2 } from "../../test_helpers"
4+
import { UsersTable, UsersTableProps } from "./UsersTable"
5+
6+
export default {
7+
title: "Components/UsersTable",
8+
component: UsersTable,
9+
} as ComponentMeta<typeof UsersTable>
10+
11+
const Template: Story<UsersTableProps> = (args) => <UsersTable {...args} />
12+
13+
export const Example = Template.bind({})
14+
Example.args = {
15+
users: [
16+
MockUser,
17+
MockUser2
18+
]
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react"
2+
import { Pager, UserResponse } from "../../api/types"
3+
import { Column, Table } from "../../components/Table"
4+
import { EmptyState } from "../EmptyState"
5+
import { UserCell } from "../Table/Cells/UserCell"
6+
7+
const Language = {
8+
usersTitle: "All users",
9+
emptyMessage: "No users found",
10+
usernameLabel: "User",
11+
siteRoleLabel: "Site Role"
12+
}
13+
14+
const emptyState = (
15+
<EmptyState
16+
message={Language.emptyMessage}
17+
/>
18+
)
19+
20+
const columns: Column<UserResponse>[] = [
21+
{
22+
key: "email",
23+
name: Language.usernameLabel,
24+
renderer: (field, data) => {
25+
return <UserCell Avatar={{ username: data.email }} primaryText={data.email} />
26+
},
27+
},
28+
{
29+
key: "siteRole",
30+
name: Language.siteRoleLabel
31+
}
32+
]
33+
34+
export interface UsersTableProps {
35+
users: UserResponse[]
36+
pager?: Pager
37+
}
38+
39+
export const UsersTable: React.FC<UsersTableProps> = ({ users, pager }) => {
40+
return (
41+
<Table
42+
columns={columns}
43+
data={users}
44+
title={Language.usersTitle}
45+
emptyState={emptyState}
46+
/>
47+
)
48+
}

site/src/pages/UsersPage/UsersPage.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@ import { ErrorSummary } from "../../components/ErrorSummary"
44
import { UsersTable } from "../../components/UsersTable/UsersTable"
55
import { XServiceContext } from "../../xServices/StateContext"
66

7-
export type Role = "Admin" | "Member"
8-
9-
export interface User {
10-
username: string
11-
email: string
12-
siteRole: Role
13-
}
14-
157
export const UsersPage: React.FC = () => {
168
const xServices = useContext(XServiceContext)
179
const [usersState] = useActor(xServices.usersXService)
18-
const { users, getUsersError } = usersState.context
10+
const { users, pager, getUsersError } = usersState.context
1911

2012
if (usersState.matches("error")) {
2113
return <ErrorSummary error={getUsersError} />
2214
} else {
23-
return <UsersTable users={users} />
15+
return <UsersTable users={users} pager={pager} />
2416
}
2517
}

site/src/test_helpers/entities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ export const MockUser: UserResponse = {
2424
username: "TestUser",
2525
2626
created_at: "",
27+
siteRole: "Admin"
2728
}
2829

2930
export const MockUser2: UserResponse = {
3031
id: "test-user-2",
3132
username: "TestUser2",
3233
3334
created_at: "",
35+
siteRole: "Member"
3436
}
3537

3638
export const MockPager: Pager = {

0 commit comments

Comments
 (0)