Skip to content

Commit

Permalink
refactor: improve convert structure logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shootermv committed Oct 15, 2023
1 parent 2086b16 commit 1629d85
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
15 changes: 15 additions & 0 deletions src/api/groupByService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ export type GroupByResponse = {
route_long_name: string
}[]

export type GroupByRes = Replace<
GroupByResponse[0],
'operator_ref',
| {
agency_id: string
agency_name: string
agency_url: string
agency_timezone: string
agency_lang: string
agency_phone: string
agency_fare_url: string
}
| undefined
>

async function groupbyAsync({
dateTo,
dateFrom,
Expand Down
50 changes: 26 additions & 24 deletions src/pages/dashboard/DashboardPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Fragment, useState } from 'react'
import { useGroupBy } from 'src/api/groupByService'
import { GroupByRes, useGroupBy } from 'src/api/groupByService'
import { PageContainer } from '../components/PageContainer'
import OperatorHbarChart from './OperatorHbarChart/OperatorHbarChart'
import './DashboardPage.scss'
Expand All @@ -15,43 +15,45 @@ import { useDate } from '../components/DateTimePicker'
import { Skeleton } from 'antd'
const now = moment()

const convertToChartCompatibleStruct = (arr: GroupByRes[]) => {
return arr.map((item: GroupByRes) => ({
id: item.operator_ref?.agency_id || 'Unknown',
name: item.operator_ref?.agency_name || 'Unknown',
total: item.total_planned_rides,
actual: item.total_actual_rides,
}))
}
const convertToWorstLineChartCompatibleStruct = (arr: GroupByRes[], operatorId: number) => {
return arr

Check failure on line 27 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `(⏎····`
?.filter((row) => +row.operator_ref?.agency_id! === operatorId || !Number(operatorId))

Check failure on line 28 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `····` with `······`

Check failure on line 28 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong
?.map((item: GroupByRes) => ({

Check failure on line 29 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
id: `${item.line_ref}|${item.operator_ref?.agency_id}` || 'Unknown',

Check failure on line 30 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `······` with `········`
operator_name: item.operator_ref?.agency_name || 'Unknown',

Check failure on line 31 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
short_name: JSON.parse(item.route_short_name)[0],

Check failure on line 32 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
long_name: item.route_long_name,

Check failure on line 33 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
total: item.total_planned_rides,

Check failure on line 34 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `··`
actual: item.total_actual_rides,

Check failure on line 35 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `······` with `········`
})) || []
}

const DashboardPage = () => {
const [startDate, setStartDate] = useDate(now.clone().subtract(7, 'days'))
const [endDate, setEndDate] = useDate(now.clone().subtract(1, 'day'))
const [groupByHour, setGroupByHour] = React.useState<boolean>(false)

const [operatorId, setOperatorId] = useState('')
const [groupByOperatorData_, groupByOperatorLoading] = useGroupBy({
const [groupByOperatorData, groupByOperatorLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref',
})
//covert to Operator data to proper structure
const groupByOperatorData = groupByOperatorData_.map((item) => ({
id: item.operator_ref?.agency_id || 'Unknown',
name: item.operator_ref?.agency_name || 'Unknown',
total: item.total_planned_rides,
actual: item.total_actual_rides,
}))

const [groupByLineData_, lineDataLoading] = useGroupBy({
const [groupByLineData, lineDataLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref,line_ref',
})

// covert LineDaata to proper structure
const groupByLineData = groupByLineData_
?.filter((row) => row.operator_ref?.agency_id == operatorId || !Number(operatorId))
.map((item) => ({
id: `${item.line_ref}|${item.operator_ref?.agency_id}` || 'Unknown',
operator_name: item.operator_ref?.agency_name || 'Unknown',
short_name: JSON.parse(item.route_short_name)[0],
long_name: item.route_long_name,
total: item.total_planned_rides,
actual: item.total_actual_rides,
}))

const [graphData_, loadingGrap] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
Expand Down Expand Up @@ -107,7 +109,7 @@ const DashboardPage = () => {
{groupByOperatorLoading ? (
<Skeleton active />
) : (
<OperatorHbarChart operators={groupByOperatorData} />
<OperatorHbarChart operators={convertToChartCompatibleStruct(groupByOperatorData)} />
)}
</div>
<div className="widget">
Expand All @@ -116,7 +118,7 @@ const DashboardPage = () => {
<Skeleton active />
) : (
<LinesHbarChart
lines={groupByLineData}
lines={convertToWorstLineChartCompatibleStruct(groupByLineData, operatorId)}

Check failure on line 121 in src/pages/dashboard/DashboardPage.tsx

View workflow job for this annotation

GitHub Actions / build

Argument of type 'string' is not assignable to parameter of type 'number'.
operators_whitelist={['אלקטרה אפיקים', 'דן', 'מטרופולין', 'קווים', 'אגד']}
/>
)}
Expand Down

0 comments on commit 1629d85

Please sign in to comment.