// Copyright 2021 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from "react"; import {Button, Popconfirm, Table, Upload} from 'antd'; import {UploadOutlined} from "@ant-design/icons"; import copy from 'copy-to-clipboard'; import * as Setting from "./Setting"; import * as ResourceBackend from "./backend/ResourceBackend"; import i18next from "i18next"; import {Link} from "react-router-dom"; import BaseListPage from "./BaseListPage"; class ResourceListPage extends BaseListPage { constructor(props) { super(props); this.state = { classes: props, data: [], pagination: { current: 1, pageSize: 10, }, loading: false, searchText: '', searchedColumn: '', fileList: [], uploading: false, }; } deleteResource(i) { ResourceBackend.deleteResource(this.state.data[i]) .then((res) => { Setting.showMessage("success", `Resource deleted successfully`); this.setState({ data: Setting.deleteRow(this.state.data, i), pagination: {total: this.state.pagination.total - 1}, }); } ) .catch(error => { Setting.showMessage("error", `Resource failed to delete: ${error}`); }); } handleUpload(info) { this.setState({uploading: true}); const filename = info.fileList[0].name; const fullFilePath = `resource/${this.props.account.owner}/${this.props.account.name}/${filename}`; ResourceBackend.uploadResource(this.props.account.owner, this.props.account.name, "custom", "ResourceListPage", fullFilePath, info.file) .then(res => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("application:File uploaded successfully")); window.location.reload(); } else { Setting.showMessage("error", res.msg); } }).finally(() => { this.setState({uploading: false}); }) } renderUpload() { return ( {return false}} onChange={info => {this.handleUpload(info)}}> ) } renderTable(resources) { const columns = [ { title: i18next.t("general:Provider"), dataIndex: 'provider', key: 'provider', width: '150px', fixed: 'left', sorter: true, ...this.getColumnSearchProps('provider'), render: (text, record, index) => { return ( {text} ) } }, { title: i18next.t("resource:Application"), dataIndex: 'application', key: 'application', width: '80px', sorter: true, ...this.getColumnSearchProps('application'), render: (text, record, index) => { return ( {text} ) } }, { title: i18next.t("resource:User"), dataIndex: 'user', key: 'user', width: '80px', sorter: true, ...this.getColumnSearchProps('user'), render: (text, record, index) => { return ( {text} ) } }, { title: i18next.t("resource:Parent"), dataIndex: 'parent', key: 'parent', width: '80px', sorter: true, ...this.getColumnSearchProps('parent'), }, { title: i18next.t("general:Name"), dataIndex: 'name', key: 'name', width: '150px', sorter: true, ...this.getColumnSearchProps('name'), }, { title: i18next.t("general:Created time"), dataIndex: 'createdTime', key: 'createdTime', width: '150px', sorter: true, render: (text, record, index) => { return Setting.getFormattedDate(text); } }, { title: i18next.t("resource:Tag"), dataIndex: 'tag', key: 'tag', width: '80px', sorter: true, ...this.getColumnSearchProps('tag'), }, // { // title: i18next.t("resource:File name"), // dataIndex: 'fileName', // key: 'fileName', // width: '120px', // sorter: (a, b) => a.fileName.localeCompare(b.fileName), // }, { title: i18next.t("resource:Type"), dataIndex: 'fileType', key: 'fileType', width: '80px', sorter: true, ...this.getColumnSearchProps('fileType'), }, { title: i18next.t("resource:Format"), dataIndex: 'fileFormat', key: 'fileFormat', width: '80px', sorter: true, ...this.getColumnSearchProps('fileFormat'), }, { title: i18next.t("resource:File size"), dataIndex: 'fileSize', key: 'fileSize', width: '100px', sorter: true, render: (text, record, index) => { return Setting.getFriendlyFileSize(text); } }, { title: i18next.t("general:Preview"), dataIndex: 'preview', key: 'preview', width: '100px', render: (text, record, index) => { if (record.fileType === "image") { return ( {record.name} ) } else if (record.fileType === "video") { return (
) } } }, { title: i18next.t("general:URL"), dataIndex: 'url', key: 'url', width: '120px', render: (text, record, index) => { return (
) } }, { title: i18next.t("general:Action"), dataIndex: '', key: 'op', width: '70px', fixed: (Setting.isMobile()) ? "false" : "right", render: (text, record, index) => { return (
{/**/} this.deleteResource(index)} okText={i18next.t("user:OK")} cancelText={i18next.t("user:Cancel")} >
) } }, ]; const paginationProps = { total: this.state.pagination.total, showQuickJumper: true, showSizeChanger: true, showTotal: () => i18next.t("general:{total} in total").replace("{total}", this.state.pagination.total), }; return (
(
{i18next.t("general:Resources")}     {/**/} { this.renderUpload() }
)} loading={this.state.loading} onChange={this.handleTableChange} /> ); } fetch = (params = {}) => { let field = params.searchedColumn, value = params.searchText; let sortField = params.sortField, sortOrder = params.sortOrder; this.setState({ loading: true }); ResourceBackend.getResources(this.props.account.owner, this.props.account.name, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder) .then((res) => { if (res.status === "ok") { this.setState({ loading: false, data: res.data, pagination: { ...params.pagination, total: res.data2, }, searchText: params.searchText, searchedColumn: params.searchedColumn, }); } }); }; } export default ResourceListPage;