diff --git a/README.md b/README.md index 05bc534..f460654 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ This repository should be fairly simple to get running locally if you have exper - once you have navigated passed the first page, there should be a 'previous' button - once you have reached the last page, two things should happen - the 'next' button should disappear or disable - - the table should contain the remaining player records (there should be 96 records in the file, so 6 results are expected) + - the table should contain the remaining player records (there should be 95 records in the file, so 5 results are expected) - clicking on a row in the table should show that players data in a summary view somewhere on the page - finally: submit a **pull request** diff --git a/src/App.js b/src/App.js index 265a516..5b34c32 100644 --- a/src/App.js +++ b/src/App.js @@ -1,17 +1,85 @@ -import React from 'react'; +import React, { useState, useMemo } from 'react'; import players from './_players.json'; +import { PLAYER_TABLE_DISPLAY_LIMIT, playersTableColumns } from './players.constants.js' +import { paginatePlayerTableItems } from './players.utils.js' const App = () => { + const [displayIndex, setDisplayIndex] = useState(0); + const [playerSummary, setPlayerSummary] = useState(null); + + // Memoized to prevent re-running calculation when selecting a player summary to view + const playersToDisplay = useMemo( + () => paginatePlayerTableItems(players, displayIndex, PLAYER_TABLE_DISPLAY_LIMIT), + [displayIndex] + ); + + const isFirstPage = displayIndex === 0; + const isLastPage = displayIndex >= players.length - PLAYER_TABLE_DISPLAY_LIMIT; + return (
+
+

Players Table

+

{`Displaying ${displayIndex + 1}-${isLastPage ? players.length : displayIndex + PLAYER_TABLE_DISPLAY_LIMIT} of ${players.length} players`}

+
+ + - + - - + {playersTableColumns.map(header => )} + + + {playersToDisplay.map(player => { + return ( + setPlayerSummary(player)}> + {playersTableColumns.map(header => { + return header.key === 'picture' ? + : + + })} + + ) + })}
helloworld{header.display}
player{player[header.key]}
+
+

Player Summary

+ +
+ {playerSummary ? + <> +
+ player +
+

{`${playerSummary.nameFirst} ${playerSummary.nameLast} -- ${playerSummary.organization}`}

+

{`${playerSummary.nameFirst} is ${playerSummary.age}-year-old, ${playerSummary.handedness.toLowerCase()} handed, and plays the ${playerSummary.position.toLowerCase()} position.`}

+
+
+
+

Contact

+

{`Phone: ${playerSummary.phone}`}

+

{`Email: ${playerSummary.email}`}

+
+ + : +

--no player selected--

+ } +
+
); } diff --git a/src/players.constants.js b/src/players.constants.js new file mode 100644 index 0000000..f2137c2 --- /dev/null +++ b/src/players.constants.js @@ -0,0 +1,36 @@ +export const PLAYER_TABLE_DISPLAY_LIMIT = 10 + +export const playersTableColumns = [ + { + display: '', + key: 'picture', + }, + { + display: 'First', + key: 'nameFirst', + }, + { + display: 'Last', + key: 'nameLast', + }, + { + display: 'Organization', + key: 'organization', + }, + { + display: 'Position', + key: 'position', + }, + { + display: 'Handedness', + key: 'handedness', + }, + { + display: 'Email', + key: 'email', + }, + { + display: 'Phone', + key: 'phone', + } +] \ No newline at end of file diff --git a/src/players.utils.js b/src/players.utils.js new file mode 100644 index 0000000..e08827b --- /dev/null +++ b/src/players.utils.js @@ -0,0 +1,3 @@ +export const paginatePlayerTableItems = (totalItems, startingIndex, displayAmount) => { + return totalItems.slice(startingIndex, startingIndex + displayAmount) +} \ No newline at end of file