55import { Add as AddIcon , Assignment as AssignmentIcon , CalendarMonth as CalendarIcon , Edit as EditIcon , EventNote as EventNoteIcon } from "@mui/icons-material" ;
66import { Link } from "react-router-dom" ;
77import { type GroupInterface } from "@churchapps/helpers" ;
8+ import { type PlanInterface } from "../../helpers" ;
89import { ArrayHelper , DateHelper , Locale , Loading , UserHelper , Permissions } from "@churchapps/apphelper" ;
910import { PlanEdit } from "./PlanEdit" ;
1011import { MinistryList } from "./MinistryList" ;
@@ -16,32 +17,24 @@ interface Props {
1617 planTypeId ?: string ;
1718}
1819
19- export interface PlanInterface {
20- id ?: string ;
21- churchId ?: string ;
22- name ?: string ;
23- ministryId ?: string ;
24- planTypeId ?: string ;
25- serviceDate ?: Date ;
26- notes ?: string ;
27- serviceOrder ?: boolean ;
28- }
20+
2921
3022export const PlanList = memo ( ( props : Props ) => {
3123 const [ plan , setPlan ] = React . useState < PlanInterface > ( null ) ;
3224 const canEdit = UserHelper . checkAccess ( Permissions . membershipApi . plans . edit ) ;
3325
3426 const plansQuery = useQuery < PlanInterface [ ] > ( {
35- queryKey : [ "/plans" , "DoingApi" ] ,
27+ queryKey : props . planTypeId ? [ `/plans/types/ ${ props . planTypeId } ` , "DoingApi" ] : [ "/plans" , "DoingApi" ] ,
3628 placeholderData : [ ] ,
3729 } ) ;
3830
3931 const plans = React . useMemo ( ( ) => {
40- let filtered = ArrayHelper . getAll ( plansQuery . data || [ ] , "ministryId" , props . ministry . id ) ;
32+ // When planTypeId is provided, the API already returns filtered data
4133 if ( props . planTypeId ) {
42- filtered = ArrayHelper . getAll ( filtered , "planTypeId" , props . planTypeId ) ;
34+ return plansQuery . data || [ ] ;
4335 }
44- return filtered ;
36+ // When no planTypeId, filter by ministry only
37+ return ArrayHelper . getAll ( plansQuery . data || [ ] , "ministryId" , props . ministry . id ) ;
4538 } , [ plansQuery . data , props . ministry . id , props . planTypeId ] ) ;
4639
4740 const addPlan = useCallback ( ( ) => {
@@ -50,18 +43,23 @@ export const PlanList = memo((props: Props) => {
5043 const name = DateHelper . prettyDate ( date ) ;
5144 setPlan ( {
5245 ministryId : props . ministry . id ,
46+ planTypeId : props . planTypeId ,
5347 serviceDate : date ,
5448 name,
5549 notes : "" ,
5650 serviceOrder : true ,
5751 } ) ;
58- } , [ props . ministry . id ] ) ;
52+ } , [ props . ministry . id , props . planTypeId ] ) ;
5953
6054 const handleUpdated = useCallback ( ( ) => {
6155 setPlan ( null ) ;
6256 plansQuery . refetch ( ) ;
57+ // Invalidate both the specific plan type query and the general plans query
58+ if ( props . planTypeId ) {
59+ queryClient . invalidateQueries ( { queryKey : [ `/plans/types/${ props . planTypeId } ` , "DoingApi" ] } ) ;
60+ }
6361 queryClient . invalidateQueries ( { queryKey : [ "/plans" , "DoingApi" ] } ) ;
64- } , [ plansQuery ] ) ;
62+ } , [ plansQuery , props . planTypeId ] ) ;
6563
6664 if ( plan && canEdit ) {
6765 return < PlanEdit plan = { plan } plans = { plans } updatedFunction = { handleUpdated } /> ;
@@ -240,7 +238,7 @@ export const PlanList = memo((props: Props) => {
240238 ) ) }
241239 </ Stack >
242240
243- < MinistryList />
241+ { ! props . planTypeId && < MinistryList /> }
244242 </ Box >
245243 ) ;
246244} ) ;
0 commit comments