Skip to content

Commit

Permalink
moved AllLinesChart to separate component (#272)
Browse files Browse the repository at this point in the history
* moved AllLinesChart to separate component

* moved WorstLinesChart logic to separate component

* moved startDate and endDate to Dashboard, fix typo
  • Loading branch information
TomRytt authored Dec 3, 2023
1 parent 79edd8f commit a35ccea
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 90 deletions.
60 changes: 60 additions & 0 deletions src/pages/dashboard/AllLineschart/AllLinesChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Tooltip } from '@mui/material'
import { Skeleton } from 'antd'
import { Fragment } from 'react'
import { TEXTS } from 'src/resources/texts'
import OperatorHbarChart from '../OperatorHbarChart/OperatorHbarChart'
import { GroupByRes, useGroupBy } from 'src/api/groupByService'
import { FC } from 'react'
import { Moment } from 'moment/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,
}))
}

interface AllChartComponentProps {
startDate: Moment
endDate: Moment
}

export const AllLinesChart: FC<AllChartComponentProps> = ({ startDate, endDate }) => {
const [groupByOperatorData, groupByOperatorLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref',
})

return (
<div className="widget">
<h2 className="title">
{TEXTS.dashboard_page_title}
<Tooltip
title={convertLineFeedToHtmlTags(TEXTS.dashboard_tooltip_content)}
placement="left"
arrow>
<span className="tooltip-icon">i</span>
</Tooltip>
</h2>
{groupByOperatorLoading ? (
<Skeleton active />
) : (
<OperatorHbarChart operators={convertToChartCompatibleStruct(groupByOperatorData)} />
)}
</div>
)
}

function convertLineFeedToHtmlTags(srt: string): React.ReactNode {
return srt.split('\n').map((row, i) => (
<Fragment key={i}>
{row}
<br />
</Fragment>
))
}

export default AllLinesChart
20 changes: 11 additions & 9 deletions src/pages/dashboard/ArrivalByTimeChart/DayTimeChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react'
import React, { FC } from 'react'
import { TEXTS } from 'src/resources/texts'
import { Skeleton, Radio, RadioChangeEvent } from 'antd'
import ArrivalByTimeChart from './ArrivalByTimeChart'
import { useDate } from 'src/pages/components/DateTimePicker'
import moment from 'moment'
import { GroupByRes, useGroupBy } from 'src/api/groupByService'
import { Moment } from 'moment/moment'

