Skip to content

Commit

Permalink
Added list refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhankarunhale committed Jul 25, 2024
1 parent 9888155 commit a111e09
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
69 changes: 44 additions & 25 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
import React from 'react';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import FileUpload from './components/FileUpload';
import FileList from './components/FileList';
import 'bootstrap/dist/css/bootstrap.min.css'; // Import Bootstrap CSS
import './App.css'; // Import custom CSS
import { Container } from 'react-bootstrap';

function App() {
return (
<Container fluid className="App">
<header className="App-header py-3">
<h1>File Management App</h1>
</header>
<main className="py-3">
<Row>
<Col md={6} className="mb-4">
<FileUpload />
</Col>
<Col md={6}>
<FileList />
</Col>
</Row>
</main>
</Container>
);
}
const App = () => {
const [files, setFiles] = useState([]);
const [loadingFiles, setLoadingFiles] = useState(false);
const apiUrl = process.env.REACT_APP_API_GATEWAY_URL;

useEffect(() => {
fetchFiles();
}, []);

const fetchFiles = async () => {
setLoadingFiles(true);
try {
const response = await axios.get(`${apiUrl}listFiles`);
if (response.status === 200) {
setFiles(response.data);
} else {
console.error('Unexpected status code:', response.status);
}
} catch (error) {
console.error('Error fetching files:', error);
} finally {
setLoadingFiles(false);
}
};

const handleFileUploadSuccess = () => {
fetchFiles(); // Reload the file list
};

return (
<Container className="mt-5">
<h1 className="text-center mb-4">File Management App</h1>
<FileUpload apiUrl={apiUrl} onUploadSuccess={handleFileUploadSuccess} />
{loadingFiles ? (
<p>Loading files...</p>
) : (
<FileList files={files} apiUrl={apiUrl} />
)}
</Container>
);
};

export default App;
19 changes: 8 additions & 11 deletions frontend/src/components/FileUpload.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React from 'react';
import axios from 'axios';
import { Container, Form, Button, Alert, Spinner } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css'; // Ensure Bootstrap CSS is imported

class FileUpload extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedFile: null,
apiUrl: process.env.REACT_APP_API_GATEWAY_URL,
error: null,
presignedUrl: null,
uploading: false,
Expand All @@ -21,7 +19,8 @@ class FileUpload extends React.Component {

onFileUpload = async event => {
event.preventDefault();
const { selectedFile, apiUrl } = this.state;
const { selectedFile } = this.state;
const { apiUrl, onUploadSuccess } = this.props;

if (!selectedFile) {
this.setError('No file selected!');
Expand Down Expand Up @@ -52,6 +51,11 @@ class FileUpload extends React.Component {
this.setState({ uploading: false });
alert('File uploaded successfully!');
console.log('File uploaded successfully:', uploadResponse.data);

// Notify parent component to refresh the file list
if (onUploadSuccess) {
onUploadSuccess();
}
} else {
console.error('Unexpected status code:', response.status);
this.setError(`Unexpected status code: ${response.status}`);
Expand All @@ -60,7 +64,6 @@ class FileUpload extends React.Component {
this.setState({ uploading: false });
if (error.response) {
console.error('Server responded with an error status:', error.response.status);
console.error('Error details:', error.response.data);
this.setError(`Error: ${error.response.status} - ${error.response.data}`);
} else if (error.request) {
console.error('No response received:', error.request);
Expand All @@ -77,17 +80,11 @@ class FileUpload extends React.Component {
};

render() {
const { error, presignedUrl, uploading } = this.state;
const { error, uploading } = this.state;
return (
<Container className="file-upload-container mt-5">
<h3 className="text-center mb-4">File Upload</h3>
{error && <Alert variant="danger">{error}</Alert>}
{presignedUrl && (
<Alert variant="info">
<p><strong>Presigned URL:</strong> {presignedUrl}</p>
</Alert>
)}

<Form onSubmit={this.onFileUpload}>
<Form.Group controlId="formFile" className="mb-3">
<Form.Label>Select file to upload</Form.Label>
Expand Down

0 comments on commit a111e09

Please sign in to comment.