-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as UserServiceModel from "@/models/service/user"; | ||
import { axiosClient } from "@/utils/axiosClient"; | ||
|
||
export async function getUserInfoList( | ||
limit?: number, | ||
offset?: number, | ||
): Promise<{ | ||
total: number; | ||
list: UserServiceModel.UserInfo[]; | ||
}> { | ||
limit = limit || 10; | ||
offset = offset || 0; | ||
|
||
let res = await axiosClient.get<{ | ||
total: number; | ||
list: UserServiceModel.UserInfo[]; | ||
}>(`/api/v1/user`, { | ||
params: { | ||
limit, | ||
offset, | ||
}, | ||
}); | ||
if (res.status !== 200) { | ||
throw Error("failed to get problem list"); | ||
} | ||
return res.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as UserServiceModel from "@/models/service/user"; | ||
import React from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import UserAvatar from "./UserAvatar"; | ||
|
||
export interface UserTableProps { | ||
data: UserServiceModel.UserInfo[]; | ||
showActions?: boolean; | ||
className?: string; | ||
} | ||
|
||
const UserTable: React.FC<UserTableProps> = (props) => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<> | ||
<div className={props.className}> | ||
<table className="table" aria-label="Problem Table"> | ||
<thead> | ||
<tr className="border-base-content/10"> | ||
<th key="avatar">{t("Avatar")}</th> | ||
<th key="name">{t("Name")}</th> | ||
<th key="account">{t("Account")}</th> | ||
<th key="roles">{t("Roles")}</th> | ||
{/* {props.showActions && <th key="actions">{t("Actions")}</th>} */} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{props.data.map((userInfo) => ( | ||
<tr key={userInfo.account} className="border-base-content/10"> | ||
<td className="flex items-center gap-3 py-2"> | ||
<div className="avatar"> | ||
<UserAvatar | ||
alt={userInfo.name} | ||
avatarUrl={userInfo.avatarUrl} | ||
/> | ||
</div> | ||
</td> | ||
<th>{userInfo.name}</th> | ||
<td>{userInfo.account}</td> | ||
<td> | ||
<UserRoles roles={userInfo.roles ?? []} /> | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
const UserRoles: React.FC<{ roles: string[] | undefined }> = (props) => { | ||
if (props.roles === undefined) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<div className="space-x-2"> | ||
{props.roles.map((role) => ( | ||
<div | ||
key={role} | ||
className="badge border-0 bg-base-300 font-semibold text-base-content/80" | ||
> | ||
{role} | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default UserTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as UserServiceModel from "@/models/service/user"; | ||
import * as UserService from "@/apis/user"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useUserInfoList = () => { | ||
const [userInfoList, setUserInfoList] = useState<UserServiceModel.UserInfo[]>( | ||
[], | ||
); | ||
const [total, setTotal] = useState<number>(0); | ||
const [limit, setLimit] = useState<number>(10); | ||
const [offset, setOffset] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
UserService.getUserInfoList(limit, offset).then((res) => { | ||
setUserInfoList(res.list); | ||
setTotal(res.total); | ||
}); | ||
}, [limit, offset]); | ||
|
||
function getUserInfoList() { | ||
return userInfoList; | ||
} | ||
|
||
function getPageCount(limit: number) { | ||
return Math.ceil(total / limit); | ||
} | ||
|
||
function setPagenation(limit: number, offset: number) { | ||
setLimit(limit); | ||
setOffset(offset); | ||
} | ||
|
||
return { getUserInfoList, getPageCount, setPagenation }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as UserServiceModel from "@/models/service/user"; | ||
|
||
export const UserInfoList: UserServiceModel.UserInfo[] = [ | ||
{ | ||
name: "mock-user", | ||
account: "mock-user", | ||
roles: ["admin", "user"], | ||
}, | ||
{ | ||
name: "mock-user-2", | ||
account: "mock-user-2", | ||
roles: ["user"], | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useEffect } from "react"; | ||
import Pagination from "@/components/control/Pagination"; | ||
import { useUserInfoList } from "@/hooks/user"; | ||
import UserTable from "@/components/display/UserTable"; | ||
|
||
const countPerPageSelections = [10, 25, 50]; | ||
|
||
const UserList: React.FC = () => { | ||
const { getUserInfoList, getPageCount, setPagenation } = useUserInfoList(); | ||
const [countPerPage, setCountPerPage] = React.useState( | ||
countPerPageSelections[0], | ||
); | ||
const [page, setPage] = React.useState(0); | ||
|
||
useEffect(() => { | ||
setPagenation(countPerPage, page * countPerPage); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [countPerPage, page]); | ||
|
||
return ( | ||
<div className="card card-bordered flex w-full flex-col rounded border-base-content/10 bg-base-100"> | ||
<UserTable | ||
data={getUserInfoList()} | ||
showActions={true} | ||
className={ | ||
getUserInfoList().length === 1 | ||
? "" | ||
: "border-b border-base-content/10" | ||
} | ||
/> | ||
<Pagination | ||
page={page} | ||
pageCount={getPageCount(countPerPage)} | ||
setCountPerPage={setCountPerPage} | ||
countPerPage={countPerPage} | ||
countPerPageSelections={countPerPageSelections} | ||
setPage={setPage} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UserList; |