Skip to content

Commit

Permalink
feat: add download reports
Browse files Browse the repository at this point in the history
  • Loading branch information
newarifrh committed Sep 8, 2024
1 parent f6c0c98 commit 5193e17
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/api/reportApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,43 @@ export const downloadTemplatePartner = async () => {
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
};

export const downloadReports = async (payload: any) => {
const auth = useAuthStore();

const response = await fetch(`${BASE_URL}/v1/reports/download`, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
},
body: JSON.stringify(payload),
});

if (!response.ok) {
const result: any = await response.json();
throw new Error(result.message);
}

const blob = await response.blob();

const contentDisposition = response.headers.get("Content-Disposition");
let filename = `Master Data Report.csv`;

if (contentDisposition) {
const matches = contentDisposition.match(/filename="(.+)"/);
if (matches && matches[1]) {
filename = matches[1];
}
}

const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();

window.URL.revokeObjectURL(url);
document.body.removeChild(a);
};
8 changes: 7 additions & 1 deletion src/views/report/ListReportView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<el-button @click="clearSelection()" v-if="['TU'].includes(user.team)">Bersihkan Pilihan</el-button>
<el-button @click="clearFilter()">Setel Ulang Penyaringan</el-button>
<el-button @click="expandData()">Tampilkan Rincian</el-button>
<el-button @click="downloadSelection()" type="success">Unduh Kertas Kerja</el-button>
</div>
</div>
</div>
Expand All @@ -87,7 +88,7 @@
import { computed, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { Printer, Plus } from "@element-plus/icons-vue";
import { getReports, deleteReport, deleteReportOutput, printReports, printReport } from "@/api/reportApi";
import { getReports, deleteReport, deleteReportOutput, printReports, printReport, downloadReports } from "@/api/reportApi";
import { useUserStore } from "@/stores/user";
import { ElNotification, type ElTable } from "element-plus";
import { generatePeriods } from "@/utils/date";
Expand Down Expand Up @@ -154,6 +155,11 @@ const clearSelection = () => {
reportsTableRef.value!.clearSelection();
};
const downloadSelection = () => {
executeOperation(() => downloadReports(reportsSelected.value));
};
const expandData = () => {
if (reportsTableRef.value) {
reportsTableRef.value.data.forEach((row: any) => {
Expand Down

0 comments on commit 5193e17

Please sign in to comment.