Extract APIs to their own files #108
							
								
								
									
										25
									
								
								app/api/groups.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								app/api/groups.tsx
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | |||||||
|  | import { Group } from '@/app/lib/definitions'; | ||||||
|  | 
 | ||||||
|  | export function loadGroups(onLoad?: (groups: Group[]) => void) { | ||||||
|  |   fetch("/api/groups.json") | ||||||
|  |     .then((response) => response.json()) | ||||||
|  |     .then((data) => { | ||||||
|  |       onLoad && onLoad(data.map((record: any) => { | ||||||
|  |         return ({ | ||||||
|  |           id: record.id, | ||||||
|  |           name: record.name, | ||||||
|  |           color: record.color, | ||||||
|  |           attendance: { | ||||||
|  |             considered: record.considered, | ||||||
|  |             invited: record.invited, | ||||||
|  |             confirmed: record.confirmed, | ||||||
|  |             tentative: record.tentative, | ||||||
|  |             declined: record.declined, | ||||||
|  |             total: record.total, | ||||||
|  |           } | ||||||
|  |         }); | ||||||
|  |       })); | ||||||
|  |     }, (error) => { | ||||||
|  |       return []; | ||||||
|  |     }); | ||||||
|  | } | ||||||
| @ -11,33 +11,9 @@ import GuestsTable from '@/app/ui/guests/table'; | |||||||
| import { TabPanel, TabView } from 'primereact/tabview'; | import { TabPanel, TabView } from 'primereact/tabview'; | ||||||
| import { Suspense, useState } from 'react'; | import { Suspense, useState } from 'react'; | ||||||
| import { loadGuests, updateGuestStatus } from '@/app/api/guests'; | import { loadGuests, updateGuestStatus } from '@/app/api/guests'; | ||||||
|  | import { loadGroups } from '@/app/api/groups'; | ||||||
| 
 | 
 | ||||||
| export default function Page() { | export default function Page() { | ||||||
|   function loadGroups() { |  | ||||||
|     fetch("/api/groups") |  | ||||||
|       .then((response) => response.json()) |  | ||||||
|       .then((data) => { |  | ||||||
|         setGroups(data.map((record: any) => { |  | ||||||
|           return ({ |  | ||||||
|             id: record.id, |  | ||||||
|             name: record.name, |  | ||||||
|             color: record.color, |  | ||||||
|             attendance: { |  | ||||||
|               considered: record.considered, |  | ||||||
|               invited: record.invited, |  | ||||||
|               confirmed: record.confirmed, |  | ||||||
|               tentative: record.tentative, |  | ||||||
|               declined: record.declined, |  | ||||||
|               total: record.total, |  | ||||||
|             } |  | ||||||
|           }); |  | ||||||
|         })); |  | ||||||
|         setGroupsLoaded(true); |  | ||||||
|       }, (error) => { |  | ||||||
|         return []; |  | ||||||
|       }); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   function refreshGuests() { |   function refreshGuests() { | ||||||
|     loadGuests((guests) => { |     loadGuests((guests) => { | ||||||
|       setGuests(guests); |       setGuests(guests); | ||||||
| @ -45,13 +21,20 @@ export default function Page() { | |||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   function refreshGroups() { | ||||||
|  |     loadGroups((groups) => { | ||||||
|  |       setGroups(groups); | ||||||
|  |       setGroupsLoaded(true); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   const [groupsLoaded, setGroupsLoaded] = useState(false); |   const [groupsLoaded, setGroupsLoaded] = useState(false); | ||||||
|   const [groups, setGroups] = useState<Array<Group>>([]); |   const [groups, setGroups] = useState<Array<Group>>([]); | ||||||
| 
 | 
 | ||||||
|   const [guestsLoaded, setGuestsLoaded] = useState(false); |   const [guestsLoaded, setGuestsLoaded] = useState(false); | ||||||
|   const [guests, setGuests] = useState<Array<Guest>>([]); |   const [guests, setGuests] = useState<Array<Guest>>([]); | ||||||
| 
 | 
 | ||||||
|   !groupsLoaded && loadGroups(); |   !groupsLoaded && refreshGroups(); | ||||||
|   !guestsLoaded && refreshGuests(); |   !guestsLoaded && refreshGuests(); | ||||||
| 
 | 
 | ||||||
|   return ( |   return ( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user