Skip to content

Commit 1f16f11

Browse files
Merge pull request #146 from rolling-scopes-school/products-file-import
TASK 5: Update logic for uploading products files
2 parents 06660ed + 7f50f81 commit 1f16f11

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

src/components/pages/admin/PageProductImport/PageProductImport.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function PageProductImport() {
2020
return (
2121
<div className={classes.content}>
2222
<Box display="flex" alignItems="center">
23-
<CSVFileImport url={`${API_PATHS.import}/product/import`} title="Import Products CSV"/>
23+
<CSVFileImport url={`${API_PATHS.import}/import`} title="Import Products CSV"/>
2424
<Button size="small" color="primary" variant="contained" component={Link} to={'/admin/product-form/'}>
2525
create product
2626
</Button>

src/components/pages/admin/PageProductImport/components/CSVFileImport.tsx

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,12 @@ type CSVFileImportProps = {
1818
export default function CSVFileImport({url, title}: CSVFileImportProps) {
1919
const classes = useStyles();
2020
const [file, setFile] = useState<any>();
21-
const [uploadUrl, setUploadUrl] = useState<any>();
22-
23-
const createFile = (file: any) => {
24-
let reader = new FileReader()
25-
reader.onload = (e: any) => {
26-
console.log(e.target.result);
27-
setFile(e.target.result);
28-
}
29-
reader.readAsDataURL(file)
30-
};
3121

3222
const onFileChange = (e: any) => {
3323
console.log(e);
3424
let files = e.target.files || e.dataTransfer.files
3525
if (!files.length) return
36-
createFile(files[0])
26+
setFile(files.item(0));
3727
};
3828

3929
const removeFile = () => {
@@ -44,24 +34,18 @@ export default function CSVFileImport({url, title}: CSVFileImportProps) {
4434
// Get the presigned URL
4535
const response = await axios({
4636
method: 'GET',
47-
url
37+
url,
38+
params: {
39+
name: encodeURIComponent(file.name)
40+
}
4841
})
49-
console.log('Response: ', response.data)
50-
console.log('Uploading: ', file)
51-
let binary = atob(file.split(',')[1])
52-
let array = []
53-
for (var i = 0; i < binary.length; i++) {
54-
array.push(binary.charCodeAt(i))
55-
}
56-
let blobData = new Blob([new Uint8Array(array)], {type: 'text/plain'})
57-
console.log('Uploading to: ', response.data.uploadURL)
58-
const result = await fetch(response.data.uploadURL, {
42+
console.log('File to upload: ', file.name)
43+
console.log('Uploading to: ', response.data)
44+
const result = await fetch(response.data, {
5945
method: 'PUT',
60-
body: blobData
46+
body: file
6147
})
6248
console.log('Result: ', result)
63-
// Final URL for the user doesn't need the query string params
64-
setUploadUrl(response.data.uploadURL.split('?')[0]);
6549
setFile('');
6650
}
6751
;
@@ -75,8 +59,8 @@ export default function CSVFileImport({url, title}: CSVFileImportProps) {
7559
<input type="file" onChange={onFileChange}/>
7660
) : (
7761
<div>
78-
{!uploadUrl && <button onClick={removeFile}>Remove file</button>}
79-
{!uploadUrl && <button onClick={uploadFile}>Upload file</button>}
62+
<button onClick={removeFile}>Remove file</button>
63+
<button onClick={uploadFile}>Upload file</button>
8064
</div>
8165
)}
8266
</div>

0 commit comments

Comments
 (0)