From e1e42fc6c17ba99b7cad940b3c3e1c97df959140 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Sun, 28 Apr 2024 18:45:30 +0300 Subject: Added a simple UI for our application --- frontend/src/components/ImageProcessor.tsx | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 frontend/src/components/ImageProcessor.tsx (limited to 'frontend/src/components') diff --git a/frontend/src/components/ImageProcessor.tsx b/frontend/src/components/ImageProcessor.tsx new file mode 100644 index 0000000..bdcae19 --- /dev/null +++ b/frontend/src/components/ImageProcessor.tsx @@ -0,0 +1,75 @@ +import React, { useState } from 'react'; + +const BACK_END_URL = 'http://localhost:5000' + +const ImageProcessor = (): JSX.Element => { + const [file, setFile] = useState(null); + const [downloadUrl, setDownloadUrl] = useState(''); + + const handleFileChange = (event: React.ChangeEvent): void => { + if (event.target.files) { + setFile(event.target.files[0]); + } + }; + + const processImage = async (operation: 'edge_detection' | 'color_inversion'): Promise => { + if (!file) { + alert('Please select a file first!'); + return; + } + + const formData = new FormData(); + formData.append('image', file); + formData.append('operation', operation); + + try { + const response = await fetch(`${BACK_END_URL}/upload`, { + method: 'POST', + body: formData, + }); + + const data = await response.json(); + if (response.ok) { + setDownloadUrl(`${data.processed_file}`); + console.log(data.processed_file) + alert('File processed successfully!'); + } else { + alert(data.error || 'Failed to process the file'); + } + } catch (error) { + alert('Error connecting to the server'); + } + }; + + const downloadImage = (): void => { + if (!downloadUrl) { + alert('No processed image available for download!'); + return; + } + window.open(downloadUrl); + }; + + return ( +
+

Image Processing

+
+ + +
+
+ + +
+ + +
+ ); +}; + +export default ImageProcessor; -- cgit v1.2.3