| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import React, { useEffect, useState } from 'react';
- import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react';
- import { Link } from 'react-router-dom';
- import { API, showError, showSuccess } from '../helpers';
- import { ITEMS_PER_PAGE } from '../constants';
- function renderRole(role) {
- switch (role) {
- case 1:
- return <Label>普通用户</Label>;
- case 10:
- return <Label color='yellow'>管理员</Label>;
- case 100:
- return <Label color='orange'>超级管理员</Label>;
- default:
- return <Label color='red'>未知身份</Label>;
- }
- }
- const ChannelsTable = () => {
- const [users, setUsers] = useState([]);
- const [loading, setLoading] = useState(true);
- const [activePage, setActivePage] = useState(1);
- const [searchKeyword, setSearchKeyword] = useState('');
- const [searching, setSearching] = useState(false);
- const loadUsers = async (startIdx) => {
- const res = await API.get(`/api/user/?p=${startIdx}`);
- const { success, message, data } = res.data;
- if (success) {
- if (startIdx === 0) {
- setUsers(data);
- } else {
- let newUsers = users;
- newUsers.push(...data);
- setUsers(newUsers);
- }
- } else {
- showError(message);
- }
- setLoading(false);
- };
- const onPaginationChange = (e, { activePage }) => {
- (async () => {
- if (activePage === Math.ceil(users.length / ITEMS_PER_PAGE) + 1) {
- // In this case we have to load more data and then append them.
- await loadUsers(activePage - 1);
- }
- setActivePage(activePage);
- })();
- };
- useEffect(() => {
- loadUsers(0)
- .then()
- .catch((reason) => {
- showError(reason);
- });
- }, []);
- const manageUser = (username, action, idx) => {
- (async () => {
- const res = await API.post('/api/user/manage', {
- username,
- action,
- });
- const { success, message } = res.data;
- if (success) {
- showSuccess('操作成功完成!');
- let user = res.data.data;
- let newUsers = [...users];
- let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx;
- if (action === 'delete') {
- newUsers[realIdx].deleted = true;
- } else {
- newUsers[realIdx].status = user.status;
- newUsers[realIdx].role = user.role;
- }
- setUsers(newUsers);
- } else {
- showError(message);
- }
- })();
- };
- const renderStatus = (status) => {
- switch (status) {
- case 1:
- return <Label basic>已激活</Label>;
- case 2:
- return (
- <Label basic color='red'>
- 已封禁
- </Label>
- );
- default:
- return (
- <Label basic color='grey'>
- 未知状态
- </Label>
- );
- }
- };
- const searchUsers = async () => {
- if (searchKeyword === '') {
- // if keyword is blank, load files instead.
- await loadUsers(0);
- setActivePage(1);
- return;
- }
- setSearching(true);
- const res = await API.get(`/api/user/search?keyword=${searchKeyword}`);
- const { success, message, data } = res.data;
- if (success) {
- setUsers(data);
- setActivePage(1);
- } else {
- showError(message);
- }
- setSearching(false);
- };
- const handleKeywordChange = async (e, { value }) => {
- setSearchKeyword(value.trim());
- };
- const sortUser = (key) => {
- if (users.length === 0) return;
- setLoading(true);
- let sortedUsers = [...users];
- sortedUsers.sort((a, b) => {
- return ('' + a[key]).localeCompare(b[key]);
- });
- if (sortedUsers[0].id === users[0].id) {
- sortedUsers.reverse();
- }
- setUsers(sortedUsers);
- setLoading(false);
- };
- return (
- <>
- <Form onSubmit={searchUsers}>
- <Form.Input
- icon='search'
- fluid
- iconPosition='left'
- placeholder='搜索用户的 ID,用户名,显示名称,以及邮箱地址 ...'
- value={searchKeyword}
- loading={searching}
- onChange={handleKeywordChange}
- />
- </Form>
- <Table basic>
- <Table.Header>
- <Table.Row>
- <Table.HeaderCell
- style={{ cursor: 'pointer' }}
- onClick={() => {
- sortUser('username');
- }}
- >
- 用户名
- </Table.HeaderCell>
- <Table.HeaderCell
- style={{ cursor: 'pointer' }}
- onClick={() => {
- sortUser('display_name');
- }}
- >
- 显示名称
- </Table.HeaderCell>
- <Table.HeaderCell
- style={{ cursor: 'pointer' }}
- onClick={() => {
- sortUser('email');
- }}
- >
- 邮箱地址
- </Table.HeaderCell>
- <Table.HeaderCell
- style={{ cursor: 'pointer' }}
- onClick={() => {
- sortUser('role');
- }}
- >
- 用户角色
- </Table.HeaderCell>
- <Table.HeaderCell
- style={{ cursor: 'pointer' }}
- onClick={() => {
- sortUser('status');
- }}
- >
- 状态
- </Table.HeaderCell>
- <Table.HeaderCell>操作</Table.HeaderCell>
- </Table.Row>
- </Table.Header>
- <Table.Body>
- {users
- .slice(
- (activePage - 1) * ITEMS_PER_PAGE,
- activePage * ITEMS_PER_PAGE
- )
- .map((user, idx) => {
- if (user.deleted) return <></>;
- return (
- <Table.Row key={user.id}>
- <Table.Cell>{user.username}</Table.Cell>
- <Table.Cell>{user.display_name}</Table.Cell>
- <Table.Cell>{user.email ? user.email : '无'}</Table.Cell>
- <Table.Cell>{renderRole(user.role)}</Table.Cell>
- <Table.Cell>{renderStatus(user.status)}</Table.Cell>
- <Table.Cell>
- <div>
- <Button
- size={'small'}
- positive
- onClick={() => {
- manageUser(user.username, 'promote', idx);
- }}
- >
- 提升
- </Button>
- <Button
- size={'small'}
- color={'yellow'}
- onClick={() => {
- manageUser(user.username, 'demote', idx);
- }}
- >
- 降级
- </Button>
- <Button
- size={'small'}
- negative
- onClick={() => {
- manageUser(user.username, 'delete', idx);
- }}
- >
- 删除
- </Button>
- <Button
- size={'small'}
- onClick={() => {
- manageUser(
- user.username,
- user.status === 1 ? 'disable' : 'enable',
- idx
- );
- }}
- >
- {user.status === 1 ? '禁用' : '启用'}
- </Button>
- <Button
- size={'small'}
- as={Link}
- to={'/user/edit/' + user.id}
- >
- 编辑
- </Button>
- </div>
- </Table.Cell>
- </Table.Row>
- );
- })}
- </Table.Body>
- <Table.Footer>
- <Table.Row>
- <Table.HeaderCell colSpan='6'>
- <Button size='small' as={Link} to='/user/add' loading={loading}>
- 添加新的用户
- </Button>
- <Pagination
- floated='right'
- activePage={activePage}
- onPageChange={onPaginationChange}
- size='small'
- siblingRange={1}
- totalPages={
- Math.ceil(users.length / ITEMS_PER_PAGE) +
- (users.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
- }
- />
- </Table.HeaderCell>
- </Table.Row>
- </Table.Footer>
- </Table>
- </>
- );
- };
- export default ChannelsTable;
|