Skip to content

Commit

Permalink
upload button
Browse files Browse the repository at this point in the history
  • Loading branch information
birm committed Feb 12, 2025
1 parent 0a34fa4 commit 7c960d2
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/components/ESNavbar/ESNavbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Nav from 'react-bootstrap/Nav';
import PropTypes from 'prop-types';
import DownloadButton from '../DownloadButton';
import HomeButton from '../HomeButton';
import UploadButton from '../UploadButton';
import Settings from '../Settings/Settings';

function ESNavbar(props) {
Expand All @@ -24,6 +25,7 @@ function ESNavbar(props) {
>
{url ? <HomeButton url={url} /> : null}
<DownloadButton data={data} title={title} />
<UploadButton />
<Navbar.Brand href={url || '#'}>{title || 'Eaglescope'}</Navbar.Brand>
<Nav className="mr-auto" />
<Form inline="true">
Expand Down
140 changes: 140 additions & 0 deletions source/components/UploadButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React, { PureComponent } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faUpload , faChartBar} from '@fortawesome/free-solid-svg-icons';

Check failure on line 3 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

There should be no space before ','

Check failure on line 3 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

A space is required before '}'
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
import * as d3 from 'd3';

class UploadButton extends PureComponent {
constructor(props, ctx) {
super(props, ctx);
this.state = {
showModal: false, // modal visibility

Check failure on line 12 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Multiple spaces found before '// modal visib...'
file: null, // selected file

Check failure on line 13 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Multiple spaces found before '// selected fi...'
fileContent: null, // file content

Check failure on line 14 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Multiple spaces found before '// file conten...'
isLoading: false,
};

this.handleFileChange = this.handleFileChange.bind(this);
this.handleUpload = this.handleUpload.bind(this);
this.toggleModal = this.toggleModal.bind(this);
this.handleSampleAnalysis = this.handleSampleAnalysis.bind(this);
}

toggleModal() {

Check failure on line 24 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

toggleModal should be placed after handleSampleAnalysis
this.setState(prevState => ({

Check failure on line 25 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Expected parentheses around arrow function argument
showModal: !prevState.showModal,
}));
}

handleFileChange(event) {
const file = event.target.files[0];
this.setState({ file });
}

handleUpload() {
const { file } = this.state;

if (!file) {
alert('Please select a CSV file to upload');

Check warning on line 39 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Unexpected alert
return;
}

this.setState({ isLoading: true });

// Use d3.csv to parse the uploaded file
d3.csv(URL.createObjectURL(file))
.then((data) => {
// Save the file and parsed content in local storage
localStorage.setItem('uploadedCSV', JSON.stringify(data));
this.setState({ fileContent: data, isLoading: false });

// Trigger sample analysis
this.handleSampleAnalysis(data);
this.toggleModal(); // Close the modal after successful upload
})
.catch((error) => {
alert('Error parsing the CSV file.');

Check warning on line 57 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Unexpected alert
console.error(error);
this.setState({ isLoading: false });
});
}

handleSampleAnalysis(data) {

Check failure on line 63 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Expected 'this' to be used by class method 'handleSampleAnalysis'
console.error("TODO!!", data);

Check failure on line 64 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Strings must use singlequote
}

render() {
const { showModal, isLoading } = this.state;

return (
<div>
<Button
size="lg"
style={{
background: 'none',
border: '2px solid #ccc', // Adding a subtle border

Check failure on line 76 in source/components/UploadButton.js

View workflow job for this annotation

GitHub Actions / Run npm lint

Multiple spaces found before '// Adding a su...'
borderRadius: '12px', // Rounded corners
position: 'relative', // Position relative for absolute positioning of icons
width: '50px', // Set width to fit icon size
height: '50px', // Set height to fit icon size
padding: 0, // Remove padding
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
onClick={this.toggleModal}
>
<FontAwesomeIcon
size="sm"
icon={faUpload}
style={{
position: 'absolute',
top: '5px',
left: '5px',
}}
/>

<FontAwesomeIcon
size="sm"
icon={faChartBar}
style={{
position: 'absolute',
bottom: '5px',
right: '5px',
}}
/>
</Button>

{/* Modal for file upload */}
<Modal show={showModal} onHide={this.toggleModal}>
<Modal.Header closeButton>
<Modal.Title>Upload CSV Data File</Modal.Title>
</Modal.Header>
<Modal.Body>
{isLoading ? (
<p>Processing your file...</p>
) : (
<div>
<input
type="file"
accept=".csv"
onChange={this.handleFileChange}
/>
<Button
variant="primary"
onClick={this.handleUpload}
disabled={!this.state.file}
>
Upload
</Button>
</div>
)}
</Modal.Body>
</Modal>
</div>
);
}
}

export default UploadButton;

0 comments on commit 7c960d2

Please sign in to comment.