@@ -10,6 +10,8 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
1010import { useState } from "react" ;
1111// import { TeamModal } from "@/components/table/team-modal";
1212import { useDebounce } from "use-debounce" ;
13+ import { Button } from "@/components/ui/button" ;
14+ import { downloadCSV } from "@/api/downloadCSV" ;
1315
1416export default function Teams ( ) {
1517 const [ cursorHistory , setCursorHistory ] = useState < string [ ] > ( [ ] ) ;
@@ -20,8 +22,9 @@ export default function Teams() {
2022 const [ theName , setTheName ] = useState < string > ( "" ) ;
2123 // const queryClient = useQueryClient();
2224 const [ selectedTeam , setSelectedTeam ] = useState < Team | null > ( null ) ;
23- const [ nameDebounce ] = useDebounce ( theName , 1000 ) ;
25+ const [ nameDebounce ] = useDebounce ( theName , 500 ) ;
2426 const [ open , setOpen ] = useState ( false ) ;
27+
2528
2629 const queryClient = useQueryClient ( ) ;
2730
@@ -65,19 +68,41 @@ export default function Teams() {
6568 setSelectedTeam ( team ) ;
6669 setOpen ( true ) ;
6770 } ;
71+ const onClick = async ( ) => {
72+ try {
73+ const blob = await downloadCSV ( { what : "teamcsv" } ) ;
74+
75+ const url = window . URL . createObjectURL ( blob ) ;
76+ const a = document . createElement ( "a" ) ;
77+ a . href = url ;
78+ a . download = "teams.csv" ; // Set the filename for the downloaded file
79+ document . body . appendChild ( a ) ;
80+
81+ a . click ( ) ;
82+
83+ window . URL . revokeObjectURL ( url ) ;
84+ document . body . removeChild ( a ) ;
85+ } catch ( err ) {
86+ console . error ( "Error downloading CSV:" , err ) ;
87+ alert ( "Failed to download CSV. Please try again." ) ;
88+ }
89+ } ;
6890
6991 return (
7092 < >
7193 < div className = "p-4" >
7294 < div className = "mb-4" > </ div >
73- < div className = "mb-4 flex flex-col items-start " >
95+ < div className = "mb-4 flex items-center " >
7496 < input
7597 className = "bg-gray w-[50%] rounded-md border p-2 text-white"
7698 placeholder = "Search"
7799 value = { theName }
78100 onChange = { ( e ) => setTheName ( e . target . value ) }
79101 type = "text"
80102 />
103+ < Button className = "ml-2" onClick = { onClick } >
104+ Download CSV{ " " }
105+ </ Button >
81106 </ div >
82107 { /* <DataTableUsers users={oosers} columns={userCol} /> */ }
83108
0 commit comments