-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileDisplayPanel.tsx
More file actions
142 lines (127 loc) · 5.9 KB
/
FileDisplayPanel.tsx
File metadata and controls
142 lines (127 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2023-2026 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/
import React, { FC, memo, useCallback, useEffect, useState } from 'react';
import { Draft, produce } from "immer";
import { ActionURL } from '@labkey/api';
import { createWebDavDirectory, deleteWebDavResource, LoadingSpinner, Modal } from '@labkey/components';
import { MY_ATTACHMENTS_DIR } from "./constants";
import { FileAttachmentModel, SavedFileModel } from "./models";
import { getUploadedFiles } from "./actions";
import { CreateDirectoryModal } from './CreateDirectoryModal';
export const FileDisplayPanel : FC = memo(() => {
const [loading, setLoading] = useState<boolean>(true);
const [showCreateDirectoryModal, setShowCreateDirectoryModal] = useState<boolean>();
const [selectedDeleteResource, setSelectedDeleteResource] = useState<string>();
const [fileAttachmentModel, setFileAttachmentModel] = useState<FileAttachmentModel>(new FileAttachmentModel());
//equivalent to componentDidMount and componentDidUpdate
useEffect(() => {
if (!loading) return;
getUploadedFiles(ActionURL.getContainer(), MY_ATTACHMENTS_DIR, true)
.then((files:Array<SavedFileModel>) => {
if (files?.length > 0) {
const updatedModel = produce(fileAttachmentModel, (draft: Draft<FileAttachmentModel>) => {
draft['savedFiles'] = files;
});
setFileAttachmentModel(updatedModel);
}
else {
setFileAttachmentModel(new FileAttachmentModel());
}
setLoading(false);
});
}, [loading]);
const onCreateDirectory = useCallback(() => {
setShowCreateDirectoryModal(true);
}, []);
const closeCreateDirectory = useCallback(() => {
setShowCreateDirectoryModal(false);
}, []);
const submitCreateDirectory = useCallback((name: string) => {
let path = MY_ATTACHMENTS_DIR;
if (!name?.startsWith('/')) path = path + '/';
path = path + name;
// Note: containerPath param can be the path (i.e. '/MyProject') or the container GUID
createWebDavDirectory(ActionURL.getContainer(), path, true)
.then(() => {
// reset loading state to force refresh of panel savedFiles
setLoading(true);
});
closeCreateDirectory();
}, [closeCreateDirectory, createWebDavDirectory]);
const onDeleteResource = useCallback((resourceName: string) => {
let path = MY_ATTACHMENTS_DIR;
if (!resourceName?.startsWith('/')) path = path + '/';
path = path + resourceName;
setSelectedDeleteResource(path);
}, []);
const onCancelDeleteResource = useCallback(() => {
setSelectedDeleteResource(undefined);
}, []);
const onConfirmDeleteResource = useCallback(() => {
deleteWebDavResource(ActionURL.getContainer(), selectedDeleteResource)
.then(() => {
// reset loading state to force refresh of panel savedFiles
setLoading(true);
setSelectedDeleteResource(undefined);
});
}, [deleteWebDavResource, selectedDeleteResource]);
return (
<div className='panel panel-default'>
<div className='panel-heading'>
My Uploaded Attachments
</div>
<div className='panel-body'>
{loading && <LoadingSpinner />}
{
!loading && fileAttachmentModel.savedFiles?.length > 0 && (
<ul>
{
fileAttachmentModel?.savedFiles?.map((savedFile) => (
<li key={savedFile.fileName}>
<a href={savedFile.href} target='_blank'>{savedFile.fileName}</a>
<a
className="labkey-text-link"
style={{ paddingLeft: '10px' }}
onClick={() => onDeleteResource(savedFile.fileName)}
>
Delete
</a>
</li>
))
}
</ul>
)
}
{
!loading && !fileAttachmentModel.savedFiles && (
<p>
No files or directories to display. Use the panel above to upload files to this location.
</p>
)
}
{!loading && (
<p>
<a className='labkey-text-link' href={ActionURL.buildURL('filecontent', 'begin', undefined, {
path: MY_ATTACHMENTS_DIR
})}>
Manage Files
</a>
<a className='labkey-text-link' onClick={onCreateDirectory}>
Create Directory
</a>
</p>
)}
{showCreateDirectoryModal && <CreateDirectoryModal close={closeCreateDirectory} submit={submitCreateDirectory} />}
{selectedDeleteResource && (
<Modal onConfirm={onConfirmDeleteResource} onCancel={onCancelDeleteResource} confirmText="Delete" confirmClass="btn-danger" title="Delete Resource?">
<p>Are you sure you want to delete the selected resource?</p>
<p><b>{selectedDeleteResource}</b></p>
</Modal>
)}
</div>
</div>
);
})