Skip to content
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

Feat/file storage #7

Merged
merged 16 commits into from
Feb 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add file upload to front end
hollannikas committed Jan 3, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 98d2f88608b11500af38186eb97f2198311cc1ff
2 changes: 2 additions & 0 deletions src/web/src/components/MyFiles.tsx
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import {useEffect, useState} from "react";
import {FileService} from "../services/fileService.ts";
import config from "../config";
import {BlobFile} from "../models/file.ts";
import UploadFile from "./UploadFile/UploadFile.tsx";

const MyFiles = () => {
const [isLoading, setIsLoading] = useState(false);
@@ -24,6 +25,7 @@ const MyFiles = () => {
<div key={file.id}><a href={file.uri}>{file.id}</a></div>
))}
</div>
<UploadFile />
</>;
}

42 changes: 42 additions & 0 deletions src/web/src/components/UploadFile/UploadFile.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.uploadFileContainer {
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
max-width: 400px;
margin: 0 auto;
}

.uploadFileContainer h2 {
font-size: 1.5em;
margin-bottom: 10px;
}

.fileUploadLabel {
display: block;
margin-bottom: 10px;
font-weight: bold;
}

.fileUploadInput {
display: block;
margin-bottom: 10px;
}

.uploadButton {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}

.uploadButton:disabled {
background-color: #ccc;
}

.uploadError {
color: red;
margin-top: 10px;
}
81 changes: 81 additions & 0 deletions src/web/src/components/UploadFile/UploadFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useState } from 'react';
import config from "../../config";
import { FileService } from "../../services/fileService.ts";
import styles from './UploadFile.module.css';

const useFileUpload = (baseUrl: string, baseRoute: string) => {
const [isUploading, setIsUploading] = useState(false);
const [uploadError, setUploadError] = useState<string | null>(null);

const uploadFile = async (file: File) => {
const formData = new FormData();
formData.append('file', file);

setIsUploading(true);
setUploadError(null);

try {
const fileService = new FileService(baseUrl, baseRoute);
const response = await fileService.upload(formData);
console.log('File uploaded successfully:', response.data);
} catch (error) {
console.error('Error uploading file:', error);
setUploadError('Failed to upload file. Please try again.');
} finally {
setIsUploading(false);
}
};

return { isUploading, uploadError, uploadFile };
};

const UploadFile = () => {
const [file, setFile] = useState<File | null>(null);
const { isUploading, uploadError, uploadFile } = useFileUpload(config.api.baseUrl, '/files');

const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.files && event.target.files.length > 0) {
setFile(event.target.files[0]);
}
};

const handleUpload = () => {
if (file) {
uploadFile(file);
}
};

return (
<div className={styles.uploadFileContainer}>
<h2>Upload File</h2>
<label htmlFor="file-upload" className={styles.fileUploadLabel}>
Choose a file to upload
</label>
<input
type="file"
id="file-upload"
onChange={handleFileChange}
className={styles.fileUploadInput}
aria-describedby="file-upload-description"
/>
<button
onClick={handleUpload}
disabled={isUploading || !file}
className={styles.uploadButton}
aria-busy={isUploading}
>
{isUploading ? 'Uploading...' : 'Upload'}
</button>
{uploadError && (
<p id="file-upload-error" className={styles.uploadError} role="alert">
{uploadError}
</p>
)}
<p id="file-upload-description" style={{ display: 'none' }}>
Please select a file to upload and then click the upload button.
</p>
</div>
);
};

export default UploadFile;
10 changes: 10 additions & 0 deletions src/web/src/services/fileService.ts
Original file line number Diff line number Diff line change
@@ -5,4 +5,14 @@ export class FileService extends RestService<BlobFile> {
public constructor(baseUrl: string, baseRoute: string) {
super(baseUrl, baseRoute);
}

upload = async (formData: FormData) => {
return await this.client.request<BlobFile>({
method: 'POST',
data: formData,
headers: {
'Content-Type': 'multipart/form-data',
}
});
}
}