From 51de925f7cea013cba80565872ce84e5a470faf4 Mon Sep 17 00:00:00 2001 From: Truong Vo <41848815+vmtruong301296@users.noreply.github.com> Date: Sat, 21 Sep 2024 08:20:53 +0700 Subject: [PATCH 1/3] Intergate api export file --- .../pages/StaffEvaluation/StaffEvaluation.tsx | 59 +++++++++++-------- FRONTEND/src/rtk/helpers/apiService.ts | 37 +++++++++++- 2 files changed, 70 insertions(+), 26 deletions(-) diff --git a/FRONTEND/src/pages/StaffEvaluation/StaffEvaluation.tsx b/FRONTEND/src/pages/StaffEvaluation/StaffEvaluation.tsx index 73a3354..b47425b 100644 --- a/FRONTEND/src/pages/StaffEvaluation/StaffEvaluation.tsx +++ b/FRONTEND/src/pages/StaffEvaluation/StaffEvaluation.tsx @@ -2,15 +2,14 @@ import { evaluation, getAllTechByUserId, getAllUser, - sprintReview + sprintReview, } from '@/api/Admin' import DataTableAll from '@/components/DataTable/DataTable' import ProjectInvolvement from '@/components/ProjectInvolvement/ProjectInvolvement' -import { get } from '@/rtk/helpers/apiService' +import { get, getDownloadFile } from '@/rtk/helpers/apiService' import { Box, Button, Loader, Select, Text, Title } from '@mantine/core' import { DateInput } from '@mantine/dates' import { notifications } from '@mantine/notifications' -import axios from 'axios' import moment from 'moment' import { useEffect, useState } from 'react' import classes from './StaffEvaluation.module.css' @@ -53,8 +52,6 @@ const StaffEvaluation = () => { toDate: null, }) - console.log(filter, 'filter') - const getListUser = async () => { try { const params = {} @@ -72,35 +69,46 @@ const StaffEvaluation = () => { return [] } + function getFormattedDateTime(): string { + const now = new Date() + const year = now.getFullYear() + const month = String(now.getMonth() + 1).padStart(2, '0') // Tháng bắt đầu từ 0 + const day = String(now.getDate()).padStart(2, '0') + const hours = String(now.getHours()).padStart(2, '0') + const minutes = String(now.getMinutes()).padStart(2, '0') + const seconds = String(now.getSeconds()).padStart(2, '0') + + return `${year}${month}${day}${hours}${minutes}${seconds}` + } + const downloadFile = async () => { - await axios({ - url: evaluation, - method: 'GET', - responseType: 'blob', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAvYXBpL3YxL2FkbWluL2xvZ2luIiwiaWF0IjoxNzI2ODE3ODU3LCJleHAiOjE3MjY5MDQyNTcsIm5iZiI6MTcyNjgxNzg1NywianRpIjoid0kzeXM5SGZiV21wS3pONSIsInN1YiI6IjE2IiwicHJ2IjoiZDJmZjI5MzM5YThhM2U4MmMzNTgyYTVhOGU3MzlkZjE3ODliYjEyZiJ9.oEHW0cIlQkawYQMVZnz5TZ5twzY18301eLmXjC55LfY`, - }, - params: { - ...filter, - userID: 3, - }, - }) - .then((response) => { - const fileURL = window.URL.createObjectURL(new Blob([response.data])) + try { + const params = { + userID: filter.userID, + } + const res = await getDownloadFile(evaluation, params) + + if (res.status) { + const fileURL = window.URL.createObjectURL(new Blob([res.data])) const fileLink = document.createElement('a') + const fileName = `EXPORT_SPRINT_REVIEW_AND_TECHNICAL_EVALUATION_${getFormattedDateTime()}.docx` + fileLink.href = fileURL - fileLink.setAttribute('download', 'RENAME_WORD_FILE.docx') // -------------> RENAME_WORD_FILE + fileLink.setAttribute('download', fileName) document.body.appendChild(fileLink) fileLink.click() - fileLink.remove() + } + } catch (error: any) { + notifications.show({ + title: 'Error', + message: error.message ?? error, + color: 'red', }) - .catch((error) => { - console.error('Error downloading the file:', error) - }) + } + return [] } useEffect(() => { @@ -302,6 +310,7 @@ const StaffEvaluation = () => { >