Skip to content

Commit

Permalink
init fe app
Browse files Browse the repository at this point in the history
  • Loading branch information
FedirAlifirenko committed Oct 20, 2024
1 parent afad36a commit 950bc98
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
7 changes: 7 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Frontend App

## Start
```commandline
python3 -m http.server 8080
```
Open [http://localhost:8080](http://localhost:8080) in your browser.
19 changes: 19 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Simple App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>My Simple App</h1>

<label for="fileInput" class="file-upload-label">Upload a file:</label>
<input type="file" id="fileInput" class="file-upload-input">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/resumable.min.js"></script>
<script src="script.js"></script>

</body>
</html>
58 changes: 58 additions & 0 deletions frontend/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Constants
const API_URL = 'http://localhost:8000/api/files';

var r = new Resumable({
target: API_URL + '/upload',
chunkSize: 1 * 1024 * 1024, // 1 MB
simultaneousUploads: 3,
testChunks: true,
throttleProgressCallbacks: 1,
});

// Event handlers
r.on('fileAdded', function(file) {
r.upload();
});

r.on('fileProgress', function(file) {
console.log('File progress', file.progress());
});

r.on('fileSuccess', function(file, message) {
console.log('File uploaded successfully', file);
completeUpload(file);
});

r.on('fileError', function(file, message) {
console.error('File upload error', message);
});

// Add a file input to trigger the upload
document.getElementById('fileInput').addEventListener('change', function(event) {
r.addFile(event.target.files[0]);
});

function completeUpload(file) {
fetch(API_URL + '/upload/complete', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
resumable_identifier: file.uniqueIdentifier,
resumable_filename: file.fileName,
}),
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to complete upload');
}
return response.json();
})
.then(data => {
console.log('Upload complete:', data);
})
.catch(error => {
console.error('Error completing upload:', error);
});
}
19 changes: 19 additions & 0 deletions frontend/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}

.file-upload-label {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: white;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
}

.file-upload-input {
display: none;
}

0 comments on commit 950bc98

Please sign in to comment.