Skip to content

Commit 9d1eb81

Browse files
committed
MP-355 re-factor rating chart to a hook
1 parent 5e6af33 commit 9d1eb81

File tree

18 files changed

+113
-334
lines changed

18 files changed

+113
-334
lines changed

src/apps/profiles/src/components/AssemblyDetailsModal/AssemblyDetailsModal.tsx

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
1+
import { Dispatch, FC, SetStateAction, useState } from 'react'
22
import Highcharts from 'highcharts'
33
import HighchartsReact from 'highcharts-react-official'
44

55
import { BaseModal, LoadingSpinner } from '~/libs/ui'
66
import {
77
MemberStats,
88
ratingToCSScolor,
9-
StatsHistory,
109
UserProfile,
1110
UserStatsDistributionResponse,
1211
UserStatsHistory,
@@ -15,9 +14,8 @@ import {
1514
} from '~/libs/core'
1615

1716
import { numberToFixed } from '../../lib'
18-
import { useRatingDistroOptions } from '../../hooks'
17+
import { useRatingDistroOptions, useRatingHistoryOptions } from '../../hooks'
1918

20-
import { RATING_CHART_CONFIG } from './chart-configs'
2119
import styles from './AssemblyDetailsModal.module.scss'
2220

2321
type SRMViewTypes = 'STATISTICS' | 'CHALLENGES DETAILS'
@@ -34,27 +32,11 @@ const AssemblyDetailsModal: FC<AssemblyDetailsModalProps> = (props: AssemblyDeta
3432

3533
const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle)
3634

37-
const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => {
38-
const assemblyHistory: Array<StatsHistory> | undefined
39-
= statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'ASSEMBLY_COMPETITION')?.history
40-
|| []
41-
const options: Highcharts.Options = RATING_CHART_CONFIG
42-
43-
if (!assemblyHistory.length) return undefined
44-
45-
options.series = [{
46-
data: assemblyHistory.sort((a, b) => b.date - a.date)
47-
.map((assemblyChallenge: StatsHistory) => ({
48-
name: assemblyChallenge.challengeName,
49-
x: assemblyChallenge.ratingDate,
50-
y: assemblyChallenge.newRating,
51-
})),
52-
name: 'Assembly Competition Rating',
53-
type: 'spline',
54-
}]
55-
56-
return options
57-
}, [statsHistory])
35+
const ratingHistoryOptions: Highcharts.Options | undefined
36+
= useRatingHistoryOptions(
37+
statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'ASSEMBLY_COMPETITION')?.history,
38+
'Assembly Competition Rating',
39+
)
5840

5941
const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({
6042
filter: 'track=DEVELOP&subTrack=ASSEMBLY_COMPETITION',

src/apps/profiles/src/components/AssemblyDetailsModal/chart-configs.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/apps/profiles/src/components/CodeDetailsModal/CodeDetailsModal.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/* eslint-disable complexity */
2-
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
2+
import { Dispatch, FC, SetStateAction, useState } from 'react'
33
import Highcharts from 'highcharts'
44
import HighchartsReact from 'highcharts-react-official'
55

66
import { BaseModal, LoadingSpinner } from '~/libs/ui'
77
import {
88
MemberStats,
9-
StatsHistory,
109
UserProfile,
1110
UserStatsDistributionResponse,
1211
UserStatsHistory,
@@ -15,9 +14,8 @@ import {
1514
} from '~/libs/core'
1615

1716
import { numberToFixed } from '../../lib'
18-
import { useRatingDistroOptions } from '../../hooks'
17+
import { useRatingDistroOptions, useRatingHistoryOptions } from '../../hooks'
1918

20-
import { RATING_CHART_CONFIG } from './chart-configs'
2119
import styles from './CodeDetailsModal.module.scss'
2220

2321
type CodeViewTypes = 'STATISTICS' | 'CHALLENGES DETAILS'
@@ -34,24 +32,11 @@ const CodeDetailsModal: FC<CodeDetailsModalProps> = (props: CodeDetailsModalProp
3432

3533
const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle)
3634

37-
const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => {
38-
const codeHistory: Array<StatsHistory> | undefined
39-
= statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'CODE')?.history
40-
const options: Highcharts.Options = RATING_CHART_CONFIG
41-
42-
if (!codeHistory?.length) return undefined
43-
44-
options.series = [{
45-
data: codeHistory.sort((a, b) => b.date - a.date)
46-
.map((testChallenge: StatsHistory) => ({
47-
name: testChallenge.challengeName, x: testChallenge.ratingDate, y: testChallenge.newRating,
48-
})),
49-
name: 'Code Rating',
50-
type: 'spline',
51-
}]
52-
53-
return options
54-
}, [statsHistory])
35+
const ratingHistoryOptions: Highcharts.Options | undefined
36+
= useRatingHistoryOptions(
37+
statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'CODE')?.history,
38+
'Code Rating',
39+
)
5540

