|
| 1 | +import { |
| 2 | + ActionType, |
| 3 | + ModalForm, |
| 4 | + PageContainer, |
| 5 | + ProColumns, |
| 6 | + ProFormGroup, |
| 7 | + ProFormText, |
| 8 | + ProTable |
| 9 | +} from '@ant-design/pro-components'; |
| 10 | +import React, {useRef, useState} from 'react'; |
| 11 | +import {useDomainName} from "@/hooks"; |
| 12 | +import { |
| 13 | + createDomainBrickapiV1DomainsDomainPost, |
| 14 | + initDomainBrickapiV1DomainsDomainInitGet, |
| 15 | + listDomainsBrickapiV1DomainsGet, |
| 16 | +} from "@/services/brick-server-playground/domains"; |
| 17 | +import {Button, message, Popconfirm} from "antd"; |
| 18 | +import {PlusOutlined} from "@ant-design/icons"; |
| 19 | + |
| 20 | +const DomainList: React.FC = () => { |
| 21 | + // const domainName = useDomainName(); |
| 22 | + const actionRef = useRef<ActionType>(); |
| 23 | + const [isAddDomainOpen, setIsAddDomainOpen] = useState<boolean>(false); |
| 24 | + const [currentDomain, setCurrentDomain] = useState<API.ResourceConstraintRead | undefined>( |
| 25 | + undefined, |
| 26 | + ); |
| 27 | + |
| 28 | + const onClickAddResource = async () => { |
| 29 | + setCurrentDomain(undefined); |
| 30 | + setIsAddDomainOpen(true); |
| 31 | + }; |
| 32 | + |
| 33 | + const onClickInitDomain = async(domain: API.DomainRead) => { |
| 34 | + const result = await initDomainBrickapiV1DomainsDomainInitGet({domain: domain.name}); |
| 35 | + if (result.errorCode !== 'Success') { |
| 36 | + message.error(`Error: ${result.errorCode}`); |
| 37 | + } else { |
| 38 | + message.success(`Domain ${domain.name} initialized!`); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const onFinishAddDomain = async (values: { name: string; }) => { |
| 43 | + console.log(values); |
| 44 | + const result = await createDomainBrickapiV1DomainsDomainPost( |
| 45 | + { domain: values.name }, |
| 46 | + ); |
| 47 | + if (result.errorCode !== 'Success') { |
| 48 | + message.error(`Error: ${result.errorCode}`); |
| 49 | + } |
| 50 | + await actionRef.current?.reload(); |
| 51 | + setCurrentDomain(undefined); |
| 52 | + setIsAddDomainOpen(false); |
| 53 | + }; |
| 54 | + |
| 55 | + const onCancelAddResource = async () => { |
| 56 | + setIsAddDomainOpen(false); |
| 57 | + }; |
| 58 | + |
| 59 | + |
| 60 | + const columns: ProColumns<API.DomainRead>[] = [ |
| 61 | + { |
| 62 | + title: 'Name', |
| 63 | + dataIndex: 'name', |
| 64 | + }, |
| 65 | + { |
| 66 | + title: 'Operations', |
| 67 | + valueType: 'option', |
| 68 | + render: (text, record, _, action) => [ |
| 69 | + <a key="add_profile" onClick={() => onClickInitDomain(record)}> |
| 70 | + Init |
| 71 | + </a> |
| 72 | + ], |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + return ( |
| 77 | + <PageContainer> |
| 78 | + <ProTable<API.DomainRead> |
| 79 | + actionRef={actionRef} |
| 80 | + columns={columns} |
| 81 | + pagination={false} |
| 82 | + search={false} |
| 83 | + request={async (params, sort, filter) => { |
| 84 | + const result = await listDomainsBrickapiV1DomainsGet(); |
| 85 | + return { |
| 86 | + data: result.data?.results || [], |
| 87 | + success: true, |
| 88 | + total: result.data?.count || 0, |
| 89 | + }; |
| 90 | + }} |
| 91 | + toolBarRender={() => [ |
| 92 | + <Button key="add" type="primary" icon={<PlusOutlined />} onClick={onClickAddResource}> |
| 93 | + Create Domain |
| 94 | + </Button>, |
| 95 | + ]} |
| 96 | + /> |
| 97 | + <ModalForm |
| 98 | + title={'Create Domain'} |
| 99 | + open={isAddDomainOpen} |
| 100 | + onFinish={onFinishAddDomain} |
| 101 | + modalProps={{ |
| 102 | + destroyOnClose: true, |
| 103 | + onCancel: onCancelAddResource, |
| 104 | + }} |
| 105 | + > |
| 106 | + <ProFormGroup> |
| 107 | + <ProFormText |
| 108 | + label="Name" |
| 109 | + name="name" |
| 110 | + width="md" |
| 111 | + rules={[ |
| 112 | + { |
| 113 | + required: true, |
| 114 | + message: 'Please enter domain name.', |
| 115 | + }, |
| 116 | + ]} |
| 117 | + /> |
| 118 | + </ProFormGroup> |
| 119 | + </ModalForm> |
| 120 | + </PageContainer> |
| 121 | + ); |
| 122 | +}; |
| 123 | +export default DomainList; |
0 commit comments