@@ -24,8 +24,8 @@ import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
24
24
import { FORM_URLS } from "@/config/api-urls" ;
25
25
import { tokenService } from "@/lib/services/token" ;
26
26
import { formatDate , formatDateTime } from "@/lib/utils" ;
27
+ import { useForms } from "@/hooks/use-forms" ;
27
28
28
- // Define response type based on your API structure
29
29
interface FormResponse {
30
30
id : string ;
31
31
created_at : string ;
@@ -42,9 +42,15 @@ export default function FormResponses({ form }: FormResponsesProps) {
42
42
const [ error , setError ] = useState < string | null > ( null ) ;
43
43
const [ pagination , setPagination ] = useState < PaginationState > ( {
44
44
pageIndex : 0 ,
45
- pageSize : 10 , // Fixed page size
45
+ pageSize : 1 ,
46
46
} ) ;
47
47
48
+ const { forms, isLoading : formsLoading } = useForms ( ) ;
49
+ const totalResponses = useMemo ( ( ) => {
50
+ const matchedForm = forms . find ( ( f ) => f . id === form . id ) ;
51
+ return matchedForm ? matchedForm . response_count : 0 ;
52
+ } , [ forms , form . id ] ) ;
53
+
48
54
// Fetch form responses
49
55
useEffect ( ( ) => {
50
56
async function fetchResponses ( ) {
@@ -91,7 +97,6 @@ export default function FormResponses({ form }: FormResponsesProps) {
91
97
const columns = useMemo < ColumnDef < FormResponse > [ ] > ( ( ) => {
92
98
if ( ! form . fields || responses . length === 0 ) return [ ] ;
93
99
94
- // Base columns
95
100
const baseColumns : ColumnDef < FormResponse > [ ] = [
96
101
{
97
102
accessorKey : "created_at" ,
@@ -103,14 +108,11 @@ export default function FormResponses({ form }: FormResponsesProps) {
103
108
} ,
104
109
] ;
105
110
106
- // Dynamically generate columns from form fields
107
111
const fieldColumns = form . fields . map ( ( field ) => ( {
108
112
accessorKey : `answers.${ field . tag } ` ,
109
113
header : field . label ,
110
114
cell : ( { getValue } : { getValue : ( ) => unknown } ) => {
111
115
const value = getValue ( ) ;
112
-
113
- // Handle different field type rendering
114
116
switch ( field . type ) {
115
117
case "multi_select" :
116
118
return Array . isArray ( value ) ? value . join ( ", " ) : value ;
@@ -138,12 +140,11 @@ export default function FormResponses({ form }: FormResponsesProps) {
138
140
state : {
139
141
pagination,
140
142
} ,
141
- pageCount : Math . ceil ( responses . length / pagination . pageSize ) ,
143
+ pageCount : Math . ceil ( totalResponses / pagination . pageSize ) ,
142
144
onPaginationChange : setPagination ,
143
145
} ) ;
144
146
145
- // Render loading state
146
- if ( isLoading ) {
147
+ if ( isLoading || formsLoading ) {
147
148
return (
148
149
< div className = "mx-auto my-8 space-y-6" >
149
150
< Card >
@@ -158,7 +159,6 @@ export default function FormResponses({ form }: FormResponsesProps) {
158
159
) ;
159
160
}
160
161
161
- // Render error state
162
162
if ( error ) {
163
163
return (
164
164
< div className = "mx-auto my-8 space-y-6" >
@@ -217,7 +217,6 @@ export default function FormResponses({ form }: FormResponsesProps) {
217
217
</ ScrollArea >
218
218
219
219
< div className = "flex items-center justify-center px-2" >
220
- { /* Pagination controls */ }
221
220
< div className = "flex items-center space-x-2" >
222
221
< Button
223
222
variant = "outline"
0 commit comments