Skip to content

Commit 1cede1b

Browse files
committed
get adding files working, need to make it instant
1 parent 050efdb commit 1cede1b

File tree

1 file changed

+76
-5
lines changed

1 file changed

+76
-5
lines changed

src/compounds/ActionsGroup/actions/ViewFiles.jsx

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import React
2-
from 'react'
1+
import React, { useState, useRef } from 'react'
32
import PropTypes from 'prop-types'
3+
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
44
import {
5+
Button,
6+
CloseButton,
7+
Form,
8+
InputGroup,
9+
ListGroup,
510
Offcanvas,
611
Tab,
712
Tabs,
813
} from 'react-bootstrap'
914
import FilesTable from '../../../components/FilesTable/FilesTable'
10-
import { allowNull } from '../../../resources/utilityFunctions'
15+
import { allowNull, apiV2CompatibleStrings, convertToBase64 } from '../../../resources/utilityFunctions'
1116

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([])
1321
const documentTabs = [
1422
{
1523
eventKey: 'files',
@@ -23,16 +31,78 @@ const ViewFiles = ({ backgroundColor, initialFiles, handleClose }) => {
2331
},
2432
]
2533

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+
2667
return (
2768
<Offcanvas show onHide={handleClose} placement='end' scroll='true'>
2869
<Offcanvas.Header className={`d-flex border-bottom px-3 py-2 bg-${backgroundColor}-8`} closeButton>
2970
<Offcanvas.Title>Documents</Offcanvas.Title>
3071
</Offcanvas.Header>
3172
<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>
32102
<Tabs defaultActiveKey='files' id='document-tabs' justify fill>
33103
{documentTabs && documentTabs.map((tab) => {
34104
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))
36106
return (
37107
<Tab
38108
eventKey={eventKey}
@@ -43,6 +113,7 @@ const ViewFiles = ({ backgroundColor, initialFiles, handleClose }) => {
43113
<FilesTable
44114
files={filteredFiles}
45115
status={status}
116+
handleDeleteFile={handleDeleteTempFile}
46117
/>
47118
</Tab>
48119
)

0 commit comments

Comments
 (0)