Skip to content

Commit

Permalink
feat: trainee component
Browse files Browse the repository at this point in the history
  • Loading branch information
amrsalama committed Jan 30, 2020
1 parent a3dd54a commit fafdab4
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
Expand Down
22 changes: 21 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import React from 'react';
import './App.css';
import { Trainee } from './Components';

const App = () => {
return <div className="App" />;
const trainees = [
{ name: 'Ahmed Hossam', handle: 'ahmed.7oskaa' },
{ name: 'Abdalla Tarek', handle: 'Bodipie' },
{ name: 'Yousab Esaa', handle: 'Yousab_Esaa' },
{ name: 'Abdalla Nasser', handle: 'abdallanaser1432000' },
{ name: 'Mahmoud Aboelsoud', handle: 'aboelsoudJr' },
{ name: 'Abdelrehman Mamdouh', handle: 'Abdelrehman' },
{ name: 'Mahmoud Elsayed', handle: 'Mahmoudelsayed00' },
{ name: 'Yousef Mohamed', handle: 'Pixelise' },
{ name: 'Hadir Alnajdy', handle: 'Hdrnjd' },
{ name: 'Lama Salah', handle: 'LamaSalah' }
];

return (
<div className="App">
{trainees.map(({ name, handle, photo }) => (
<Trainee key={handle} name={name} handle={handle} />
))}
</div>
);
};

export default App;
42 changes: 42 additions & 0 deletions src/Components/Trainee/Trainee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import PropTypes from 'prop-types';
import { getShortName, getUniqueHashFromName } from '../../utils';
import './styles.css';

class Trainee extends React.Component {
render() {
const { name, handle } = this.props;
const shortName = getShortName(name);
const colorPercent = getUniqueHashFromName(name);

return (
<div className="trainee">
<div
className="photo"
style={{ backgroundColor: `hsl(${colorPercent * 360}, 100%, 75%)` }}
>
{shortName}
</div>
<div className="info">
<div className="name">{name.trim()}</div>
<div className="handle">
<a
href={`https://codeforces.com/profile/${handle}`}
rel="noopener noreferrer"
target="_blank"
>
@{handle.trim()}
</a>
</div>
</div>
</div>
);
}
}

Trainee.propTypes = {
name: PropTypes.string.isRequired,
handle: PropTypes.string.isRequired
};

export default Trainee;
1 change: 1 addition & 0 deletions src/Components/Trainee/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Trainee } from './Trainee';
39 changes: 39 additions & 0 deletions src/Components/Trainee/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.trainee {
display: flex;
flex-direction: row;
font-size: 18px;
}

.trainee .photo {
display: flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
margin: 10px;
border-radius: 5px;
user-select: none;
}

.trainee .info {
display: flex;
flex-direction: column;
justify-content: center;
}

.trainee .info .handle {
font-size: 12px;
}

.trainee .info .handle > a {
color: grey;
text-decoration: none;
}

.trainee .info .handle > a:hover {
color: darkgray;
}

.trainee .info .handle > a:active {
color: black;
}
1 change: 1 addition & 0 deletions src/Components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Trainee';
1 change: 1 addition & 0 deletions src/consts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as primes } from './primes';
28 changes: 28 additions & 0 deletions src/consts/primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default [
2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101
];
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './names';
30 changes: 30 additions & 0 deletions src/utils/names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { primes } from '../consts';

export const getShortName = name => {
return name
.trim()
.split(' ')
.reduce((acc, name) => (name.length ? `${acc}${name[0]}` : acc), '')
.toUpperCase();
};

export const getUniqueHashFromName = name => {
const chars = name
.trim()
.toUpperCase()
.split(' ')
.join('')
.split('');

let rand = 0;
for (let i = 0; i < chars.length; i++) {
const index = chars[i].charCodeAt() - 'A'.charCodeAt();
rand += Math.pow(primes[index], chars.length - i);
}

while (rand > 1) {
rand /= 10;
}

return rand;
};

0 comments on commit fafdab4

Please sign in to comment.