diff --git a/README.md b/README.md index 631537d..f4c1ceb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# Deployed URL + +https://d1gsz2pixpaq08.cloudfront.net/ + # serverless-frontend # React + TypeScript + Vite diff --git a/src/App.tsx b/src/App.tsx index bf0b9e0..debc047 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react'; import './App.css'; import { getProducts, getProductsById } from './queries/products'; import { twJoin } from 'tailwind-merge'; +import { uploadFile } from './mutations/upload'; function App() { const [productList, setProductList] = useState>([]); @@ -29,6 +30,25 @@ function App() { } }; + const fileUploadHandler = async ( + event: React.ChangeEvent + ) => { + const file = event?.target.files?.[0]; + + if (!file) { + return; + } + + console.log(file.name); + + const response = await uploadFile(file.name, file); + if (response) { + alert('File upload: success'); + } else { + setError('Error uploading file'); + } + }; + return (

@@ -62,14 +82,27 @@ function App() { {selectedProduct && !isLoading && (
-

{selectedProduct.title}

-

${selectedProduct.price}

-

Stock: {selectedProduct.count}

+

Name: {selectedProduct.title}

+

Price: ${selectedProduct.price}

+

Count: {selectedProduct.count ?? 0}

)}

{error &&
{error}
} +
+

Upload your CSV

+
+ + +
+
); } diff --git a/src/http.ts b/src/http.ts index 0e736f6..9a71615 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,4 +1,5 @@ export const APIS = { products: 'https://ej64rpzdqi.execute-api.us-east-1.amazonaws.com/dev/products', + import: 'https://auf4d66a99.execute-api.us-east-1.amazonaws.com/dev/uploaded', }; diff --git a/src/mutations/upload.ts b/src/mutations/upload.ts new file mode 100644 index 0000000..b0540d2 --- /dev/null +++ b/src/mutations/upload.ts @@ -0,0 +1,20 @@ +import { getSignedUrl } from '../queries/upload'; + +export const uploadFile = async (fileName: string, file: File) => { + const signedUrl = await getSignedUrl(fileName); + + try { + await fetch(signedUrl, { + method: 'PUT', + body: file, + headers: { + 'Content-Type': 'text/csv', + 'Content-Disposition': `attachment; filename="${fileName}"`, + }, + }); + return true; + } catch (error) { + console.log('uploadFile', error); + return false; + } +}; diff --git a/src/queries/upload.ts b/src/queries/upload.ts new file mode 100644 index 0000000..ac4898f --- /dev/null +++ b/src/queries/upload.ts @@ -0,0 +1,11 @@ +import { APIS } from '../http'; + +export const getSignedUrl = async (fileName: string) => { + try { + const response = await fetch(`${APIS.import}/${fileName}`); + const json = await response.json(); + return json.url; + } catch (error) { + throw new Error('Error getting signedUrl'); + } +};