1- import React from "react" ;
1+ import React , { useState } from "react" ;
22import PropTypes from "prop-types" ;
33import Loading from "../loading/Loading" ;
44import SkillCard from "./SkillCard" ;
@@ -8,8 +8,8 @@ import styles from "./SkillsSection.module.scss";
88import {
99 Skill ,
1010 SkillSortComparator ,
11- COMPARATORS ,
12- DEFAULT_COMPARATOR
11+ SkillComparatorDisplayNames ,
12+ COMPARATORS_MAP
1313} from "./skillsUtils" ;
1414import { Error } from "../error/errorUtils" ;
1515
@@ -19,46 +19,18 @@ interface Props {
1919 skills : Skill [ ] ;
2020}
2121
22- interface State {
23- sortComparator : SkillSortComparator ;
24- }
25-
2622function getSkillCards ( skills : Skill [ ] ) : JSX . Element [ ] {
2723 return skills . map ( skill => < SkillCard skill = { skill } key = { skill . name } /> ) ;
2824}
2925
30- export default class SkillsSection extends React . Component < Props , State > {
31- static propTypes = {
32- loading : PropTypes . bool ,
33- error : PropTypes . shape ( {
34- message : PropTypes . string
35- } ) ,
36- skills : PropTypes . arrayOf (
37- PropTypes . shape ( {
38- name : PropTypes . string . isRequired ,
39- displayName : PropTypes . string . isRequired ,
40- repoCount : PropTypes . number . isRequired
41- } )
42- ) . isRequired
43- } ;
44-
45- static defaultProps = {
46- loading : false ,
47- error : undefined
48- } ;
49-
50- constructor ( props : Props ) {
51- super ( props ) ;
26+ export default function SkillsSection ( props : Props ) : JSX . Element {
27+ const { loading, error, skills } = props ;
5228
53- this . state = {
54- sortComparator : DEFAULT_COMPARATOR
55- } ;
56- }
57-
58- getContent = ( ) : JSX . Element | JSX . Element [ ] => {
59- const { loading, error, skills } = this . props ;
60- const { sortComparator } = this . state ;
29+ const [ currentSort , setCurrentSort ] = useState (
30+ SkillComparatorDisplayNames . DATE
31+ ) ;
6132
33+ function getContent ( ) : JSX . Element | JSX . Element [ ] {
6234 if ( loading ) {
6335 return < Loading /> ;
6436 }
@@ -73,32 +45,53 @@ export default class SkillsSection extends React.Component<Props, State> {
7345 ) ;
7446 }
7547
48+ const sortComparator = COMPARATORS_MAP . get (
49+ currentSort
50+ ) as SkillSortComparator ;
51+
7652 const sortedSkills = skills . sort ( sortComparator ) ;
7753
7854 return getSkillCards ( sortedSkills ) ;
79- } ;
80-
81- handleSort = ( event : React . ChangeEvent < HTMLSelectElement > ) : void => {
82- const sortType = event . target . value ;
83- const sortComparator = COMPARATORS . get ( sortType ) as SkillSortComparator ;
84-
85- this . setState ( { sortComparator } ) ;
86- } ;
87-
88- render ( ) : JSX . Element {
89- const { loading } = this . props ;
90-
91- return (
92- < section className = { styles . contentSection } >
93- < div className = { styles . container } >
94- < header >
95- < h2 > Skills</ h2 >
55+ }
9656
97- < SkillSortSelector disabled = { loading } onChange = { this . handleSort } />
98- </ header >
99- < div className = { styles . list } > { this . getContent ( ) } </ div >
100- </ div >
101- </ section >
102- ) ;
57+ function handleChangeSkillSort (
58+ event : React . ChangeEvent < HTMLSelectElement >
59+ ) : void {
60+ setCurrentSort ( event . target . value as SkillComparatorDisplayNames ) ;
10361 }
62+
63+ return (
64+ < section className = { styles . contentSection } >
65+ < div className = { styles . container } >
66+ < header >
67+ < h2 > Skills</ h2 >
68+
69+ < SkillSortSelector
70+ disabled = { loading }
71+ onChange = { handleChangeSkillSort }
72+ />
73+ </ header >
74+ < div className = { styles . list } > { getContent ( ) } </ div >
75+ </ div >
76+ </ section >
77+ ) ;
10478}
79+
80+ SkillsSection . propTypes = {
81+ loading : PropTypes . bool ,
82+ error : PropTypes . shape ( {
83+ message : PropTypes . string
84+ } ) ,
85+ skills : PropTypes . arrayOf (
86+ PropTypes . shape ( {
87+ name : PropTypes . string . isRequired ,
88+ displayName : PropTypes . string . isRequired ,
89+ repoCount : PropTypes . number . isRequired
90+ } )
91+ ) . isRequired
92+ } ;
93+
94+ SkillsSection . defaultProps = {
95+ loading : false ,
96+ error : undefined
97+ } ;
0 commit comments