-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WAITP-1330: File upload question type (#5100)
* WIP * Use built in functionality * Add comment * Fix build errors * Working file upload * Fix build * Remove unused vars * Update format of file upload answers * Update schemas.ts * Update processSurveyResponse.test.ts * Update upsertAnswers.js --------- Co-authored-by: Tom Caiger <caigertom@gmail.com>
- Loading branch information
Showing
16 changed files
with
283 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/datatrak-web/src/features/Questions/FileQuestion.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { IconButton } from '@material-ui/core'; | ||
import { Close } from '@material-ui/icons'; | ||
import { FileUploadField } from '@tupaia/ui-components'; | ||
import { SurveyQuestionInputProps } from '../../types'; | ||
import { QuestionHelperText } from './QuestionHelperText'; | ||
|
||
const MAX_FILE_SIZE_BYTES = 20 * 1024 * 1024; // 20 MB | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
align-items: flex-end; | ||
label { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
.MuiBox-root { | ||
margin-top: 0.5rem; | ||
order: 2; // put the helper text above the input | ||
span { | ||
font-size: 0.875rem; | ||
} | ||
} | ||
`; | ||
|
||
const ClearButton = styled(IconButton)` | ||
padding: 0.5rem; | ||
margin-left: 0.5rem; | ||
`; | ||
|
||
type Base64 = string | null | ArrayBuffer; | ||
|
||
const createEncodedFile = (fileObject: File): Promise<Base64> => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = () => { | ||
resolve(reader.result); | ||
}; | ||
|
||
reader.onerror = reject; | ||
|
||
reader.readAsDataURL(fileObject); | ||
}); | ||
}; | ||
|
||
export const FileQuestion = ({ | ||
label, | ||
required, | ||
detailLabel, | ||
controllerProps: { onChange, value: selectedFile, name }, | ||
}: SurveyQuestionInputProps) => { | ||
const handleChange = async (_e, _name, files) => { | ||
const file = files[0]; | ||
const encodedFile = await createEncodedFile(file); | ||
// convert to an object with an encoded file so that it can be handled in the backend and uploaded to s3 | ||
onChange({ | ||
name: file.name, | ||
value: encodedFile, | ||
}); | ||
}; | ||
|
||
const handleClearFile = () => { | ||
onChange(null); | ||
}; | ||
return ( | ||
<Wrapper> | ||
<FileUploadField | ||
name={name} | ||
fileName={selectedFile?.name} | ||
onChange={handleChange} | ||
label={label!} | ||
helperText={detailLabel!} | ||
maxSizeInBytes={MAX_FILE_SIZE_BYTES} | ||
showFileSize | ||
FormHelperTextComponent={QuestionHelperText} | ||
required={required} | ||
/> | ||
{selectedFile?.value && ( | ||
<ClearButton title="Clear file" onClick={handleClearFile}> | ||
<Close /> | ||
</ClearButton> | ||
)} | ||
</Wrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.