5641
const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({
5742
filter: 'track=DEVELOP&subTrack=CODE',

src/apps/profiles/src/components/CodeDetailsModal/chart-configs.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/apps/profiles/src/components/ContentCreationDetailsModal/ContentCreationDetailsModal.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/* eslint-disable complexity */
2-
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
2+
import { Dispatch, FC, SetStateAction, useState } from 'react'
33
import Highcharts from 'highcharts'
44
import HighchartsReact from 'highcharts-react-official'
55

66
import { BaseModal, LoadingSpinner } from '~/libs/ui'
77
import {
88
MemberStats,
99
ratingToCSScolor,
10-
StatsHistory,
1110
UserProfile,
1211
UserStatsDistributionResponse,
1312
UserStatsHistory,
@@ -16,9 +15,8 @@ import {
1615
} from '~/libs/core'
1716

1817
import { numberToFixed } from '../../lib'
19-
import { useRatingDistroOptions } from '../../hooks'
18+
import { useRatingDistroOptions, useRatingHistoryOptions } from '../../hooks'
2019

21-
import { RATING_CHART_CONFIG } from './chart-configs'
2220
import styles from './ContentCreationDetailsModal.module.scss'
2321

2422
type TestScenViewTypes = 'STATISTICS' | 'CHALLENGES DETAILS'
@@ -35,24 +33,11 @@ const ContentCreationDetailsModal: FC<ContentCreationDetailsModalProps> = (props
3533

3634
const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle)
3735

38-
const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => {
39-
const contentCreationHistory: Array<StatsHistory> | undefined
40-
= statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'CONTENT_CREATION')?.history
41-
const options: Highcharts.Options = RATING_CHART_CONFIG
42-
43-
if (!contentCreationHistory?.length) return undefined
44-
45-
options.series = [{
46-
data: contentCreationHistory.sort((a, b) => b.date - a.date)
47-
.map((testChallenge: StatsHistory) => ({
48-
name: testChallenge.challengeName, x: testChallenge.ratingDate, y: testChallenge.newRating,
49-
})),
50-
name: 'Content Creation Rating',
51-
type: 'spline',
52-
}]
53-
54-
return options
55-
}, [statsHistory])
36+
const ratingHistoryOptions: Highcharts.Options | undefined
37+
= useRatingHistoryOptions(
38+
statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'CONTENT_CREATION')?.history,
39+
'Content Creation Rating',
40+
)
5641

