|
| 1 | +import React, { FC, memo, useEffect, useState } from 'react'; |
| 2 | +import { Draft, produce } from "immer"; |
| 3 | +import { ActionURL } from "@labkey/api"; |
| 4 | +import { LoadingSpinner } from '@labkey/components'; |
| 5 | + |
| 6 | +import { MY_ATTACHMENTS_DIR } from "./constants"; |
| 7 | +import { FileAttachmentModel, SavedFileModel } from "./models"; |
| 8 | +import { getUploadedFiles } from "./actions"; |
| 9 | + |
| 10 | +export const FileDisplayPanel : FC = memo(() => { |
| 11 | + const [loading, setLoading] = useState<boolean>(true); |
| 12 | + const [fileAttachmentModel, setFileAttachmentModel] = useState<FileAttachmentModel>(new FileAttachmentModel()); |
| 13 | + |
| 14 | + //equivalent to componentDidMount and componentDidUpdate |
| 15 | + useEffect(() => { |
| 16 | + getUploadedFiles(ActionURL.getContainer(), MY_ATTACHMENTS_DIR, false) |
| 17 | + .then((files:Array<SavedFileModel>) => { |
| 18 | + if (files?.length > 0) { |
| 19 | + const updatedModel = produce(fileAttachmentModel, (draft: Draft<FileAttachmentModel>) => { |
| 20 | + draft['savedFiles'] = files; |
| 21 | + }); |
| 22 | + setFileAttachmentModel(updatedModel); |
| 23 | + } |
| 24 | + else { |
| 25 | + setFileAttachmentModel(new FileAttachmentModel()); |
| 26 | + } |
| 27 | + |
| 28 | + setLoading(false); |
| 29 | + }); |
| 30 | + }, []); |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className='panel panel-default'> |
| 34 | + <div className='panel-heading'> |
| 35 | + My Uploaded Attachments |
| 36 | + </div> |
| 37 | + <div className='panel-body'> |
| 38 | + {loading && <LoadingSpinner />} |
| 39 | + { |
| 40 | + !loading && fileAttachmentModel.savedFiles?.length > 0 && ( |
| 41 | + <ul> |
| 42 | + { |
| 43 | + fileAttachmentModel?.savedFiles?.map((savedFile) => ( |
| 44 | + <li key={savedFile.fileName}> |
| 45 | + <a href={savedFile.href} target='_blank'>{savedFile.fileName}</a> |
| 46 | + </li> |
| 47 | + )) |
| 48 | + } |
| 49 | + </ul> |
| 50 | + ) |
| 51 | + } |
| 52 | + { |
| 53 | + !loading && !fileAttachmentModel.savedFiles && ( |
| 54 | + <p> |
| 55 | + No files to display. Use the panel above to upload files to this location. |
| 56 | + </p> |
| 57 | + ) |
| 58 | + } |
| 59 | + {!loading && ( |
| 60 | + <p> |
| 61 | + <a className='labkey-text-link' href={ActionURL.buildURL('filecontent', 'begin', undefined, { |
| 62 | + path: MY_ATTACHMENTS_DIR |
| 63 | + })}> |
| 64 | + Manage Files |
| 65 | + </a> |
| 66 | + </p> |
| 67 | + )} |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +}) |
0 commit comments