Skip to content

Commit

Permalink
refactor: move sortByMode to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shootermv committed Oct 10, 2023
1 parent 91060ba commit 9d6db5f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/pages/components/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const bySeverityHandler = (a: HourlyData, b: HourlyData) => {
return b.planned_rides - a.planned_rides
}

export const sortByMode = (hourlyData: HourlyData[] = [], sortingMode: string) => {
return hourlyData.toSorted(sortingMode === 'hour' ? byHourHandler : bySeverityHandler)
}

export const mapColorByExecution = (planned: number, actual: number) => {
const misses = planned - actual
const percentageMisses = (misses / planned) * 100
Expand Down
24 changes: 7 additions & 17 deletions src/pages/useGapsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@ import { useEffect, useState } from 'react'
import { Moment } from 'moment'
import { getGapsAsync } from '../api/gapsService'

import { HourlyData, byHourHandler, bySeverityHandler } from './components/utils'
import { sortByMode, HourlyData } from './components/utils'
import { GapsList } from 'src/model/gaps'

export const useGapsList = (
fromDate: Moment,
toDate: Moment,
operatorRef: string,
lineRef: number,
sortingMode: string,
) => {
): HourlyData[] => {
const [hourlyData, setHourlyData] = useState<HourlyData[]>([])

const sortData = (hourlyData: HourlyData[] = [], sortingMode: string) => {
const orderedData = [...hourlyData]
if (sortingMode === 'hour') {
orderedData.sort(byHourHandler)
} else if (sortingMode === 'severity') {
orderedData.sort(bySeverityHandler)
}
return orderedData
}
useEffect(() => {
const fetchData = async () => {
try {
const gapsList = await getGapsAsync(fromDate, toDate, operatorRef, lineRef)
const gapsList: GapsList = await getGapsAsync(fromDate, toDate, operatorRef, lineRef)

// Convert gapsList data into hourly mapping as needed
// Convert gapsList data to hourly mapping structure, where hour is a key
const hourlyMapping: Record<string, { planned_rides: number; actual_rides: number }> = {}

for (const ride of gapsList) {
Expand All @@ -50,11 +42,9 @@ export const useGapsList = (
planned_hour: hour,
actual_rides: data.actual_rides,
planned_rides: data.planned_rides,
}))

const orderedData = sortData(result, sortingMode)
}))

Check failure on line 45 in src/pages/useGapsList.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

setHourlyData(orderedData)
setHourlyData(sortByMode(result, sortingMode))
} catch (error) {
console.error('Error fetching data:', error)
}
Expand Down

0 comments on commit 9d6db5f

Please sign in to comment.