1- import { useState , useEffect } from "react" ;
2- import { ApiHelper , UserHelper , Loading , PageHeader , Locale } from "@churchapps/apphelper" ;
1+ import { useState } from "react" ;
2+ import { useQuery } from "@tanstack/react-query" ;
3+ import { UserHelper , Loading , PageHeader , Locale } from "@churchapps/apphelper" ;
34import { Permissions , type CuratedCalendarInterface } from "@churchapps/helpers" ;
45import { Link , useNavigate } from "react-router-dom" ;
56import {
@@ -26,34 +27,22 @@ import {
2627 Description as DescriptionIcon
2728} from "@mui/icons-material" ;
2829import { CalendarEdit } from "./components" ;
29- import { PermissionDenied } from "../components " ;
30+ import { useRequirePermission } from "../hooks " ;
3031import { EmptyState } from "../components/ui/EmptyState" ;
32+ import { hoverRowSx } from "../components/ui/tableStyles" ;
3133import { AppIconButton } from "../components/ui/AppIconButton" ;
3234import { HeaderPrimaryButton } from "../components/ui/headerButtons" ;
3335
3436export const CalendarsPage = ( ) => {
35- const [ calendars , setCalendars ] = useState < CuratedCalendarInterface [ ] > ( [ ] ) ;
37+ const calendarsQuery = useQuery < CuratedCalendarInterface [ ] > ( { queryKey : [ "/curatedCalendars" , "ContentApi" ] , placeholderData : [ ] } ) ;
3638 const [ currentCalendar , setCurrentCalendar ] = useState < CuratedCalendarInterface | null > ( null ) ;
37- const [ loading , setLoading ] = useState ( true ) ;
3839 const navigate = useNavigate ( ) ;
40+ const denied = useRequirePermission ( Permissions . contentApi . content . edit ) ;
3941
40- const loadData = ( ) => {
41- setLoading ( true ) ;
42- ApiHelper . get ( "/curatedCalendars" , "ContentApi" ) . then ( ( data : any ) => {
43- setCalendars ( data ) ;
44- setLoading ( false ) ;
45- } ) . catch ( ( ) => {
46- setLoading ( false ) ;
47- } ) ;
48- } ;
49-
50- const getRows = ( ) => calendars . map ( ( calendar ) => (
42+ const getRows = ( ) => calendarsQuery . data . map ( ( calendar ) => (
5143 < TableRow
5244 key = { calendar . id }
53- sx = { {
54- "&:hover" : { backgroundColor : "action.hover" } ,
55- transition : "background-color 0.2s ease"
56- } }
45+ sx = { hoverRowSx }
5746 >
5847 < TableCell >
5948 < Stack direction = "row" spacing = { 2 } alignItems = "center" >
@@ -117,20 +106,16 @@ export const CalendarsPage = () => {
117106 </ TableRow >
118107 ) ) ;
119108
120- useEffect ( ( ) => {
121- loadData ( ) ;
122- } , [ ] ) ;
123-
124- if ( ! UserHelper . checkAccess ( Permissions . contentApi . content . edit ) ) return < PermissionDenied permissions = { [ Permissions . contentApi . content . edit ] } /> ;
109+ if ( denied ) return denied ;
125110
126111 return (
127112 < >
128113 < PageHeader
129114 icon = { < CalendarIcon /> }
130115 title = { Locale . label ( "calendars.calendarList.title" ) }
131116 subtitle = {
132- calendars . length > 0
133- ? Locale . label ( "calendars.calendarList.subtitleWithCount" , `${ calendars . length } ${ calendars . length === 1 ? Locale . label ( "calendars.calendarList.calendar" ) : Locale . label ( "calendars.calendarList.calendars" ) } ` )
117+ calendarsQuery . data . length > 0
118+ ? Locale . label ( "calendars.calendarList.subtitleWithCount" , `${ calendarsQuery . data . length } ${ calendarsQuery . data . length === 1 ? Locale . label ( "calendars.calendarList.calendar" ) : Locale . label ( "calendars.calendarList.calendars" ) } ` )
134119 : Locale . label ( "calendars.calendarList.subtitleEmpty" )
135120 }
136121 >
@@ -152,15 +137,15 @@ export const CalendarsPage = () => {
152137 calendar = { currentCalendar }
153138 updatedCallback = { ( ) => {
154139 setCurrentCalendar ( null ) ;
155- loadData ( ) ;
140+ calendarsQuery . refetch ( ) ;
156141 } }
157142 />
158143 </ Box >
159144 ) }
160145
161- { loading ? (
146+ { calendarsQuery . isLoading ? (
162147 < Loading data-testid = "calendars-loading" />
163- ) : calendars . length === 0 ? (
148+ ) : calendarsQuery . data . length === 0 ? (
164149 < EmptyState
165150 icon = { < CalendarIcon /> }
166151 title = { Locale . label ( "calendars.calendarList.noCalendars" ) }
@@ -197,7 +182,7 @@ export const CalendarsPage = () => {
197182 </ TableContainer >
198183 ) }
199184
200- { calendars . length > 0 && ! currentCalendar && (
185+ { calendarsQuery . data . length > 0 && ! currentCalendar && (
201186 < Card sx = { { mt : 3 , borderRadius : 2 , border : "1px solid" , borderColor : "divider" } } >
202187 < CardContent >
203188 < Stack direction = "row" spacing = { 2 } alignItems = "flex-start" >
0 commit comments