import React, { useEffect, useState } from 'react'; import { Button, Form, Label, Message, Pagination, Table } from 'semantic-ui-react'; import { Link } from 'react-router-dom'; import { API, copy, showError, showInfo, showSuccess, showWarning, timestamp2string } from '../helpers'; import { ITEMS_PER_PAGE } from '../constants'; function renderTimestamp(timestamp) { return ( <> {timestamp2string(timestamp)} > ); } function renderStatus(status) { switch (status) { case 1: return ; case 2: return ; case 3: return ; case 4: return ; default: return ; } } const TokensTable = () => { const [tokens, setTokens] = useState([]); const [loading, setLoading] = useState(true); const [activePage, setActivePage] = useState(1); const [searchKeyword, setSearchKeyword] = useState(''); const [searching, setSearching] = useState(false); const loadTokens = async (startIdx) => { const res = await API.get(`/api/token/?p=${startIdx}`); const { success, message, data } = res.data; if (success) { if (startIdx === 0) { setTokens(data); } else { let newTokens = tokens; newTokens.push(...data); setTokens(newTokens); } } else { showError(message); } setLoading(false); }; const onPaginationChange = (e, { activePage }) => { (async () => { if (activePage === Math.ceil(tokens.length / ITEMS_PER_PAGE) + 1) { // In this case we have to load more data and then append them. await loadTokens(activePage - 1); } setActivePage(activePage); })(); }; useEffect(() => { loadTokens(0) .then() .catch((reason) => { showError(reason); }); }, []); const manageToken = async (id, action, idx) => { let data = { id }; let res; switch (action) { case 'delete': res = await API.delete(`/api/token/${id}/`); break; case 'enable': data.status = 1; res = await API.put('/api/token/', data); break; case 'disable': data.status = 2; res = await API.put('/api/token/', data); break; } const { success, message } = res.data; if (success) { showSuccess('操作成功完成!'); let token = res.data.data; let newTokens = [...tokens]; let realIdx = (activePage - 1) * ITEMS_PER_PAGE + idx; if (action === 'delete') { newTokens[realIdx].deleted = true; } else { newTokens[realIdx].status = token.status; } setTokens(newTokens); } else { showError(message); } }; const searchTokens = async () => { if (searchKeyword === '') { // if keyword is blank, load files instead. await loadTokens(0); setActivePage(1); return; } setSearching(true); const res = await API.get(`/api/token/search?keyword=${searchKeyword}`); const { success, message, data } = res.data; if (success) { setTokens(data); setActivePage(1); } else { showError(message); } setSearching(false); }; const handleKeywordChange = async (e, { value }) => { setSearchKeyword(value.trim()); }; const sortToken = (key) => { if (tokens.length === 0) return; setLoading(true); let sortedTokens = [...tokens]; sortedTokens.sort((a, b) => { return ('' + a[key]).localeCompare(b[key]); }); if (sortedTokens[0].id === tokens[0].id) { sortedTokens.reverse(); } setTokens(sortedTokens); setLoading(false); }; return ( <>