Skip to content

Instantly share code, notes, and snippets.

@DanyF-github
Created January 8, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanyF-github/910e601a59d999d6e308eb8b8049ae27 to your computer and use it in GitHub Desktop.
Save DanyF-github/910e601a59d999d6e308eb8b8049ae27 to your computer and use it in GitHub Desktop.
// client/src/components/Students/StudentsList.tsx
import React from 'react';
import { useQuery } from '@apollo/client';
import { GET_STUDENTS } from '../../data/queries';
import { Student, StudentListProps } from '../../models';
const StudentsList = ({ actions = [] }: StudentListProps) => {
// create the query
const { data, loading, error } = useQuery(GET_STUDENTS);
// render the table
return (
<>
{loading && <p>Loading...</p>}
{error && <p>Error!</p>}
{data && (
<table>
<thead>
<tr>
<th>Phone Number</th>
<th>First Name</th>
<th>Last Name</th>
{actions.length > 0 && <th>Actions</th>}
</tr>
</thead>
<tbody>
{data.students.map((student: Student) => {
return (
<tr key={student.phoneNumber}>
<td>{student.phoneNumber}</td>
<td>{student.firstName}</td>
<td>{student.lastName}</td>
<td>
{actions.map((action) => (
<button key={action.actionName} onClick={() => action.onAction(student)}>
{action.actionName}
</button>
))}
</td>
</tr>
);
})}
</tbody>
</table>
)}
</>
);
};
export default StudentsList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment