1- import React , { FC , memo , useEffect , useState } from 'react' ;
1+ import React , { FC , memo , useCallback , useEffect , useState } from 'react' ;
22import { Draft , produce } from "immer" ;
33import { ActionURL } from "@labkey/api" ;
4- import { LoadingSpinner } from '@labkey/components' ;
4+ import { createWebDavDirectory , LoadingSpinner } from '@labkey/components' ;
55
66import { MY_ATTACHMENTS_DIR } from "./constants" ;
77import { FileAttachmentModel , SavedFileModel } from "./models" ;
88import { getUploadedFiles } from "./actions" ;
9+ import { CreateDirectoryModal } from './CreateDirectoryModal' ;
910
1011export 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