From 93832a77885f18754399c9746ac245f7beb2a268 Mon Sep 17 00:00:00 2001 From: timofejmalinin Date: Sun, 6 Nov 2022 23:55:47 +0400 Subject: [PATCH 1/2] download file --- src/modules/Sidebar/Sidebar.jsx | 142 ++++++++++++++++---------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/modules/Sidebar/Sidebar.jsx b/src/modules/Sidebar/Sidebar.jsx index 251204b..603b60a 100644 --- a/src/modules/Sidebar/Sidebar.jsx +++ b/src/modules/Sidebar/Sidebar.jsx @@ -8,79 +8,79 @@ import { GridSizeSelect } from "./GridSizeSelect"; import { ModelSelect } from "./ModelSelect"; import { Settings } from "./Settings"; -// const activeTablesMapper = { -// net_3: ["point3", "net_3"], -// net_4: ["point4", "net_4"], -// net_5: ["point5", "net_5"], -// }; - -// function download(filename, data) { -// const link = document.createElement("a"); -// link.href = -// "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + -// encodeURIComponent(data); -// link.setAttribute("download", filename); -// -// link.style.display = "none"; -// document.body.appendChild(link); -// -// link.click(); -// -// document.body.removeChild(link); -// } - -// const getRegionParam = (regionId) => { -// if (!regionId) return null; -// -// const [type, id] = regionId.split("-"); -// -// if (type === "ao") { -// return { msk_ao: Number(id) }; -// } -// -// return { msk_rayon: Number(id) }; -// }; +const activeTablesMapper = { + net_3: ["point3", "net_3"], + net_4: ["point4", "net_4"], + net_5: ["point5", "net_5"], +}; + +function download(filename, data) { + const link = document.createElement("a"); + link.href = + "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + + encodeURIComponent(data); + link.setAttribute("download", filename); + + link.style.display = "none"; + document.body.appendChild(link); + + link.click(); + + document.body.removeChild(link); +} + +const getRegionParam = (regionId) => { + if (!regionId) return null; + + const [type, id] = regionId.split("-"); + + if (type === "ao") { + return { msk_ao: Number(id) }; + } + + return { msk_rayon: Number(id) }; +}; export const Sidebar = () => { - // const { factors } = useFactors(); - // const { activeTypes } = useActiveTypes(); - // const { region } = useRegion(); - // const { gridSize } = useGridSize(); - // - // const handleExport = async () => { - // const params = { - // koefs: factors, - // tables: activeTablesMapper[gridSize], - // }; - // - // if (region) { - // params.filters = { - // ...params.filters, - // ...getRegionParam(region), - // }; - // } - // - // if (activeTypes.length) { - // params.filters = { - // ...params.filters, - // category: activeTypes, - // }; - // } - // - // const resp = await api.post("/api/raschet/", params); - // const blob = resp.data; - // - // const downloadLink = window.document.createElement("a"); - // downloadLink.href = window.URL.createObjectURL( - // new Blob([blob], { - // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - // }) - // ); - // downloadLink.download = "postamates.xlsx"; - // document.body.appendChild(downloadLink); - // downloadLink.click(); - // document.body.removeChild(downloadLink); - // }; + const { factors } = useFactors(); + const { activeTypes } = useActiveTypes(); + const { region } = useRegion(); + const { gridSize } = useGridSize(); + + const handleExport = async () => { + const params = { + koefs: factors, + tables: activeTablesMapper[gridSize], + }; + + if (region) { + params.filters = { + ...params.filters, + ...getRegionParam(region), + }; + } + + if (activeTypes.length) { + params.filters = { + ...params.filters, + category: activeTypes, + }; + } + + const resp = await api.post("/api/raschet/", params); + const blob = resp.data; + + const downloadLink = window.document.createElement("a"); + downloadLink.href = window.URL.createObjectURL( + new Blob([blob], { + type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + }) + ); + downloadLink.download = "postamates.xlsx"; + document.body.appendChild(downloadLink); + downloadLink.click(); + document.body.removeChild(downloadLink); + }; return (
From c82cecef34b3bef54aa591a22dbb807d402c9f7b Mon Sep 17 00:00:00 2001 From: timofejmalinin Date: Mon, 7 Nov 2022 00:09:09 +0400 Subject: [PATCH 2/2] working version --- src/modules/Sidebar/Sidebar.jsx | 142 ++++++++++++++++---------------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/modules/Sidebar/Sidebar.jsx b/src/modules/Sidebar/Sidebar.jsx index 603b60a..251204b 100644 --- a/src/modules/Sidebar/Sidebar.jsx +++ b/src/modules/Sidebar/Sidebar.jsx @@ -8,79 +8,79 @@ import { GridSizeSelect } from "./GridSizeSelect"; import { ModelSelect } from "./ModelSelect"; import { Settings } from "./Settings"; -const activeTablesMapper = { - net_3: ["point3", "net_3"], - net_4: ["point4", "net_4"], - net_5: ["point5", "net_5"], -}; - -function download(filename, data) { - const link = document.createElement("a"); - link.href = - "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + - encodeURIComponent(data); - link.setAttribute("download", filename); - - link.style.display = "none"; - document.body.appendChild(link); - - link.click(); - - document.body.removeChild(link); -} - -const getRegionParam = (regionId) => { - if (!regionId) return null; - - const [type, id] = regionId.split("-"); - - if (type === "ao") { - return { msk_ao: Number(id) }; - } - - return { msk_rayon: Number(id) }; -}; +// const activeTablesMapper = { +// net_3: ["point3", "net_3"], +// net_4: ["point4", "net_4"], +// net_5: ["point5", "net_5"], +// }; + +// function download(filename, data) { +// const link = document.createElement("a"); +// link.href = +// "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64," + +// encodeURIComponent(data); +// link.setAttribute("download", filename); +// +// link.style.display = "none"; +// document.body.appendChild(link); +// +// link.click(); +// +// document.body.removeChild(link); +// } + +// const getRegionParam = (regionId) => { +// if (!regionId) return null; +// +// const [type, id] = regionId.split("-"); +// +// if (type === "ao") { +// return { msk_ao: Number(id) }; +// } +// +// return { msk_rayon: Number(id) }; +// }; export const Sidebar = () => { - const { factors } = useFactors(); - const { activeTypes } = useActiveTypes(); - const { region } = useRegion(); - const { gridSize } = useGridSize(); - - const handleExport = async () => { - const params = { - koefs: factors, - tables: activeTablesMapper[gridSize], - }; - - if (region) { - params.filters = { - ...params.filters, - ...getRegionParam(region), - }; - } - - if (activeTypes.length) { - params.filters = { - ...params.filters, - category: activeTypes, - }; - } - - const resp = await api.post("/api/raschet/", params); - const blob = resp.data; - - const downloadLink = window.document.createElement("a"); - downloadLink.href = window.URL.createObjectURL( - new Blob([blob], { - type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - }) - ); - downloadLink.download = "postamates.xlsx"; - document.body.appendChild(downloadLink); - downloadLink.click(); - document.body.removeChild(downloadLink); - }; + // const { factors } = useFactors(); + // const { activeTypes } = useActiveTypes(); + // const { region } = useRegion(); + // const { gridSize } = useGridSize(); + // + // const handleExport = async () => { + // const params = { + // koefs: factors, + // tables: activeTablesMapper[gridSize], + // }; + // + // if (region) { + // params.filters = { + // ...params.filters, + // ...getRegionParam(region), + // }; + // } + // + // if (activeTypes.length) { + // params.filters = { + // ...params.filters, + // category: activeTypes, + // }; + // } + // + // const resp = await api.post("/api/raschet/", params); + // const blob = resp.data; + // + // const downloadLink = window.document.createElement("a"); + // downloadLink.href = window.URL.createObjectURL( + // new Blob([blob], { + // type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + // }) + // ); + // downloadLink.download = "postamates.xlsx"; + // document.body.appendChild(downloadLink); + // downloadLink.click(); + // document.body.removeChild(downloadLink); + // }; return (