const convertToGraphCompatibleStruct = (arr: GroupByRes[]) => {
return arr.map((item: GroupByRes) => ({
Expand All @@ -17,13 +16,16 @@ const convertToGraphCompatibleStruct = (arr: GroupByRes[]) => {
gtfs_route_hour: item.gtfs_route_hour,
}))
}
const now = moment()
const DayTimeChart = () => {
const [startDate] = useDate(now.clone().subtract(7, 'days'))
const [endDate] = useDate(now.clone().subtract(1, 'day'))

interface DayTimeChartProps {
startDate: Moment
endDate: Moment
}

const DayTimeChart: FC<DayTimeChartProps> = ({ startDate, endDate }) => {
const [groupByHour, setGroupByHour] = React.useState<boolean>(false)

const [graphData, loadingGrap] = useGroupBy({
const [graphData, loadingGraph] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: groupByHour ? 'operator_ref,gtfs_route_hour' : 'operator_ref,gtfs_route_date',
Expand All @@ -41,7 +43,7 @@ const DayTimeChart = () => {
<Radio.Button value="byDay">{TEXTS.group_by_day_tooltip_content}</Radio.Button>
<Radio.Button value="byHour">{TEXTS.group_by_hour_tooltip_content}</Radio.Button>
</Radio.Group>
{loadingGrap ? (
{loadingGraph ? (
<Skeleton active />
) : (
<ArrivalByTimeChart data={convertToGraphCompatibleStruct(graphData)} operatorId={''} />
Expand Down
101 changes: 20 additions & 81 deletions src/pages/dashboard/DashboardPage.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,31 @@
import React, { Fragment, useState } from 'react'
import { GroupByRes, useGroupBy } from 'src/api/groupByService'
import { PageContainer } from '../components/PageContainer'
import OperatorHbarChart from './OperatorHbarChart/OperatorHbarChart'
import { useState } from 'react'

// Services and libraries
import { DateSelector } from '../components/DateSelector'
import { useDate } from '../components/DateTimePicker'
import moment from 'moment'

// Styling
import './DashboardPage.scss'
import { PageContainer } from '../components/PageContainer'
import { TEXTS } from 'src/resources/texts'
import moment from 'moment'
import LinesHbarChart from './LineHbarChart/LinesHbarChart'
import { Tooltip } from '@mui/material'
import OperatorSelector from 'src/pages/components/OperatorSelector'
import { useDate } from '../components/DateTimePicker'
import { Skeleton, Typography } from 'antd'
import { Typography } from 'antd'
import Grid from '@mui/material/Unstable_Grid2' // Grid version 2
import { DateSelector } from '../components/DateSelector'

// Components
import OperatorSelector from 'src/pages/components/OperatorSelector'
import DayTimeChart from './ArrivalByTimeChart/DayTimeChart'
const { Title } = Typography
import AllLinesChart from './AllLineschart/AllLinesChart'
import WorstLinesChart from './WorstLinesChart/WorstLinesChart'

// Declarations
const { Title } = Typography
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: string) => {
if (!arr || !arr.length) return []
return arr
.filter((row: GroupByRes) => row.operator_ref?.agency_id === operatorId || !Number(operatorId))
.map((item: GroupByRes) => ({
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 DashboardPage = () => {
const [startDate, setStartDate] = useDate(now.clone().subtract(7, 'days'))
const [endDate, setEndDate] = useDate(now.clone().subtract(1, 'day'))
const [operatorId, setOperatorId] = useState('')
const [groupByOperatorData, groupByOperatorLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref',
})
const [groupByLineData, lineDataLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref,line_ref',
})

return (
<PageContainer>
Expand Down Expand Up @@ -90,22 +63,7 @@ const DashboardPage = () => {
</Grid>
<Grid container spacing={2} alignItems="flex-start">
<Grid xs={12} lg={6}>
<div className="widget">
<h2 className="title">
{TEXTS.dashboard_page_title}
<Tooltip
title={convertLineFeedToHtmlTags(TEXTS.dashboard_tooltip_content)}
placement="left"
arrow>
<span className="tooltip-icon">i</span>
</Tooltip>
</h2>
{groupByOperatorLoading ? (
<Skeleton active />
) : (
<OperatorHbarChart operators={convertToChartCompatibleStruct(groupByOperatorData)} />
)}
</div>
<AllLinesChart startDate={startDate} endDate={endDate} />
</Grid>
<Grid xs={12} display={{ xs: 'block', lg: 'none' }}>
<OperatorSelector
Expand All @@ -115,33 +73,14 @@ const DashboardPage = () => {
/>
</Grid>
<Grid xs={12} lg={6}>
<div className="widget">
<h2 className="title">{TEXTS.worst_lines_page_title}</h2>
{lineDataLoading ? (
<Skeleton active />
) : (
<LinesHbarChart
lines={convertToWorstLineChartCompatibleStruct(groupByLineData, operatorId)}
operators_whitelist={['אלקטרה אפיקים', 'דן', 'מטרופולין', 'קווים', 'אגד']}
/>
)}
</div>
<WorstLinesChart startDate={startDate} endDate={endDate} operatorId={operatorId} />
</Grid>
<Grid xs={12}>
<DayTimeChart />
<DayTimeChart startDate={startDate} endDate={endDate} />
</Grid>
</Grid>
</PageContainer>
)
}

function convertLineFeedToHtmlTags(srt: string): React.ReactNode {
return srt.split('\n').map((row, i) => (
<Fragment key={i}>
{row}
<br />
</Fragment>
))
}

export default DashboardPage
52 changes: 52 additions & 0 deletions src/pages/dashboard/WorstLinesChart/WorstLinesChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Skeleton } from 'antd'
import { GroupByRes, useGroupBy } from 'src/api/groupByService'
import LinesHbarChart from '../LineHbarChart/LinesHbarChart'
import { TEXTS } from 'src/resources/texts'
import { FC } from 'react'
import { Moment } from 'moment/moment'

interface WorstLinesChartProps {
startDate: Moment
endDate: Moment
operatorId: string
}

export const WorstLinesChart: FC<WorstLinesChartProps> = ({ startDate, endDate, operatorId }) => {
const [groupByLineData, lineDataLoading] = useGroupBy({
dateTo: endDate,
dateFrom: startDate,
groupBy: 'operator_ref,line_ref',
})

const convertToWorstLineChartCompatibleStruct = (arr: GroupByRes[], operatorId: string) => {
if (!arr || !arr.length) return []
return arr
.filter(
(row: GroupByRes) => row.operator_ref?.agency_id === operatorId || !Number(operatorId),
)
.map((item: GroupByRes) => ({
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,
}))
}

return (
<div className="widget">
<h2 className="title">{TEXTS.worst_lines_page_title}</h2>
{lineDataLoading ? (
<Skeleton active />
) : (
<LinesHbarChart
lines={convertToWorstLineChartCompatibleStruct(groupByLineData, operatorId)}
operators_whitelist={['אלקטרה אפיקים', 'דן', 'מטרופולין', 'קווים', 'אגד']}
/>
)}
</div>
)
}

export default WorstLinesChart

0 comments on commit a35ccea

Please sign in to comment.