Skip to content

Commit 898701c

Browse files
committed
fix conflicts
Signed-off-by: cwen0 <[email protected]>
2 parents 773a2ed + 7cefc77 commit 898701c

File tree

3 files changed

+99
-52
lines changed

3 files changed

+99
-52
lines changed

web/src/App.tsx

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,63 @@
1-
import React, { Dispatch, SetStateAction, useState } from 'react';
2-
import styled, { ThemeProvider } from 'styled-components';
3-
import { Point } from 'react-simple-maps';
4-
import Banner from './components/Banner';
5-
import ChartContainer from './components/ChartContainer';
6-
import TrafficChart from './components/TrafficChart';
1+
import React, { Dispatch, SetStateAction } from 'react'
2+
import styled, { ThemeProvider } from 'styled-components'
3+
import { Point } from 'react-simple-maps'
4+
import Banner from './components/Banner'
5+
import TrafficChartContainer from './components/ChartContainer'
76

8-
import { data } from './data';
7+
export interface IAppState {
8+
selectedPoint: Point
9+
setSelectedPoint: Dispatch<SetStateAction<Point>>
10+
}
911

1012
const theme = {
11-
colors: [
12-
'#935B92',
13-
'#52A429',
14-
'#F3B266',
15-
'#A09E92',
16-
'#634465',
17-
'#00AFD6',
18-
'#95C01F',
19-
'#F796B8',
20-
'#E67921',
21-
'#A62D4E'
22-
]
23-
};
13+
colors: [
14+
'#935B92',
15+
'#52A429',
16+
'#F3B266',
17+
'#A09E92',
18+
'#634465',
19+
'#00AFD6',
20+
'#95C01F',
21+
'#F796B8',
22+
'#E67921',
23+
'#A62D4E'
24+
]
25+
}
2426

2527
const AppStyles = styled.div`
2628
display: flex;
2729
flex-direction: column;
2830
text-align: center;
2931
height: 100%;
3032
margin-bottom: 20px;
31-
`;
33+
`
3234
const BodyStyles = styled.div`
3335
width: 95%;
3436
margin-left: auto;
3537
margin-right: auto;
36-
`;
38+
`
3739

3840
const Row = styled.div`
3941
margin-top: 20px;
4042
width: 100%;
4143
display: flex;
4244
flex-direction: row;
4345
justify-content: space-between;
44-
`;
46+
`
4547

4648
const App: React.FC = () => {
47-
// get the data coressponding to the selected point on the map
48-
const location = data;
49+
return (
50+
<ThemeProvider theme={theme}>
51+
<AppStyles className="App">
52+
<Banner />
53+
<BodyStyles className="body">
54+
<Row>
55+
<TrafficChartContainer title="Ping" />
56+
</Row>
57+
</BodyStyles>
58+
</AppStyles>
59+
</ThemeProvider>
60+
)
61+
}
4962

50-
return (
51-
<ThemeProvider theme={theme}>
52-
<AppStyles className="App">
53-
<Banner />
54-
<BodyStyles className="body">
55-
<Row>
56-
<ChartContainer title="Ping">
57-
<TrafficChart data={location.traffic}></TrafficChart>
58-
</ChartContainer>
59-
</Row>
60-
</BodyStyles>
61-
</AppStyles>
62-
</ThemeProvider>
63-
);
64-
};
65-
66-
export default App;
63+
export default App
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import React from 'react';
2-
import styled from 'styled-components';
1+
import React, { useState } from 'react'
2+
import styled from 'styled-components'
3+
4+
import TrafficChart from './TrafficChart'
5+
6+
import { useInterval } from '../utils/useInterval'
7+
import { data, ITrafficData } from '../data'
38

49
interface IChartProps {
5-
title: string;
10+
title: string
611
}
712

813
const ChartContainerStyles = styled.div`
@@ -14,21 +19,33 @@ const ChartContainerStyles = styled.div`
1419
flex-direction: column;
1520
width: 30%;
1621
height: 400px;
17-
`;
22+
`
1823
const ChartTitleStyles = styled.h2`
24+
width: 100%;
1925
padding: 0;
2026
font-size: 14px;
2127
align-self: flex-start;
22-
`;
28+
text-align: center;
29+
`
2330

2431
const ChartContainer: React.FC<IChartProps> = props => {
25-
const { title } = props;
32+
const { title } = props
33+
const [traffic, setTraffic] = useState<ITrafficData[]>([])
34+
35+
const fetchData = () => {
36+
console.log('on fetch data', new Date())
37+
// TODO: use real fetch data API, and update response data with setTraffic Hook
38+
setTraffic(data.traffic)
39+
}
40+
41+
useInterval(fetchData, 10 * 1000, true)
42+
2643
return (
2744
<ChartContainerStyles>
2845
<ChartTitleStyles>{title}</ChartTitleStyles>
29-
{props.children}
46+
<TrafficChart data={traffic} />
3047
</ChartContainerStyles>
31-
);
32-
};
48+
)
49+
}
3350

34-
export default ChartContainer;
51+
export default ChartContainer

web/src/utils/useInterval.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// useInterval Custom Hook
2+
// Making setInterval Declarative with React Hooks
3+
4+
import { useEffect, useRef } from 'react'
5+
6+
const noop = () => {} // keep typescript happy
7+
export const useInterval = (
8+
callback: () => void,
9+
delay: number | null,
10+
immediate?: boolean // called when mounted if true
11+
) => {
12+
const savedCallback = useRef(noop)
13+
14+
// Remember the latest function.
15+
useEffect(() => {
16+
savedCallback.current = callback
17+
})
18+
19+
// Execute callback if immediate is set.
20+
useEffect(() => {
21+
if (!immediate) return
22+
savedCallback.current()
23+
}, [immediate])
24+
25+
// Set up the interval.
26+
useEffect(() => {
27+
if (delay === null) return undefined
28+
29+
const tick = () => savedCallback.current()
30+
const id = setInterval(tick, delay)
31+
return () => clearInterval(id)
32+
}, [delay])
33+
}

0 commit comments

Comments
 (0)