|
| 1 | +import { ChangeEvent, useState } from 'react' |
| 2 | +import { |
| 3 | + UploadOutlined, |
| 4 | + ImportOutlined, |
| 5 | + LoadingOutlined, |
| 6 | +} from '@ant-design/icons' |
| 7 | +import { |
| 8 | + Upload, |
| 9 | + UploadProps, |
| 10 | + Button, |
| 11 | + Modal, |
| 12 | + Input, |
| 13 | + Row, |
| 14 | + Col, |
| 15 | + Divider, |
| 16 | +} from 'antd' |
| 17 | +import { createResource } from '@/api' |
| 18 | +import { addKey } from '@/utils' |
| 19 | +import { useQueryClient } from '@tanstack/react-query' |
| 20 | +import { Link } from 'react-router-dom' |
| 21 | + |
| 22 | +const baseUrl = process.env.BASE_URL || '' |
| 23 | + |
| 24 | +const JsonUpload = () => { |
| 25 | + const [ |
| 26 | + isLoading, |
| 27 | + setIsLoading, |
| 28 | + ] = useState(false) |
| 29 | + const [ |
| 30 | + showLink, |
| 31 | + setShowLink, |
| 32 | + ] = useState(false) |
| 33 | + |
| 34 | + const [ |
| 35 | + theProjectId, |
| 36 | + setTheProjectId, |
| 37 | + ] = useState<number>(0) |
| 38 | + |
| 39 | + const [ |
| 40 | + jsonContent, |
| 41 | + setJsonContent, |
| 42 | + ] = useState<string>('') |
| 43 | + |
| 44 | + const [ |
| 45 | + isJsonUploadModalOpen, |
| 46 | + setIsJsonUploadModalOpen, |
| 47 | + ] = useState(false) |
| 48 | + |
| 49 | + const queryClient = useQueryClient() |
| 50 | + |
| 51 | + const showJsonUploadModal = () => { |
| 52 | + setIsJsonUploadModalOpen(true) |
| 53 | + } |
| 54 | + |
| 55 | + const handleJsonUploadOk = () => { |
| 56 | + setIsJsonUploadModalOpen(false) |
| 57 | + } |
| 58 | + |
| 59 | + const handleJsonUploadCancel = () => { |
| 60 | + setIsJsonUploadModalOpen(false) |
| 61 | + setShowLink(false) |
| 62 | + } |
| 63 | + |
| 64 | + const props: UploadProps = { |
| 65 | + accept: '.json', |
| 66 | + name: 'file', |
| 67 | + multiple: false, |
| 68 | + |
| 69 | + onChange({ file }) { |
| 70 | + setIsLoading(true) |
| 71 | + |
| 72 | + const reader = new FileReader() |
| 73 | + reader.onload = (event) => { |
| 74 | + const content = event?.target?.result || '{}' |
| 75 | + setJsonContent(content as string) |
| 76 | + // setJsonContent(JSON.parse(content as string)) |
| 77 | + setIsLoading(false) |
| 78 | + } |
| 79 | + if (file?.originFileObj) { |
| 80 | + reader.readAsText(file.originFileObj) |
| 81 | + } |
| 82 | + }, |
| 83 | + onDrop(e) { |
| 84 | + console.log('Dropped files', e.dataTransfer.files) |
| 85 | + }, |
| 86 | + maxCount: 1, |
| 87 | + showUploadList: false, |
| 88 | + className: 'block w-full', |
| 89 | + } |
| 90 | + |
| 91 | + const handleImport = async () => { |
| 92 | + //add key & groupKey back |
| 93 | + const scopesWithoutKey = JSON.parse(jsonContent || '{}') |
| 94 | + const scopes = addKey(scopesWithoutKey) |
| 95 | + |
| 96 | + const createResult = await createResource({ |
| 97 | + resource: 'carbon-project', |
| 98 | + args: { |
| 99 | + title: scopes?.info?.title || '○○○○股份有限公司', |
| 100 | + content: scopes?.info?.content || '', |
| 101 | + status: 'publish', |
| 102 | + featured_media: scopes?.info?.imgData?.attachmentId || 0, |
| 103 | + meta: { |
| 104 | + project_data: JSON.stringify(scopes || {}), |
| 105 | + }, |
| 106 | + }, |
| 107 | + }) |
| 108 | + if (createResult?.status === 201 || createResult?.status === 200) { |
| 109 | + queryClient.invalidateQueries([ |
| 110 | + 'get_carbon-projects', |
| 111 | + ]) |
| 112 | + |
| 113 | + setShowLink(true) |
| 114 | + setTheProjectId(createResult?.data?.id || 0) |
| 115 | + } else { |
| 116 | + console.log('createResult', createResult) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const handleTextAreaChange = (e: ChangeEvent<any>) => { |
| 121 | + const text = e?.target?.value || '' |
| 122 | + setJsonContent(text) |
| 123 | + } |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <Button icon={<UploadOutlined />} onClick={showJsonUploadModal}> |
| 128 | + 使用 JSON 檔案上傳 |
| 129 | + </Button> |
| 130 | + <Modal |
| 131 | + title="匯入專案 JSON 數據" |
| 132 | + centered |
| 133 | + open={isJsonUploadModalOpen} |
| 134 | + onOk={handleJsonUploadOk} |
| 135 | + onCancel={handleJsonUploadCancel} |
| 136 | + footer={null} |
| 137 | + > |
| 138 | + <Input.TextArea |
| 139 | + disabled={isLoading} |
| 140 | + value={jsonContent} |
| 141 | + rows={6} |
| 142 | + onChange={handleTextAreaChange} |
| 143 | + /> |
| 144 | + <Row gutter={24} className="mt-4 json-upload"> |
| 145 | + <Col span={12}> |
| 146 | + <Upload {...props}> |
| 147 | + <Button |
| 148 | + className="w-full" |
| 149 | + icon={isLoading ? <LoadingOutlined /> : <UploadOutlined />} |
| 150 | + > |
| 151 | + 使用 JSON 檔案上傳 |
| 152 | + </Button> |
| 153 | + </Upload> |
| 154 | + </Col> |
| 155 | + <Col span={12}> |
| 156 | + <Button |
| 157 | + type="primary" |
| 158 | + className="w-full" |
| 159 | + onClick={handleImport} |
| 160 | + icon={<ImportOutlined />} |
| 161 | + > |
| 162 | + 確認匯入 JSON |
| 163 | + </Button> |
| 164 | + </Col> |
| 165 | + {showLink && ( |
| 166 | + <Col span={24} className="mt-4"> |
| 167 | + <Divider plain>已成功創建專案</Divider> |
| 168 | + <Link to={`${baseUrl}check`} state={{ id: theProjectId }}> |
| 169 | + <Button type="primary" className="w-full"> |
| 170 | + 立即查看專案 |
| 171 | + </Button> |
| 172 | + </Link> |
| 173 | + </Col> |
| 174 | + )} |
| 175 | + </Row> |
| 176 | + </Modal> |
| 177 | + </> |
| 178 | + ) |
| 179 | +} |
| 180 | + |
| 181 | +export default JsonUpload |
0 commit comments