Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Journeyers Table #34

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"react-helmet": "^6.1.0",
"react-icons": "^3.10.0",
"react-slick": "^0.27.0",
"react-tooltip": "^4.2.8",
"slick-carousel": "^1.8.1"
},
"devDependencies": {
Expand Down
158 changes: 158 additions & 0 deletions src/components/JourneyersTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React from 'react';
import ProfileBadge from './ProfileBadge/ProfileBadge';
import ProgressBar from './ProgressBar/ProgressBar';
import { Link } from 'gatsby';
import ReactTooltip from 'react-tooltip';

const useSortableData = (entries, config = null) => {
const [sortConfig, setSortConfig] = React.useState(config);

const sortedEntries = React.useMemo(() => {
let sortableEntries = [...entries];
if (sortConfig !== null) {
sortableEntries.sort((a, b) => {
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === 'ascending' ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === 'ascending' ? 1 : -1;
}
return 0;
});
}
return sortableEntries;
}, [entries, sortConfig]);

const requestSort = (key) => {
let direction = 'ascending';
if (
sortConfig &&
sortConfig.key === key &&
sortConfig.direction === 'ascending'
) {
direction = 'descending';
}
setSortConfig({ key, direction });
};

return { entries: sortedEntries, requestSort, sortConfig };
};

const pluralizeDays = (number) => {
if (number === 1) {
return '1 day';
}
return number + ' days';
};

const JTable = (props) => {
const { entries, requestSort, sortConfig } = useSortableData(props.users);
const getClassNamesFor = (name) => {
if (!sortConfig) {
return;
}
return sortConfig.key === name ? sortConfig.direction : undefined;
};

return (
<div className="container">
<table className="table-auto w-5/6 divide-y divide-gray-200 ">
<thead className="text-center">
<tr>
<th className="jtable-header">
<button
type="button"
onClick={() => requestSort('full_name')}
className={getClassNamesFor('full_name')}
>
Name
</button>
</th>

<th className="jtable-header">
<button
type="button"
onClick={() => requestSort('github_username')}
className={getClassNamesFor('github_username')}
>
Github Username
</button>
</th>
<th className="jtable-header">
<button
type="button"
onClick={() => requestSort('twitter_username')}
className={getClassNamesFor('twitter_username')}
>
Twitter Username
</button>
</th>

<th className="jtable-header">
<button
type="button"
onClick={() => requestSort('days_completed')}
className={getClassNamesFor('days_completed')}
>
Days Completed
</button>
</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200 text-center">
{entries.map((user) => (
<tr key={user.full_name}>
<td>
<ProfileBadge
name={user.full_name}
handle=""
avatar={user.avatar_image}
contactDirection="right"
size="small"
shadow="false"
padding="false"
justify="left"
/>
</td>
<td>
<Link
to={'https://github.com/' + user.github_username } >
{user.github_username}
</Link>
</td>
<td>
<Link
to={ 'https://twitter.com/' + user.twitter_username } >
@{user.twitter_username}
</Link>
</td>

<td>
<a data-tip data-for={user.full_name}>
<ProgressBar
key={user.full_name}
bgcolor="#00695c"
completed={Math.min(
user.days_completed,
100
)}
/>
</a>
<ReactTooltip id={user.full_name} type="info">
<span>
{' '}
{pluralizeDays(
user.days_completed
)}{' '}
</span>
</ReactTooltip>
</td>
</tr>
))}
</tbody>
</table>
</div>
);
};

export default JTable;
14 changes: 11 additions & 3 deletions src/components/ProfileBadge/ProfileBadge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const ProfileContent = (props, contactDirection) => {
let cardSize = 'max-w-lg';
let cardClass;
let cardPadding = "p-4";
let cardShadow = "shadow";
let cardShadow = "shadow";
let cardJustifyContent = "justify-center";

if (typeof props['size'!='undefined']){
if (props.size === 'small') {
Expand All @@ -30,12 +31,19 @@ const ProfileContent = (props, contactDirection) => {
if (props.shadow==='false'){
cardShadow = "";
}
}
}

if (typeof props['justify'] != 'undefined') {
if (props.justify==='left'){
cardJustifyContent = "justify-left";
}
}




cardClass =
'flex flex-shrink items-center justify-center rounded-lg w-full '+cardPadding+' '+cardShadow+' '+cardSize;
'flex flex-shrink items-center rounded-lg w-full '+cardJustifyContent+' '+cardPadding+' '+cardShadow+' '+cardSize;

if (contactDirection === 'left') {
return (
Expand Down
37 changes: 37 additions & 0 deletions src/components/ProgressBar/ProgressBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";

const ProgressBar = (props) => {
const { bgcolor, completed } = props;

const containerStyles = {
height: 20,
width: '100%',
backgroundColor: "#e0e0de",
borderRadius: 50,
margin: 50
}

const fillerStyles = {
height: '100%',
width: `${completed}%`,
backgroundColor: bgcolor,
borderRadius: 'inherit',
textAlign: 'right'
}

const labelStyles = {
padding: 5,
color: 'white',
fontWeight: 'bold'
}

return (
<div style={containerStyles}>
<div style={fillerStyles}>
<span style={labelStyles}>{`${completed}%`}</span>
</div>
</div>
);
};

export default ProgressBar;
41 changes: 41 additions & 0 deletions src/pages/journeyers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';

import Header from '../components/Header';
import Footer from '../components/Footer';
import JTable from '../components/JourneyersTable';
import data from '../data/users2.json';
import '../styles.css';

export default function TablePage() {
const [count, setCount] = React.useState(0);
const numberOfUsers = data['users'].length;
const users = data['users'].slice(count, count + 10);

return (
<div className="max-w-8xl m-auto">
<Header />

<div className="site">
<div className="site-content">
<JTable users={users} />


<div className="show-more">
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full"
onClick={() =>
setCount((count + 10) % numberOfUsers)
}
>
show more
</button>


</div>
</div>

<Footer />
</div>
</div>
);
}
16 changes: 16 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ li {
}


.container {
@apply flex justify-center items-center min-h-screen;
}


.jtable-header{
@apply px-6 py-3 border-b border-gray-200 text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider;
}


.show-more{
@apply flex items-center justify-center;
}



@tailwind utilities;