Skip to content

Commit b0687d7

Browse files
Add demo for FileAttachmentForm & WebDav public components (#41)
- File attachment upload example that uses FileAttachmentForm & WebDav's uploadWebDavFile api. - Panel to view uploaded attachments that uses WebDav's getWebDavFiles api.
1 parent 922a5bd commit b0687d7

File tree

11 files changed

+303
-1
lines changed

11 files changed

+303
-1
lines changed

demo/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ including keeping the standard LabKey header and header menus.
77
Clone the tutorialModules repo into `/server/modules` directory and add the directory in `settings.gradle`. You can then build this module
88
using the [LabKey Gradle build]. This will install necessary packages, generate resources and put resources in correct LabKey module's directories.
99

10-
There are three example React pages:
10+
The demo module includes the following example React pages:
1111
- http://localhost:8080/labkey/home/demo-helloWorld.view?
1212
- http://localhost:8080/labkey/home/demo-queryModel.view?
1313
- http://localhost:8080/labkey/home/demo-todoList.view?
14+
- http://localhost:8080/labkey/home/demo-fileAttachmentForm.view?
1415

1516
Also, this module has an example of using the todoList page in a LabKey webpart.
1617

@@ -42,6 +43,7 @@ pages (note the "Dev" added to the end of the action name).
4243
- http://localhost:8080/labkey/home/demo-helloWorldDev.view?
4344
- http://localhost:8080/labkey/home/demo-queryModelDev.view?
4445
- http://localhost:8080/labkey/home/demo-todoListDev.view?
46+
- http://localhost:8080/labkey/home/demo-fileAttachmentFormDev.view?
4547

4648
<a name="jest"></a>
4749
## Jest Tests
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020 LabKey Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import React, { FC, memo, useCallback, useState } from 'react'
17+
import { Alert } from "@labkey/components";
18+
19+
import { FileAttachmentPanel } from "./FileAttachmentPanel";
20+
import { FileDisplayPanel } from './FileDisplayPanel';
21+
import { FileAttachmentModel } from "./models";
22+
import { uploadMyAttachmentsToServer } from "./actions";
23+
24+
import './fileAttachment.scss';
25+
26+
export const App : FC = memo(() => {
27+
const [fileAttachmentModel, setFileAttachmentModel] = useState<FileAttachmentModel>(new FileAttachmentModel());
28+
29+
const fileAttachmentChange = useCallback((model:FileAttachmentModel)=> {
30+
setFileAttachmentModel(model);
31+
}, [setFileAttachmentModel]);
32+
33+
const onSaveBtnHandler = useCallback(() => {
34+
uploadMyAttachmentsToServer(fileAttachmentModel)
35+
.then(r => {
36+
if (r) {
37+
window.location.reload();
38+
}
39+
})
40+
.catch(reject => {
41+
alert(reject);
42+
});
43+
}, [fileAttachmentModel]);
44+
45+
return (
46+
<>
47+
<Alert bsStyle="info">Note: saving uploaded files will place them in the LabKey filesystem for this container.</Alert>
48+
49+
<FileAttachmentPanel
50+
model={fileAttachmentModel}
51+
onInputChange={fileAttachmentChange}
52+
onSaveBtnHandler={onSaveBtnHandler}
53+
/>
54+
55+
<FileDisplayPanel />
56+
</>
57+
)
58+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { FC, memo, useCallback } from 'react';
2+
import { FileAttachmentForm } from '@labkey/components';
3+
import { Map } from 'immutable';
4+
import { Draft, produce } from 'immer';
5+
import { FileAttachmentModel } from "./models";
6+
import { Panel } from 'react-bootstrap';
7+
8+
interface Props {
9+
model: FileAttachmentModel;
10+
onInputChange: (model: FileAttachmentModel) => void;
11+
onSaveBtnHandler: () => void;
12+
}
13+
14+
// Functional component which would be rendered as part of the parent component
15+
export const FileAttachmentPanel: FC<Props> = memo((props) => {
16+
const { model, onInputChange, onSaveBtnHandler } = props;
17+
18+
const onFileChange = useCallback((files: Map<string, File>) => {
19+
const updatedModel = produce(model, (draft: Draft<FileAttachmentModel>) => {
20+
draft['filesToUpload'] = files;
21+
});
22+
onInputChange(updatedModel);
23+
}, [model, onInputChange]);
24+
25+
return (
26+
<div className='panel panel-default'>
27+
<div className='panel-heading'>
28+
My File Attachments
29+
</div>
30+
<div className='panel-body'>
31+
<FileAttachmentForm
32+
acceptedFormats=".pdf, .jpg"
33+
allowDirectories={false}
34+
allowMultiple={true}
35+
showLabel={false}
36+
showButtons={true}
37+
onSubmit={onSaveBtnHandler}
38+
onFileChange={onFileChange}
39+
/>
40+
</div>
41+
</div>
42+
);
43+
})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ActionURL } from "@labkey/api";
2+
import { getWebDavFiles, uploadWebDavFile, WebDavFile } from "@labkey/components";
3+
4+
import { MY_ATTACHMENTS_DIR } from "./constants";
5+
import { FileAttachmentModel, SavedFileModel } from "./models";
6+
7+
//Uploads files to the server using WebDav api.
8+
export function uploadMyAttachmentsToServer(model: FileAttachmentModel): Promise<any> {
9+
return new Promise((resolve, reject) => {
10+
11+
// Nothing to do here
12+
if (model.filesToUpload?.size === 0) {
13+
resolve(model.filesToUpload);
14+
}
15+
const uploadedFiles = Array<string>();
16+
17+
model.filesToUpload.map((fileToUpload) => {
18+
19+
if (fileToUpload) {
20+
uploadWebDavFile(fileToUpload, ActionURL.getContainer(), MY_ATTACHMENTS_DIR, true)
21+
.then((name: string) => {
22+
uploadedFiles.push(name);
23+
if (uploadedFiles.length === model.filesToUpload.size) {
24+
resolve(uploadedFiles);
25+
}
26+
})
27+
.catch(reason => {
28+
reject(reason);
29+
});
30+
}
31+
}, this);
32+
});
33+
}
34+
35+
// Gets file(s) previously uploaded to the server using WebDav api
36+
export async function getUploadedFiles(container: string, directory?: string, includeSubdirectories?: boolean): Promise<Array<SavedFileModel>> {
37+
return new Promise(async (resolve, reject) => {
38+
try {
39+
const webDavFilesResponse = await getWebDavFiles(container, directory, includeSubdirectories);
40+
41+
// Note: you can return other properties of the WebDavFile, below only returns file name & href
42+
const displayFiles = webDavFilesResponse.get('files').valueSeq().map((file: WebDavFile) => {
43+
return new SavedFileModel({fileName: file.name, href: file.href});
44+
});
45+
resolve(displayFiles.toArray());
46+
47+
} catch (response) {
48+
let msg = `Unable to load files in ${(directory ? directory : 'root')}`;
49+
if (response) {
50+
msg += `: ${response}`;
51+
reject(msg);
52+
}
53+
else {
54+
resolve(Array<SavedFileModel>());
55+
}
56+
}
57+
});
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
import { App } from './FileAttachmentForm';
5+
6+
// Need to wait for container element to be available in labkey wrapper before render
7+
window.addEventListener('DOMContentLoaded', (event) => {
8+
ReactDOM.render(<App/>, document.getElementById('app'));
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MY_ATTACHMENTS_DIR = 'MyAttachments';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { AppContainer } from 'react-hot-loader';
4+
5+
import { App } from './FileAttachmentForm';
6+
7+
const render = () => {
8+
ReactDOM.render(
9+
<AppContainer>
10+
<App />
11+
</AppContainer>,
12+
document.getElementById('app')
13+
)
14+
};
15+
16+
declare const module: any;
17+
18+
if (module.hot) {
19+
module.hot.accept();
20+
}
21+
22+
render();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "~@labkey/components/dist/assets/scss/theme";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { immerable } from "immer";
2+
import { Map } from "immutable";
3+
4+
export class FileAttachmentModel {
5+
[immerable] = true;
6+
7+
readonly filesToUpload?: Map<string, File>; // to upload files to the server
8+
readonly savedFiles?: Array<SavedFileModel>; // to get uploaded file props from the server
9+
10+
constructor(values?: Partial<FileAttachmentModel>) {
11+
Object.assign(this, values);
12+
}
13+
14+
static create(raw?: any): FileAttachmentModel {
15+
return new FileAttachmentModel({ ...raw });
16+
}
17+
}
18+
19+
export class SavedFileModel {
20+
[immerable] = true;
21+
22+
readonly href: string;
23+
readonly fileName: string;
24+
25+
constructor(values?: Partial<SavedFileModel>) {
26+
Object.assign(this, values);
27+
}
28+
29+
static create(raw?: any): SavedFileModel {
30+
return new SavedFileModel({ ...raw });
31+
}
32+
}

0 commit comments

Comments
 (0)