-
Notifications
You must be signed in to change notification settings - Fork 19
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
download filename fixes #693
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
import axios from "axios"; | ||||||
|
||||||
/* Fetching sheet as json object from the API , converting them into blob and downloading it. | ||||||
* Way to use the function. Just import the funtion downloadExcelWithCustomName and pass the filestoreid and customName you want to download the file. | ||||||
* Rest this function will take care for you and download it in your system. | ||||||
* | ||||||
* Eg. -> | ||||||
* const handleDownload = (id, name) => { | ||||||
* downloadExcelWithCustomName({fileStoreId: id, customName: name}); | ||||||
* } | ||||||
* | ||||||
*/ | ||||||
|
||||||
export const downloadExcelWithCustomName = ({ fileStoreId = null, customName = null }) => { | ||||||
const downloadExcel = (blob, fileName) => { | ||||||
const link = document.createElement("a"); | ||||||
link.href = URL.createObjectURL(blob); | ||||||
link.download = fileName + ".xlsx"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use template literals for better readability in string concatenation. - link.download = fileName + ".xlsx";
+ link.download = `${fileName}.xlsx`; Committable suggestion
Suggested change
|
||||||
document.body.append(link); | ||||||
link.click(); | ||||||
link.remove(); | ||||||
setTimeout(() => URL.revokeObjectURL(link.href), 7000); | ||||||
}; | ||||||
|
||||||
if (fileStoreId) { | ||||||
axios | ||||||
.get("/filestore/v1/files/id", { | ||||||
responseType: "arraybuffer", | ||||||
headers: { | ||||||
"Content-Type": "application/json", | ||||||
Accept: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||||||
"auth-token": Digit.UserService.getUser()?.["access_token"], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the access token retrieval. - "auth-token": Digit.UserService.getUser()?.["access_token"],
+ "auth-token": Digit.UserService.getUser()?.accessToken, Committable suggestion
Suggested change
|
||||||
}, | ||||||
params: { | ||||||
tenantId: Digit.ULBService.getCurrentTenantId(), | ||||||
fileStoreId: fileStoreId, | ||||||
}, | ||||||
}) | ||||||
.then(async (res) => { | ||||||
downloadExcel( | ||||||
new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }), | ||||||
customName ? customName : "download" | ||||||
); | ||||||
}); | ||||||
} | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import _ from "lodash"; | ||
import { downloadExcelWithCustomName } from "./downloadExcel"; | ||
|
||
export default {}; | ||
|
||
export const PRIMARY_COLOR="#C84C0E"; | ||
export { downloadExcelWithCustomName }; | ||
export const PRIMARY_COLOR = "#C84C0E"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use optional chaining consistently to ensure robustness.
Also applies to: 727-727
Committable suggestion