|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import React, { useRef, useState } from 'react'; |
| 18 | +import { PageHeaderWrapper } from '@ant-design/pro-layout'; |
| 19 | +import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'; |
| 20 | +import ProtoDrawer from './components/ProtoDrawer'; |
| 21 | +import { Button, notification, Popconfirm, Space } from 'antd'; |
| 22 | +import { useIntl } from 'umi'; |
| 23 | +import usePagination from '@/hooks/usePagination'; |
| 24 | + |
| 25 | +import { PlusOutlined } from '@ant-design/icons'; |
| 26 | + |
| 27 | +import { timestampToLocaleString } from '@/helpers'; |
| 28 | +import { fetchList, remove } from './service'; |
| 29 | + |
| 30 | +const Page: React.FC = () => { |
| 31 | + const ref = useRef<ActionType>(); |
| 32 | + const { formatMessage } = useIntl(); |
| 33 | + const [drawerVisible, setDrawerVisible] = useState(false); |
| 34 | + const { paginationConfig, savePageList } = usePagination(); |
| 35 | + const emptyProtoData = { |
| 36 | + id: null, |
| 37 | + content: '', |
| 38 | + desc: '', |
| 39 | + }; |
| 40 | + const [protoData, setProtoData] = useState<ProtoModule.ProtoData>(emptyProtoData); |
| 41 | + const [editMode, setEditMode] = useState<ProtoModule.EditMode>('create'); |
| 42 | + |
| 43 | + const refreshTable = () => { |
| 44 | + ref.current?.reload(); |
| 45 | + }; |
| 46 | + |
| 47 | + const showDrawer = (data: ProtoModule.ProtoData, mode: ProtoModule.EditMode) => { |
| 48 | + setDrawerVisible(true); |
| 49 | + setProtoData(data); |
| 50 | + setEditMode(mode); |
| 51 | + }; |
| 52 | + |
| 53 | + const columns: ProColumns<ProtoModule.ResponseBody>[] = [ |
| 54 | + { |
| 55 | + title: formatMessage({ id: 'component.global.id' }), |
| 56 | + hideInSearch: true, |
| 57 | + dataIndex: 'id', |
| 58 | + width: 100, |
| 59 | + }, |
| 60 | + { |
| 61 | + title: formatMessage({ id: 'page.proto.desc' }), |
| 62 | + dataIndex: 'desc', |
| 63 | + ellipsis: true, |
| 64 | + width: 200, |
| 65 | + }, |
| 66 | + { |
| 67 | + title: formatMessage({ id: 'page.proto.content' }), |
| 68 | + hideInSearch: true, |
| 69 | + dataIndex: 'content', |
| 70 | + ellipsis: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + title: formatMessage({ id: 'component.global.updateTime' }), |
| 74 | + dataIndex: 'update_time', |
| 75 | + hideInSearch: true, |
| 76 | + render: (text) => timestampToLocaleString(text as number), |
| 77 | + width: 200, |
| 78 | + }, |
| 79 | + { |
| 80 | + title: formatMessage({ id: 'component.global.operation' }), |
| 81 | + valueType: 'option', |
| 82 | + fixed: 'right', |
| 83 | + hideInSearch: true, |
| 84 | + render: (_, record) => ( |
| 85 | + <Space align="baseline"> |
| 86 | + <Button |
| 87 | + type="primary" |
| 88 | + onClick={() => |
| 89 | + showDrawer({ id: record.id, content: record.content, desc: record.desc }, 'update') |
| 90 | + } |
| 91 | + > |
| 92 | + {formatMessage({ id: 'component.global.edit' })} |
| 93 | + </Button> |
| 94 | + <Popconfirm |
| 95 | + title={formatMessage({ id: 'page.upstream.list.confirm.delete' })} |
| 96 | + okText={formatMessage({ id: 'page.upstream.list.confirm' })} |
| 97 | + cancelText={formatMessage({ id: 'page.upstream.list.cancel' })} |
| 98 | + onConfirm={() => { |
| 99 | + remove(record.id).then(() => { |
| 100 | + notification.success({ |
| 101 | + message: formatMessage({ id: 'page.upstream.list.delete.successfully' }), |
| 102 | + }); |
| 103 | + /* eslint-disable no-unused-expressions */ |
| 104 | + ref.current?.reload(); |
| 105 | + }); |
| 106 | + }} |
| 107 | + > |
| 108 | + <Button type="primary" danger> |
| 109 | + {formatMessage({ id: 'page.upstream.list.delete' })} |
| 110 | + </Button> |
| 111 | + </Popconfirm> |
| 112 | + </Space> |
| 113 | + ), |
| 114 | + }, |
| 115 | + ]; |
| 116 | + |
| 117 | + return ( |
| 118 | + <PageHeaderWrapper |
| 119 | + title={formatMessage({ id: 'page.proto.list' })} |
| 120 | + content={formatMessage({ id: 'page.proto.list.description' })} |
| 121 | + > |
| 122 | + <ProtoDrawer |
| 123 | + protoData={protoData as ProtoModule.ProtoData} |
| 124 | + setProtoData={setProtoData} |
| 125 | + visible={drawerVisible} |
| 126 | + setVisible={setDrawerVisible} |
| 127 | + editMode={editMode} |
| 128 | + refreshTable={refreshTable} |
| 129 | + /> |
| 130 | + <ProTable<ProtoModule.ResponseBody> |
| 131 | + actionRef={ref} |
| 132 | + rowKey="id" |
| 133 | + columns={columns} |
| 134 | + request={fetchList} |
| 135 | + pagination={{ |
| 136 | + onChange: (page, pageSize?) => savePageList(page, pageSize), |
| 137 | + pageSize: paginationConfig.pageSize, |
| 138 | + current: paginationConfig.current, |
| 139 | + }} |
| 140 | + search={{ |
| 141 | + searchText: formatMessage({ id: 'component.global.search' }), |
| 142 | + resetText: formatMessage({ id: 'component.global.reset' }), |
| 143 | + }} |
| 144 | + toolBarRender={() => [ |
| 145 | + <Button type="primary" onClick={() => showDrawer(emptyProtoData, 'create')}> |
| 146 | + <PlusOutlined /> |
| 147 | + {formatMessage({ id: 'component.global.create' })} |
| 148 | + </Button>, |
| 149 | + ]} |
| 150 | + tableAlertRender={false} |
| 151 | + scroll={{ x: 1300 }} |
| 152 | + /> |
| 153 | + </PageHeaderWrapper> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export default Page; |
0 commit comments