-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WAITP-1330: File upload question type #5100
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c7c20bb
WIP
alexd-bes b6759ad
Use built in functionality
alexd-bes d6f34da
Add comment
alexd-bes 8671ccb
Fix build errors
alexd-bes 70fcc83
Working file upload
alexd-bes 5d5b630
Fix build
alexd-bes 2dd49c5
Remove unused vars
alexd-bes 8dbdb39
Merge branch 'datatrak-web-epic' into waitp-1330-doc-file-question
tcaiger 1ccb75d
Update format of file upload answers
alexd-bes bf9d464
Merge branch 'datatrak-web-epic' into waitp-1330-doc-file-question
alexd-bes bb04b4e
Update schemas.ts
alexd-bes 6c1bebc
Update processSurveyResponse.test.ts
alexd-bes 28c1843
Merge branch 'datatrak-web-epic' into waitp-1330-doc-file-question
alexd-bes 4b2340b
Merge branch 'datatrak-web-epic' into waitp-1330-doc-file-question
tcaiger 0aa69dd
Update upsertAnswers.js
tcaiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good chance to move the handling of files and images from meditrak-app-server to central server and re-use that. It looks pretty much the same but will require a bit of refactoring on the meditrak-app-server side. This might be a good task to pair on with @rohan-bes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you wanna have a pair on this @acdunham? I think there'd be some time next week if you wanted 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, sounds good @rohan-bes, I will make a calendar invite :)