Skip to content

Commit 096e6fb

Browse files
download user csv
1 parent 2ec38dc commit 096e6fb

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/api/downloadCSV.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import axios from "axios";
2+
import instance from "./axiosConfig";
3+
4+
export const downloadCSV = async () => {
5+
try {
6+
const response = await instance.get(`/admin/usercsv`, {
7+
withCredentials: true,
8+
responseType: 'blob'
9+
}
10+
);
11+
return response;
12+
} catch (err: any) {
13+
throw new Error(err.response?.data?.message || 'Failed to delete score');
14+
}
15+
};
16+

src/app/team/[id]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ function ScoreSection({ teamId }: { teamId: string }) {
258258
<div className="space-x-2">
259259
<Button
260260
onClick={handleSubmit}
261-
disabled={updateScoreMutation.isPending || createScoreMutation.isPending}
261+
262+
disabled={updateScoreMutation.isPending || createScoreMutation.isPending ||editMode}
262263
>
263264
{editMode ? "Update Score" : "Add Score"}
264265
</Button>
@@ -288,6 +289,7 @@ function ScoreSection({ teamId }: { teamId: string }) {
288289
size="sm"
289290
variant="secondary"
290291
onClick={() => handleEdit(score)}
292+
disabled={editMode}
291293
>
292294
Edit
293295
</Button>

src/app/users/page.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Image from "next/image";
1313
import loading from "@/assets/images/loading.gif";
1414
import { UserModal } from "@/components/table/user-modal";
1515
import { Button } from "@/components/ui/button";
16+
import { downloadCSV } from "@/api/downloadCSV";
1617

1718
export default function Users() {
1819
const [cursorHistory, setCursorHistory] = useState<string[]>([]);
@@ -54,7 +55,29 @@ export default function Users() {
5455

5556

5657

57-
58+
const onClick = async () => {
59+
try {
60+
// Fetch the CSV file as a Blob
61+
const blob = await downloadCSV();
62+
63+
// Create a download link
64+
const url = window.URL.createObjectURL(blob.data as Blob);
65+
const a = document.createElement('a');
66+
a.href = url;
67+
a.download = 'users.csv'; // Set the filename for the downloaded file
68+
document.body.appendChild(a);
69+
70+
// Trigger the download
71+
a.click();
72+
73+
// Clean up
74+
window.URL.revokeObjectURL(url);
75+
document.body.removeChild(a);
76+
} catch (err) {
77+
console.error('Error downloading CSV:', err);
78+
alert('Failed to download CSV. Please try again.');
79+
}
80+
};
5881
return (
5982
<div className="p-4">
6083
<div className="mb-4"></div>
@@ -67,7 +90,7 @@ export default function Users() {
6790
onChange={(e) => setTheName(e.target.value)}
6891
type="text"
6992
/>
70-
<Button></Button>
93+
<Button onClick={onClick}>Download CSV </Button>
7194
</div>
7295

7396
{isError && <div className="text-red-500">Error fetching team data</div>}

0 commit comments

Comments
 (0)