generated from ctc-uci/npo-template-merged
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from ctc-uci/35-create-endpoints-7
35 create endpoints 7
- Loading branch information
Showing
5 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { | ||
Table, | ||
Thead, | ||
Tbody, | ||
Tr, | ||
Th, | ||
Td, | ||
Menu, | ||
MenuButton, | ||
MenuList, | ||
MenuItem, | ||
Button, | ||
} from '@chakra-ui/react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { useBackendContext } from "../contexts/hooks/useBackendContext"; | ||
|
||
export const EventsTable = () => { | ||
const { backend } = useBackendContext(); | ||
const [ events, setEvents ] = useState([]); | ||
|
||
const fetchEvents = async () => { | ||
try { | ||
const eventsResponse = await backend.get('/events'); | ||
const eventsData = eventsResponse.data; | ||
const eventsWithAssignments = []; | ||
|
||
for (const event of eventsData) { | ||
const instructors = []; | ||
const payees = []; | ||
|
||
try { | ||
// Attempt to fetch assignment information for each event | ||
const assignmentsResponse = await backend.get('/assignments/event/' + event.id); | ||
const assignmentsData = assignmentsResponse.data; | ||
|
||
// Process assignment information if available | ||
if (Array.isArray(assignmentsData) && assignmentsData.length > 0) { | ||
for (const assignment of assignmentsData) { | ||
if (assignment.role.toLowerCase() === 'instructor') { | ||
instructors.push(assignment.clientName); | ||
} else if (assignment.role.toLowerCase() === 'payee') { | ||
payees.push(assignment.clientName); | ||
} | ||
} | ||
} | ||
} catch { | ||
console.error('Failed to fetch assignments for event', event.id); | ||
} | ||
|
||
// Add event with assignments to list | ||
eventsWithAssignments.push({ | ||
id: event.id, | ||
name: event.name, | ||
description: event.description, | ||
instructors: instructors.join(', '), | ||
payees: payees.join(', ') | ||
}); | ||
} | ||
|
||
setEvents(eventsWithAssignments); | ||
} catch { | ||
console.error('Failed to fetch events'); | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
// call the fetch event here to fetch all events | ||
fetchEvents(); | ||
}, []); | ||
|
||
return ( | ||
<Table variant="striped" colorScheme="teal"> | ||
<Thead> | ||
<Tr> | ||
<Th>Class</Th> | ||
<Th>Description</Th> | ||
<Th>Instructor</Th> | ||
<Th>Payee</Th> | ||
<Th>Actions</Th> | ||
</Tr> | ||
</Thead> | ||
<Tbody> | ||
{events.map((event) => ( | ||
<Tr key={event.id}> | ||
<Td>{event.name}</Td> | ||
<Td>{event.description}</Td> | ||
<Td>{event.instructors}</Td> | ||
<Td>{event.payees}</Td> | ||
<Td> | ||
<Menu> | ||
<MenuButton | ||
as={Button} | ||
size="sm" | ||
variant="outline" | ||
> | ||
Actions | ||
</MenuButton> | ||
<MenuList> | ||
<MenuItem onClick={() => {}}>Edit</MenuItem> | ||
<MenuItem onClick={() => {}}>Delete</MenuItem> | ||
</MenuList> | ||
</Menu> | ||
</Td> | ||
</Tr> | ||
))} | ||
</Tbody> | ||
</Table> | ||
); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters