Skip to content

Commit eee59c9

Browse files
authored
FileAttachmentForm demo page update to include example usage of createWebDavDirectory() (#103)
1 parent bccc09a commit eee59c9

File tree

4 files changed

+103
-20
lines changed

4 files changed

+103
-20
lines changed

demo/package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"test": "cross-env NODE_ENV=test jest"
1313
},
1414
"dependencies": {
15-
"@labkey/components": "2.218.2"
15+
"@labkey/components": "2.222.0"
1616
},
1717
"devDependencies": {
1818
"@labkey/build": "6.2.1",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { FC, memo, useCallback, useState } from 'react';
2+
import { Button, Col, Form, FormControl, Modal, Row } from 'react-bootstrap';
3+
4+
interface Props {
5+
close: () => void;
6+
submit: (name: string) => void;
7+
}
8+
9+
export const CreateDirectoryModal : FC<Props> = memo(props => {
10+
const { close, submit } = props;
11+
const [name, setName] = useState<string>('');
12+
13+
const onChange = useCallback((evt: any) => {
14+
setName(evt.target.value);
15+
}, []);
16+
17+
const _submit = useCallback((evt: any) => {
18+
submit(name?.trim());
19+
}, [submit, name]);
20+
21+
return (
22+
<Modal show={true} onHide={close}>
23+
<Modal.Header>Create Directory</Modal.Header>
24+
<Modal.Body>
25+
<Form>
26+
<Row className="form-group">
27+
<Col xs={4}>
28+
<div>Directory Name:</div>
29+
</Col>
30+
<Col xs={8}>
31+
<FormControl type="text" id={'directory-name'} name={'directory-name'} onChange={onChange} />
32+
</Col>
33+
</Row>
34+
</Form>
35+
<Row>
36+
<Col xs={12}>
37+
<Button onClick={close} className="pull-left">
38+
Cancel
39+
</Button>
40+
<Button
41+
className="pull-right"
42+
bsStyle="success"
43+
disabled={name?.trim().length === 0}
44+
onClick={_submit}
45+
>
46+
Submit
47+
</Button>
48+
</Col>
49+
</Row>
50+
</Modal.Body>
51+
</Modal>
52+
)
53+
});

demo/src/client/FileAttachmentPage/FileDisplayPanel.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
import React, { FC, memo, useEffect, useState } from 'react';
1+
import React, { FC, memo, useCallback, useEffect, useState } from 'react';
22
import { Draft, produce } from "immer";
33
import { ActionURL } from "@labkey/api";
4-
import { LoadingSpinner } from '@labkey/components';
4+
import { createWebDavDirectory, LoadingSpinner } from '@labkey/components';
55

66
import { MY_ATTACHMENTS_DIR } from "./constants";
77
import { FileAttachmentModel, SavedFileModel } from "./models";
88
import { getUploadedFiles } from "./actions";
9+
import { CreateDirectoryModal } from './CreateDirectoryModal';
910

1011
export const FileDisplayPanel : FC = memo(() => {
1112
const [loading, setLoading] = useState<boolean>(true);
13+
const [showCreateDirectoryModal, setShowCreateDirectoryModal] = useState<boolean>();
1214
const [fileAttachmentModel, setFileAttachmentModel] = useState<FileAttachmentModel>(new FileAttachmentModel());
1315

1416
//equivalent to componentDidMount and componentDidUpdate
1517
useEffect(() => {
16-
getUploadedFiles(ActionURL.getContainer(), MY_ATTACHMENTS_DIR, false)
18+
if (!loading) return;
19+
20+
getUploadedFiles(ActionURL.getContainer(), MY_ATTACHMENTS_DIR, true)
1721
.then((files:Array<SavedFileModel>) => {
1822
if (files?.length > 0) {
1923
const updatedModel = produce(fileAttachmentModel, (draft: Draft<FileAttachmentModel>) => {
@@ -27,8 +31,30 @@ export const FileDisplayPanel : FC = memo(() => {
2731

2832
setLoading(false);
2933
});
34+
}, [loading]);
35+
36+
const onCreateDirectory = useCallback(() => {
37+
setShowCreateDirectoryModal(true);
3038
}, []);
3139

40+
const closeCreateDirectory = useCallback(() => {
41+
setShowCreateDirectoryModal(false);
42+
}, []);
43+
44+
const submitCreateDirectory = useCallback((name: string) => {
45+
let path = MY_ATTACHMENTS_DIR;
46+
if (!name?.startsWith('/')) path = path + '/';
47+
path = path + name;
48+
49+
createWebDavDirectory(ActionURL.getContainer(), path, true)
50+
.then(() => {
51+
// reset loading state to force refresh of panel savedFiles
52+
setLoading(true);
53+
});
54+
55+
closeCreateDirectory();
56+
}, [closeCreateDirectory]);
57+
3258
return (
3359
<div className='panel panel-default'>
3460
<div className='panel-heading'>
@@ -52,7 +78,7 @@ export const FileDisplayPanel : FC = memo(() => {
5278
{
5379
!loading && !fileAttachmentModel.savedFiles && (
5480
<p>
55-
No files to display. Use the panel above to upload files to this location.
81+
No files or directories to display. Use the panel above to upload files to this location.
5682
</p>
5783
)
5884
}
@@ -63,8 +89,12 @@ export const FileDisplayPanel : FC = memo(() => {
6389
})}>
6490
Manage Files
6591
</a>
92+
<a className='labkey-text-link' onClick={onCreateDirectory}>
93+
Create Directory
94+
</a>
6695
</p>
6796
)}
97+
{showCreateDirectoryModal && <CreateDirectoryModal close={closeCreateDirectory} submit={submitCreateDirectory} />}
6898
</div>
6999
</div>
70100
);

0 commit comments

Comments
 (0)