@@ -4,12 +4,13 @@ import { type FormInterface } from "@churchapps/helpers";
44import { ApiHelper , UserHelper , Permissions , Loading , Locale } from "@churchapps/apphelper" ;
55import { Link } from "react-router-dom" ;
66import {
7- Icon , Table , TableBody , TableCell , TableRow , TableHead , Box , Typography , Stack , Button , Card , Tab
7+ Icon , Table , TableBody , TableCell , TableRow , TableHead , Box , Typography , Stack , Button , Card
88} from "@mui/material" ;
99import { Description as DescriptionIcon , Add as AddIcon , Archive as ArchiveIcon } from "@mui/icons-material" ;
1010import { SmallButton } from "@churchapps/apphelper" ;
1111import { PageHeader } from "@churchapps/apphelper" ;
1212import { useQuery } from "@tanstack/react-query" ;
13+ import { SmartTabs } from "../components/ui" ;
1314
1415export const FormsPage = ( ) => {
1516 const [ selectedFormId , setSelectedFormId ] = React . useState ( "notset" ) ;
@@ -147,33 +148,63 @@ export const FormsPage = () => {
147148
148149 if ( forms . isLoading || archivedForms . isLoading ) return < Loading /> ;
149150
150- const contents = (
151+ const renderTable = ( rows : JSX . Element [ ] ) => (
151152 < Table >
152153 < TableHead > { getTableHeader ( ) } </ TableHead >
153- < TableBody > { selectedTab === "forms" ? getRows ( ) : getArchivedRows ( ) } </ TableBody >
154+ < TableBody > { rows } </ TableBody >
154155 </ Table >
155156 ) ;
156157
157- const getTab = ( keyName : string , icon : string , text : string ) => (
158- < Tab
159- key = { keyName }
160- style = { { textTransform : "none" , color : "#000" } }
161- onClick = { ( ) => {
162- setSelectedTab ( keyName ) ;
163- } }
164- label = { < > { text } </ > }
165- />
158+ const formsCount = forms . data ?. length || 0 ;
159+ const archivedCount = archivedForms . data ?. length || 0 ;
160+
161+ const formsCard = (
162+ < Card sx = { { mt : getSidebar ( ) ? 2 : 0 } } >
163+ < Box sx = { { p : 2 , borderBottom : 1 , borderColor : "divider" } } >
164+ < Stack direction = "row" justifyContent = "space-between" alignItems = "center" >
165+ < Stack direction = "row" spacing = { 1 } alignItems = "center" >
166+ < DescriptionIcon />
167+ < Typography variant = "h6" > { Locale . label ( "forms.formsPage.forms" ) } </ Typography >
168+ </ Stack >
169+ < Typography variant = "body2" color = "text.secondary" >
170+ { `${ formsCount } ${ formsCount === 1 ? "form" : "forms" } ` }
171+ </ Typography >
172+ </ Stack >
173+ </ Box >
174+ < Box sx = { { p : 0 } } > { renderTable ( getRows ( ) ) } </ Box >
175+ </ Card >
176+ ) ;
177+
178+ const archivedCard = (
179+ < Card >
180+ < Box sx = { { p : 2 , borderBottom : 1 , borderColor : "divider" } } >
181+ < Stack direction = "row" justifyContent = "space-between" alignItems = "center" >
182+ < Stack direction = "row" spacing = { 1 } alignItems = "center" >
183+ < ArchiveIcon />
184+ < Typography variant = "h6" > { Locale . label ( "forms.formsPage.archForms" ) } </ Typography >
185+ </ Stack >
186+ < Typography variant = "body2" color = "text.secondary" >
187+ { `${ archivedCount } archived ${ archivedCount === 1 ? "form" : "forms" } ` }
188+ </ Typography >
189+ </ Stack >
190+ </ Box >
191+ < Box sx = { { p : 0 } } > { renderTable ( getArchivedRows ( ) ) } </ Box >
192+ </ Card >
166193 ) ;
167194
168- const tabs = [ ] ;
169- let defaultTab = "" ;
170- tabs . push ( getTab ( "forms" , "format_align_left" , Locale . label ( "forms.formsPage.forms" ) ) ) ;
171- if ( defaultTab === "" ) defaultTab = "forms" ;
172- if ( archivedForms . data ?. length > 0 ) {
173- tabs . push ( getTab ( "archived" , "archive" , Locale . label ( "forms.formsPage.archForms" ) ) ) ;
174- if ( defaultTab === "" ) defaultTab = "archived" ;
175- }
176- // Default tab is initialized via useState; avoid setting state during render.
195+ const tabs = [
196+ {
197+ key : "forms" ,
198+ label : Locale . label ( "forms.formsPage.forms" ) ,
199+ content : (
200+ < >
201+ { getSidebar ( ) }
202+ { formsCard }
203+ </ >
204+ )
205+ } ,
206+ { key : "archived" , label : Locale . label ( "forms.formsPage.archForms" ) , content : archivedCard , hidden : archivedForms . data ?. length === 0 } ,
207+ ] ;
177208
178209 return (
179210 < >
@@ -194,50 +225,10 @@ export const FormsPage = () => {
194225 { Locale . label ( "forms.formsPage.addForm" ) || "Add Form" }
195226 </ Button >
196227 ) }
197- { archivedForms . data ?. length > 0 && (
198- < Button
199- variant = "outlined"
200- onClick = { ( ) => {
201- setSelectedTab ( "archived" ) ;
202- } }
203- sx = { {
204- color : "#FFF" ,
205- backgroundColor : "transparent" ,
206- borderColor : "#FFF" ,
207- fontWeight : selectedTab === "archived" ? 600 : 400 ,
208- "&:hover" : {
209- backgroundColor : "rgba(255,255,255,0.1)" ,
210- color : "#FFF" ,
211- borderColor : "#FFF" ,
212- } ,
213- } } >
214- { Locale . label ( "forms.formsPage.archForms" ) }
215- </ Button >
216- ) }
217228 </ PageHeader >
218-
219229 { /* Tab Content */ }
220230 < Box sx = { { p : 3 } } >
221- { getSidebar ( ) }
222- < Card sx = { { mt : getSidebar ( ) ? 2 : 0 } } >
223- { /* Card Header */ }
224- < Box sx = { { p : 2 , borderBottom : 1 , borderColor : "divider" } } >
225- < Stack direction = "row" justifyContent = "space-between" alignItems = "center" >
226- < Stack direction = "row" spacing = { 1 } alignItems = "center" >
227- { selectedTab === "forms" ? < DescriptionIcon /> : < ArchiveIcon /> }
228- < Typography variant = "h6" > { selectedTab === "forms" ? Locale . label ( "forms.formsPage.forms" ) : Locale . label ( "forms.formsPage.archForms" ) } </ Typography >
229- </ Stack >
230- < Typography variant = "body2" color = "text.secondary" >
231- { selectedTab === "forms"
232- ? `${ forms . data ?. length || 0 } ${ forms . data ?. length === 1 ? "form" : "forms" } `
233- : `${ archivedForms . data ?. length || 0 } archived ${ archivedForms . data ?. length === 1 ? "form" : "forms" } ` }
234- </ Typography >
235- </ Stack >
236- </ Box >
237-
238- { /* Card Content */ }
239- < Box sx = { { p : 0 } } > { contents } </ Box >
240- </ Card >
231+ < SmartTabs tabs = { tabs } value = { selectedTab } onChange = { setSelectedTab } ariaLabel = "forms-tabs" />
241232 </ Box >
242233 </ >
243234 ) ;
0 commit comments