11"use client" ;
2- import { DataTable } from "@/components/table/data-table" ;
32import userCol from "@/components/columns/UserCol" ;
3+ import { DataTable } from "@/components/table/data-table" ;
44// import { useEffect, useMemo, useState } from "react";
55// import { user } from "@/store/interfaces";
66// import useToast from "@/lib/toast";
7- import { useQuery } from "@tanstack/react-query " ;
7+ import { downloadCSV } from "@/api/downloadCSV " ;
88import { fetchUsers } from "@/api/fetchUsers" ;
9- import { type User } from "@/data/schema" ;
10- import { useState } from "react" ;
11- import { useDebounce } from "use-debounce" ;
12- import Image from "next/image" ;
139import loading from "@/assets/images/loading.gif" ;
14- import { UserModal } from "@/components/table/user-modal" ;
1510import { Button } from "@/components/ui/button" ;
16- import { downloadCSV } from "@/api/downloadCSV" ;
11+ import { type User } from "@/data/schema" ;
12+ import { useQuery } from "@tanstack/react-query" ;
13+ import Image from "next/image" ;
14+ import { useState } from "react" ;
15+ import { useDebounce } from "use-debounce" ;
1716
1817export default function Users ( ) {
1918 const [ cursorHistory , setCursorHistory ] = useState < string [ ] > ( [ ] ) ;
2019 const [ currentCursor , setCurrentCursor ] = useState < string | undefined > (
2120 undefined ,
2221 ) ;
23- const [ pageLimit , setPageLimit ] = useState < number > ( 10 ) ;
22+ const [ pageLimit , setPageLimit ] = useState < number > ( 10 ) ;
2423 const [ theName , setTheName ] = useState < string > ( "" ) ;
2524 // const queryClient = useQueryClient();
2625 const [ nameDebounce ] = useDebounce ( theName , 1000 ) ;
@@ -31,7 +30,11 @@ export default function Users() {
3130 } = useQuery ( {
3231 queryKey : [ "users" , currentCursor , nameDebounce , pageLimit ] ,
3332 queryFn : ( ) =>
34- fetchUsers ( { limit : pageLimit , cursorId : currentCursor , name : nameDebounce } ) ,
33+ fetchUsers ( {
34+ limit : pageLimit ,
35+ cursorId : currentCursor ,
36+ name : nameDebounce ,
37+ } ) ,
3538 placeholderData : ( previousData ) => previousData ,
3639 // enabled: !!debouncedSearch,
3740 } ) ;
@@ -52,33 +55,28 @@ export default function Users() {
5255 }
5356 } ;
5457
58+ const onClick = async ( ) => {
59+ try {
60+ const blob = await downloadCSV ( ) ;
5561
62+ const url = window . URL . createObjectURL ( blob . data as Blob ) ;
63+ const a = document . createElement ( "a" ) ;
64+ a . href = url ;
65+ a . download = "users.csv" ; // Set the filename for the downloaded file
66+ document . body . appendChild ( a ) ;
5667
68+ a . click ( ) ;
5769
58- const onClick = async ( ) => {
59- try {
60- const blob = await downloadCSV ( ) ;
61-
62- const url = window . URL . createObjectURL ( blob . data as Blob ) ;
63- const a = document . createElement ( 'a' ) ;
64- a . href = url ;
65- a . download = 'users.csv' ; // Set the filename for the downloaded file
66- document . body . appendChild ( a ) ;
67-
68- a . click ( ) ;
69-
70- window . URL . revokeObjectURL ( url ) ;
71- document . body . removeChild ( a ) ;
72- } catch ( err ) {
73- console . error ( 'Error downloading CSV:' , err ) ;
74- alert ( 'Failed to download CSV. Please try again.' ) ;
75- }
76- } ;
70+ window . URL . revokeObjectURL ( url ) ;
71+ document . body . removeChild ( a ) ;
72+ } catch ( err ) {
73+ console . error ( "Error downloading CSV:" , err ) ;
74+ alert ( "Failed to download CSV. Please try again." ) ;
75+ }
76+ } ;
7777 return (
7878 < div className = "p-4" >
79- < div className = "mb-4" > </ div >
80- { /* <DataTableUsers users={oosers} columns={userCol} /> */ }
81- < div className = "mb-4 flex flex items-start" >
79+ < div className = "mb-4 flex items-start" >
8280 < input
8381 className = "bg-gray w-[50%] rounded-md border p-2 text-white"
8482 placeholder = "Search"
@@ -88,9 +86,9 @@ export default function Users() {
8886 />
8987 < Button onClick = { onClick } > Download CSV </ Button >
9088 </ div >
91-
89+
9290 { isError && < div className = "text-red-500" > Error fetching team data</ div > }
93-
91+
9492 { isLoading && (
9593 < div className = "flex justify-center" >
9694 < Image
@@ -102,17 +100,18 @@ export default function Users() {
102100 />
103101 </ div >
104102 ) }
105-
106-
107- < DataTable < User , string >
108- setPageLimit = { setPageLimit }
109- pageLimit = { pageLimit }
110- columns = { userCol }
111- data = { userList ?. users ?? [ ] }
112- // data={oosers}
113- handleNextPage = { handleNextPage }
114- handlePrevPage = { handlePrevPage }
115- />
103+
104+ < div className = "w-full overflow-hidden" >
105+ < DataTable < User , string >
106+ setPageLimit = { setPageLimit }
107+ pageLimit = { pageLimit }
108+ columns = { userCol }
109+ data = { userList ?. users ?? [ ] }
110+ // data={oosers}
111+ handleNextPage = { handleNextPage }
112+ handlePrevPage = { handlePrevPage }
113+ />
114+ </ div >
116115 </ div >
117116 ) ;
118117}
0 commit comments