Skip to content

Commit

Permalink
Add validation to file select
Browse files Browse the repository at this point in the history
  • Loading branch information
MattWong-ca committed Dec 8, 2024
1 parent 2d2157d commit fa54900
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/files/file-input/FileInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class FileInput extends React.Component {
webkitdirectory='true'
ref={el => { this.folderInput = el }}
onChange={this.onInputChange(this.folderInput)} />

</div>
)
}
Expand Down
53 changes: 32 additions & 21 deletions src/files/modals/bulk-import-modal/BulkImportModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,37 @@ class BulkImportModal extends React.Component {
}

state = {
selectedFile: null
selectedFile: null,
validationError: null
}

validatePath = (p) => {
if (!p.startsWith('/ipfs/')) {
p = `/ipfs/${p}`
validateFileContents = async (file) => {
try {
const text = await file.text()
const lines = text.split('\n').filter(line => line.trim())

for (const line of lines) {
const [cid] = line.trim().split(' ')
if (!isIPFS.cid(cid)) {
return { isValid: false, error: '*Invalid CID(s) found' }
}
}

return { isValid: true }
} catch (err) {
return { isValid: false, error: '*Failed to read file contents' }
}

return isIPFS.ipfsPath(p)
}

onChange = (event) => {
const files = event.target.files
this.setState({ selectedFile: files[0] })
onChange = async (event) => {
const file = event.target.files[0]
if (!file) return

const validation = await this.validateFileContents(file)
this.setState({
selectedFile: validation.isValid ? file : null,
validationError: validation.error
})
}

selectFile = async () => {
Expand All @@ -46,18 +63,6 @@ class BulkImportModal extends React.Component {
}
}

get inputClass () {
if (this.state.path === '') {
return ''
}

if (this.validatePath(this.state.path)) {
return 'b--green-muted focus-outline-green'
}

return 'b--red-muted focus-outline-red'
}

get isDisabled () {
return !this.state.selectedFile
}
Expand Down Expand Up @@ -98,12 +103,18 @@ class BulkImportModal extends React.Component {
>
{'Select File'}
</Button>

{this.state.selectedFile && (
<p className='mt2 charcoal'>
Selected file: {this.state.selectedFile.name}
</p>
)}

{this.state.validationError && (
<p className='mt2 red'>
{this.state.validationError}
</p>
)}
</ModalBody>

<ModalActions>
Expand Down

0 comments on commit fa54900

Please sign in to comment.