|
| 1 | +import { FormattedMessage, useModel, useParams, useIntl, history } from '@umijs/max'; |
| 2 | +import { |
| 3 | + App, |
| 4 | + Pagination, |
| 5 | + Card, |
| 6 | + Upload, |
| 7 | + UploadProps, |
| 8 | +} from 'antd'; |
| 9 | +import _ from 'lodash'; |
| 10 | +import { useEffect, useState } from 'react'; |
| 11 | +import AddUrl from './addUrl'; |
| 12 | +import Documentlist from '@/components/DocumentTable'; |
| 13 | + |
| 14 | +export default () => { |
| 15 | + const { user } = useModel('user'); |
| 16 | + const { collectionId, page } = useParams(); |
| 17 | + const [searchKey, setSearchKey] = useState<string>(''); |
| 18 | + const [pageSize, setPageSize] = useState<number>(10); |
| 19 | + const [pageNumber, setPageNumber] = useState<number>(1); |
| 20 | + const { modal, message } = App.useApp(); |
| 21 | + const { collections, getCollection } = useModel('collection'); |
| 22 | + const { documents, documentLoading, totalCount,getDocuments,deleteDocument } = useModel('document'); |
| 23 | + const [uploading, setUploading] = useState<boolean>(false); |
| 24 | + const [ collection, setCollection ] = useState<any>(); |
| 25 | + const [ readonly, setReadonly ] = useState(false); |
| 26 | + const config = collection?.config; |
| 27 | + |
| 28 | + useEffect(()=>{ |
| 29 | + const collectionDetail = getCollection(collectionId); |
| 30 | + setCollection(collectionDetail); |
| 31 | + setReadonly(collectionDetail?.system && !user?.is_admin); |
| 32 | + }, [collections]) |
| 33 | + |
| 34 | + const dataSource = documents?.filter((c) => { |
| 35 | + return c.name?.match(new RegExp(searchKey, 'i')) || _.isEmpty(searchKey); |
| 36 | + }); |
| 37 | + |
| 38 | + const { formatMessage } = useIntl(); |
| 39 | + |
| 40 | + const getDocument = async (page, page_size) => { |
| 41 | + const pageId = page || pageNumber; |
| 42 | + const page_Size = page_size || pageSize; |
| 43 | + getDocuments(String(collectionId), pageId, page_Size); |
| 44 | + setPageNumber(pageId); |
| 45 | + }; |
| 46 | + |
| 47 | + const changePage = (page, pageSize) => { |
| 48 | + setPageSize(pageSize); |
| 49 | + // getDocument(page, pageSize); |
| 50 | + history.replace(`/collections/${collectionId}/documents/${page}`); |
| 51 | + }; |
| 52 | + |
| 53 | + const onDelete = (record) => { |
| 54 | + if (!collectionId) return; |
| 55 | + |
| 56 | + let msg = []; |
| 57 | + |
| 58 | + if(!_.isArray(record)){ |
| 59 | + record = [record]; |
| 60 | + } |
| 61 | + |
| 62 | + record.map((item, idx)=>{ |
| 63 | + msg.push((idx+1) + '. ' + item.name); |
| 64 | + }); |
| 65 | + |
| 66 | + msg = msg.join('<br/>'); |
| 67 | + |
| 68 | + modal.confirm({ |
| 69 | + title: formatMessage({ id: 'text.delete.help' }), |
| 70 | + content: <>{formatMessage({ id: 'text.documents' })}: <div dangerouslySetInnerHTML={{ __html: msg }} /><br/>{formatMessage({ id: 'text.delete.confirm' })}</>, |
| 71 | + onOk: async () => { |
| 72 | + const items = []; |
| 73 | + record.map((item)=>items.push(item.id)); |
| 74 | + await deleteDocument(collectionId, items); |
| 75 | + setTimeout(() => { |
| 76 | + getDocument(); |
| 77 | + }); |
| 78 | + }, |
| 79 | + okButtonProps: { |
| 80 | + danger: true, |
| 81 | + }, |
| 82 | + }); |
| 83 | + }; |
| 84 | + |
| 85 | + const SUPPORTED_DOC_EXTENSIONS=['.pdf','.doc','.docx','.ppt','.pptx','.csv','.xls','.xlsx','.epub','.md','.mbox','.ipynb','.txt','.htm','.html']; |
| 86 | + const SUPPORTED_MEDIA_EXTENSIONS=['.jpg','.jpeg','.png','.webm','.mp3','.mp4','.mpeg','.mpga','.m4a','.wav']; |
| 87 | + const SUPPORTED_COMPRESSED_EXTENSIONS=['.zip','.rar', '.r00','.7z','.tar', '.gz', '.xz', '.bz2', '.tar.gz', '.tar.xz', '.tar.bz2', '.tar.7z']; |
| 88 | + |
| 89 | + const uploadProps: UploadProps = { |
| 90 | + name: 'file', |
| 91 | + multiple: true, |
| 92 | + disabled: readonly, |
| 93 | + action: `/api/v1/collections/${collectionId}/documents`, |
| 94 | + data: {}, |
| 95 | + showUploadList: false, |
| 96 | + headers: { |
| 97 | + Authorization: 'Bearer ' + user?.__raw, |
| 98 | + }, |
| 99 | + accept: SUPPORTED_DOC_EXTENSIONS.concat(SUPPORTED_MEDIA_EXTENSIONS).concat(SUPPORTED_COMPRESSED_EXTENSIONS).join(','), |
| 100 | + onChange(info) { |
| 101 | + const { status, response } = info.file; |
| 102 | + if (status === 'done') { |
| 103 | + if (response.code === '200') { |
| 104 | + getDocument(); |
| 105 | + } else { |
| 106 | + message.error(response.message || 'update error'); |
| 107 | + } |
| 108 | + setUploading(false); |
| 109 | + } else { |
| 110 | + setUploading(true); |
| 111 | + if (status === 'error') { |
| 112 | + message.error('upload error'); |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + useEffect(() => { |
| 119 | + getDocument(parseInt(page)); |
| 120 | + }, [history.location.pathname]); |
| 121 | + |
| 122 | + return ( |
| 123 | + <div className="border-block document"> |
| 124 | + <Card bordered={false}> |
| 125 | + <div className='hd'> |
| 126 | + <div className='title'> |
| 127 | + |
| 128 | + </div> |
| 129 | + <div className="action"> |
| 130 | + <label className="search"> |
| 131 | + <input |
| 132 | + type="text" |
| 133 | + onChange={(e) => setSearchKey(e.currentTarget.value)} |
| 134 | + /> |
| 135 | + </label> |
| 136 | + {!readonly && config?.source === 'system' ? ( |
| 137 | + <Upload {...uploadProps} disabled={(readonly || uploading)}> |
| 138 | + {/* eslint-disable-next-line react/button-has-type */} |
| 139 | + <button disabled={readonly} className={uploading ? 'waiting' : ''}> |
| 140 | + <FormattedMessage id="action.documents.add" /> |
| 141 | + </button> |
| 142 | + </Upload> |
| 143 | + ) : null} |
| 144 | + {config?.source === 'url' ? ( |
| 145 | + <AddUrl onSuccess={() => getDocument()} collection={collection} /> |
| 146 | + ) : null} |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + <div className='bd'> |
| 150 | + <Documentlist list={dataSource} getDocuments={()=> getDocument()} onDelete={onDelete} loading={documentLoading} selection={!readonly && totalCount} editable={!readonly}/> |
| 151 | + {totalCount ? ( |
| 152 | + <div className='pagination'> |
| 153 | + <div className='wrap'> |
| 154 | + <Pagination |
| 155 | + simple |
| 156 | + total={totalCount} |
| 157 | + pageSize={pageSize} |
| 158 | + current={pageNumber} |
| 159 | + showQuickJumper |
| 160 | + showSizeChanger |
| 161 | + onChange={changePage} |
| 162 | + hideOnSinglePage={true} |
| 163 | + responsive={true} |
| 164 | + showTotal={(totalItems) => `${totalItems}${formatMessage({id:'text.items'})}`} |
| 165 | + /> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + ):''} |
| 169 | + </div> |
| 170 | + </Card> |
| 171 | + </div> |
| 172 | + ); |
| 173 | +}; |
0 commit comments