5742
const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({
5843
filter: 'track=DEVELOP&subTrack=CONTENT_CREATION',

src/apps/profiles/src/components/ContentCreationDetailsModal/chart-configs.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/apps/profiles/src/components/MMDetailsModal/MMDetailsModal.tsx

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
/* eslint-disable complexity */
2-
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
2+
import { Dispatch, FC, SetStateAction, useState } from 'react'
33
import Highcharts from 'highcharts'
44
import HighchartsReact from 'highcharts-react-official'
55

66
import { BaseModal, LoadingSpinner } from '~/libs/ui'
77
import {
88
MemberStats,
99
ratingToCSScolor,
10-
StatsHistory,
1110
UserProfile,
1211
UserStatsDistributionResponse,
1312
UserStatsHistory,
1413
useStatsDistribution,
1514
useStatsHistory,
1615
} from '~/libs/core'
1716

18-
import { useRatingDistroOptions } from '../../hooks'
17+
import { useRatingDistroOptions, useRatingHistoryOptions } from '../../hooks'
1918

20-
import { RATING_CHART_CONFIG } from './chart-configs'
2119
import styles from './MMDetailsModal.module.scss'
2220

2321
type SRMViewTypes = 'STATISTICS' | 'MATCH DETAILS'
@@ -34,21 +32,13 @@ const MMDetailsModal: FC<MMDetailsModalProps> = (props: MMDetailsModalProps) =>
3432

3533
const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle)
3634

37-
const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => {
38-
const mmHistory: Array<StatsHistory> = statsHistory?.DATA_SCIENCE?.MARATHON_MATCH?.history || []
39-
const options: Highcharts.Options = RATING_CHART_CONFIG
40-
41-
if (!mmHistory.length) return undefined
42-
43-
options.series = [{
44-
data: mmHistory.sort((a, b) => b.date - a.date)
45-
.map((srm: StatsHistory) => ({ name: srm.challengeName, x: srm.date, y: srm.rating })),
46-
name: 'Marathon Match Rating',
47-
type: 'spline',
48-
}]
49-
50-
return options
51-
}, [statsHistory])
35+
const ratingHistoryOptions: Highcharts.Options | undefined
36+
= useRatingHistoryOptions(
37+
statsHistory?.DATA_SCIENCE?.MARATHON_MATCH?.history,
38+
'Marathon Match Rating',
39+
'date',
40+
'rating',
41+
)
5242

5343
const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({
5444
filter: 'track=DATA_SCIENCE&subTrack=MARATHON_MATCH',

src/apps/profiles/src/components/MMDetailsModal/chart-configs.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/apps/profiles/src/components/SRMDetailsModal/SRMDetailsModal.tsx

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable complexity */
2-
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
2+
import { Dispatch, FC, SetStateAction, useState } from 'react'
33
import { bind } from 'lodash'
44
import AnnotationsModule from 'highcharts/modules/annotations'
55
import Highcharts from 'highcharts'
@@ -9,7 +9,6 @@ import { BaseModal, Button, LoadingSpinner } from '~/libs/ui'
99
import {
1010
ratingToCSScolor,
1111
SRMStats,
12-
StatsHistory,
1312
UserProfile,
1413
UserStatsDistributionResponse,
1514
UserStatsHistory,
@@ -19,9 +18,8 @@ import {
1918

2019
import { ChallengesGrid } from '../ChallengesGrid'
2120
import { DivisionGrid } from '../DivisionGrid'
22-
import { useRatingDistroOptions } from '../../hooks'
21+
import { useRatingDistroOptions, useRatingHistoryOptions } from '../../hooks'
2322

24-
import { RATING_CHART_CONFIG } from './chart-configs'
2523
import styles from './SRMDetailsModal.module.scss'
2624

2725
type SRMViewTypes = 'STATISTICS' | 'SRM DETAILS' | 'PAST SRM'
@@ -40,21 +38,13 @@ const SRMDetailsModal: FC<SRMDetailsModalProps> = (props: SRMDetailsModalProps)
4038

4139
const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle)
4240

43-
const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => {
44-
const srmHistory: Array<StatsHistory> = statsHistory?.DATA_SCIENCE?.SRM?.history || []
45-
const options: Highcharts.Options = RATING_CHART_CONFIG
46-
47-
if (!srmHistory.length) return undefined
48-
49-
options.series = [{
50-
data: srmHistory.sort((a, b) => b.date - a.date)
51-
.map((srm: StatsHistory) => ({ name: srm.challengeName, x: srm.date, y: srm.rating })),
52-
name: 'SRM Rating',
53-
type: 'spline',
54-
}]
55-
56-
return options
57-
}, [statsHistory])
41+
const ratingHistoryOptions: Highcharts.Options | undefined
42+
= useRatingHistoryOptions(
43+
statsHistory?.DATA_SCIENCE?.SRM?.history,
44+
'SRM Rating',
45+
'date',
46+
'rating',
47+
)
5848

5949
const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({
6050
filter: 'track=DATA_SCIENCE&subTrack=SRM',

0 commit comments

Comments
 (0)