1
- import React
2
- from 'react'
1
+ import React , { useState , useRef } from 'react'
3
2
import PropTypes from 'prop-types'
3
+ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
4
4
import {
5
+ Button ,
6
+ CloseButton ,
7
+ Form ,
8
+ InputGroup ,
9
+ ListGroup ,
5
10
Offcanvas ,
6
11
Tab ,
7
12
Tabs ,
8
13
} from 'react-bootstrap'
9
14
import FilesTable from '../../../components/FilesTable/FilesTable'
10
- import { allowNull } from '../../../resources/utilityFunctions'
15
+ import { allowNull , apiV2CompatibleStrings , convertToBase64 } from '../../../resources/utilityFunctions'
11
16
12
- const ViewFiles = ( { backgroundColor, initialFiles, handleClose } ) => {
17
+ const ViewFiles = ( { backgroundColor, initialFiles, handleClose, onSubmit } ) => {
18
+ const fileRef = useRef ( null )
19
+ const [ files , setFiles ] = useState ( initialFiles )
20
+ const [ tempFiles , setTempFiles ] = useState ( [ ] )
13
21
const documentTabs = [
14
22
{
15
23
eventKey : 'files' ,
@@ -23,16 +31,78 @@ const ViewFiles = ({ backgroundColor, initialFiles, handleClose }) => {
23
31
} ,
24
32
]
25
33
34
+ // TODO(summercook):
35
+ // - comment back in the following 3 methods once posting messages/attachments is working
36
+ // may need to use the handleSendingMessagesOrFiles to post
37
+
38
+ const handleAddFile = async ( event ) => {
39
+ event . preventDefault ( )
40
+ try {
41
+ // "event.target.files" returns a FileList, which looks like an array but does not respond to array methods
42
+ // except "length". we are using the spread syntax to set "files" to be an iterable array
43
+ const fileArray = [ ...event . target . files ]
44
+ const newBase64Files = await Promise . all ( convertToBase64 ( fileArray ) )
45
+ const newFiles = fileArray . map ( ( file , index ) => ( { [ file . name ] : newBase64Files [ index ] } ) )
46
+ setTempFiles ( [ ...tempFiles , ...newFiles ] )
47
+ console . log ( tempFiles , 'tempFilesAfterAdd' )
48
+ fileRef . current . value = ''
49
+ } catch ( error ) {
50
+ throw new Error ( error )
51
+ }
52
+ }
53
+
54
+ const handleSubmit = ( event ) => {
55
+ event . preventDefault ( )
56
+ onSubmit ( { files : apiV2CompatibleStrings ( [ ...tempFiles ] ) } )
57
+ console . log ( files , 'filesBeforeSubmit' )
58
+ //setFiles([...files, ...apiCompatibleTempFiles])
59
+ setTempFiles ( [ ] )
60
+ }
61
+
62
+ const handleDeleteTempFile = ( file ) => {
63
+ const remainingFiles = tempFiles . filter ( ( obj ) => obj !== file )
64
+ setTempFiles ( remainingFiles )
65
+ }
66
+
26
67
return (
27
68
< Offcanvas show onHide = { handleClose } placement = 'end' scroll = 'true' >
28
69
< Offcanvas . Header className = { `d-flex border-bottom px-3 py-2 bg-${ backgroundColor } -8` } closeButton >
29
70
< Offcanvas . Title > Documents</ Offcanvas . Title >
30
71
</ Offcanvas . Header >
31
72
< Offcanvas . Body className = 'border rounded p-2 m-3' >
73
+ < Form >
74
+ < h6 className = 'mt-3' > Upload Additional Documents</ h6 >
75
+ < InputGroup controlId = 'attachments' className = 'mb-3' >
76
+ < Form . Control
77
+ multiple
78
+ type = 'file'
79
+ onChange = { handleAddFile }
80
+ ref = { fileRef }
81
+ />
82
+ < Button
83
+ variant = 'outline-primary'
84
+ onClick = { handleSubmit }
85
+ type = 'submit'
86
+ >
87
+ < FontAwesomeIcon icon = 'fa-upload' />
88
+ </ Button >
89
+ </ InputGroup >
90
+ </ Form >
91
+ < ListGroup variant = 'flush' >
92
+ { tempFiles . map ( ( file ) => {
93
+ const fileName = Object . keys ( file ) [ 0 ]
94
+ return (
95
+ < ListGroup . Item key = { fileName } className = 'd-flex align-items-center' >
96
+ < span > { fileName } </ span >
97
+ < CloseButton onClick = { ( ) => handleDeleteTempFile ( file ) } className = 'ms-auto' />
98
+ </ ListGroup . Item >
99
+ )
100
+ } ) }
101
+ </ ListGroup >
32
102
< Tabs defaultActiveKey = 'files' id = 'document-tabs' justify fill >
33
103
{ documentTabs && documentTabs . map ( ( tab ) => {
34
104
const { eventKey, title, status } = tab
35
- const filteredFiles = initialFiles . filter ( ( f ) => ( status === f . status ) || ( status === 'Other File' && f . status === null ) )
105
+ const filteredFiles = files . filter ( ( f ) => ( status === f . status ) || ( status === 'Other File' && f . status === null ) )
36
106
return (
37
107
< Tab
38
108
eventKey = { eventKey }
@@ -43,6 +113,7 @@ const ViewFiles = ({ backgroundColor, initialFiles, handleClose }) => {
43
113
< FilesTable
44
114
files = { filteredFiles }
45
115
status = { status }
116
+ handleDeleteFile = { handleDeleteTempFile }
46
117
/>
47
118
</ Tab >
48
119
)
0 commit comments