diff --git a/README.md b/README.md index 631537d..95e122d 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,7 @@ export default { - Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` - Optionally add `plugin:@typescript-eslint/stylistic-type-checked` - Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list + +# Note to me + +If project does not update, invalidate in cloudfront. diff --git a/src/api/index.ts b/src/api/index.ts index 7c3e17f..ed301ca 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -19,33 +19,34 @@ export const getProductsById = async (productId: number) => { }; export const getSignedUrl = async (fileName: string) => { - try { - const response = await fetch(`${APIS.import}/${fileName}`); + const credentials = localStorage.getItem('authorization_token') ?? ''; + const token = btoa(credentials); + + const response = await fetch(`${APIS.import}/${fileName}`, { + headers: { + Authorization: `Basic ${token}`, + }, + }); + if (response.ok) { const json = await response.json(); return json.url; - } catch (error) { - throw new Error('Error getting signedUrl'); + } else { + throw response; } }; -export const uploadFile = async (payload: { - fileName: string; - file: File; -}): Promise => { +export const uploadFile = async (payload: { fileName: string; file: File }) => { const signedUrl = await getSignedUrl(payload.fileName); - try { - await fetch(signedUrl, { - method: 'PUT', - body: payload.file, - headers: { - 'Content-Type': 'text/csv', - 'Content-Disposition': `attachment; filename="${payload.fileName}"`, - }, - }); - return true; - } catch (error) { - console.log('uploadFile', error); - return false; + const response = await fetch(signedUrl, { + method: 'PUT', + body: payload.file, + headers: { + 'Content-Type': 'text/csv', + 'Content-Disposition': `attachment; filename="${payload.fileName}"`, + }, + }); + if (!response.ok) { + throw response; } }; diff --git a/src/components/UploadCsv.tsx b/src/components/UploadCsv.tsx index 160b372..5aedf77 100644 --- a/src/components/UploadCsv.tsx +++ b/src/components/UploadCsv.tsx @@ -14,11 +14,15 @@ export default function UploadCsv() { title: 'Products uploaded', }); }, - onError: (error) => { - console.error(error); + onError: (error: any) => { + let description = 'Something went wrong'; + if (error.status === 401 || error.status === 403) { + console.error(error); + description = 'Unauthorized'; + } toast({ title: 'Error!', - description: 'Something went wrong', + description, }); }, }); diff --git a/src/main.tsx b/src/main.tsx index af077b6..473233a 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -25,6 +25,10 @@ const router = createBrowserRouter([ }, ], }, + { + path: '/upload', + element: , + }, ]); ReactDOM.createRoot(document.getElementById('root')!).render(