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