Skip to content

Pattern frequencies per company/difficulty #113

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

Merged
merged 2 commits into from
Sep 3, 2021
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
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ module.exports = {
rules: {
'jsx-a11y/href-no-hash': ['off'],
'react/jsx-filename-extension': ['warn', { extensions: ['.js', '.jsx'] }],
'react/destructuring-assignment': [
'warn',
'always',
{
ignoreClassFields: false,
},
],
'max-len': [
'warn',
{
Expand Down
74 changes: 74 additions & 0 deletions src/components/PatternFrequencies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Badge } from 'reactstrap';
import React from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import PropTypes from 'prop-types';
import './styles.scss';

const PatternFrequencies = ({ filters, rows }) => {
const patternsMap = rows.reduce((acc, row) => {
for (let i = 0; i < row.original.pattern.length; i += 1) {
const pattern = row.original.pattern[i];
acc[pattern] = acc[pattern] + 1 || 1;
}
return acc;
}, Object.create(null));
const sortedPatternsByFrequency = Object.keys(patternsMap).sort(
(a, b) => patternsMap[b] - patternsMap[a],
);
const showComponent = filters.find(filter =>
['companies', 'difficulty'].includes(filter.id),
);

const getFrequencyClass = rate => {
const highestFrequency = Math.round(
patternsMap[sortedPatternsByFrequency[0]],
);

if (highestFrequency / 3 < 1) {
return '';
}

const frequencyRate = {
easy: Math.round(highestFrequency / 3),
medium: Math.round((highestFrequency / 3) * 2),
hard: highestFrequency,
};

return Object.keys(frequencyRate).find(key => rate <= frequencyRate[key]);
};

return showComponent ? (
<div className="pattern-count">
<h5>Problems pattern frequency</h5>
{sortedPatternsByFrequency.map((pattern, index) => (
<Badge
// eslint-disable-next-line react/no-array-index-key
key={pattern + index}
className={`${getFrequencyClass(patternsMap[pattern])}`}
pill
>
<span
data-tip={`${patternsMap[pattern]} "${pattern}" related problems`}
>
{pattern} : {patternsMap[pattern]}
</span>
</Badge>
))}
</div>
) : null;
};

PatternFrequencies.propTypes = {
filters: PropTypes.arrayOf(
PropTypes.shape({ id: PropTypes.string, value: PropTypes.string }),
).isRequired,
rows: PropTypes.arrayOf(
PropTypes.shape({
original: PropTypes.shape({
pattern: PropTypes.arrayOf(PropTypes.string),
}),
}),
).isRequired,
};

export default PatternFrequencies;
11 changes: 11 additions & 0 deletions src/components/PatternFrequencies/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.pattern-count {
padding: 0.75em;

h5 {
font-weight: bold;
}

.badge {
margin: 0.25em 0.25em;
}
}
4 changes: 4 additions & 0 deletions src/components/Table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import questions from '../../data';

import 'react-toggle/style.css';
import './styles.scss';
import PatternFrequencies from '../PatternFrequencies';

const iconPath = `${process.env.PUBLIC_URL}/assets/icons/`;

Expand Down Expand Up @@ -328,6 +329,8 @@ const Table = () => {
getTableProps,
getTableBodyProps,
headerGroups,
filteredRows,
state: { filters },
rows,
prepareRow,
} = useTable(
Expand All @@ -343,6 +346,7 @@ const Table = () => {
return (
<Container className="table">
<ReactTooltip />
<PatternFrequencies filters={filters} rows={filteredRows} />
<ReactTable borderless striped hover {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
Expand Down