diff --git a/src/App.js b/src/App.js index 74fdeab..7ac13f8 100644 --- a/src/App.js +++ b/src/App.js @@ -7,6 +7,7 @@ import about from './pages/about'; import favorite from './pages/favorite'; import history from './pages/history'; import { AuthProvider } from './providers/AuthProvider'; +import route from './pages/route'; function App() { return ( @@ -17,7 +18,6 @@ function App() { {/* */} -
} />
} />
} /> + diff --git a/src/components/BusInfoContainer.js b/src/components/BusInfoContainer.js index e172c07..1c15cc2 100644 --- a/src/components/BusInfoContainer.js +++ b/src/components/BusInfoContainer.js @@ -10,22 +10,22 @@ import BusInformations from './BusInformations'; import VerticalLineDivide from './VerticalLineDivide'; import PropTypes from 'prop-types'; -export default function BusInfoContainer({ dontShowTakeIt, fillHeart }) { - const way = [ - { id: 0, isStart: true, name: '32 peak' }, - { id: 1, name: 'Italian Village' }, - { id: 2, name: 'Park View' }, - { id: 3, name: 'Naz Naz' }, - { id: 4, name: 'Empire' }, - { id: 5, name: 'Ankawa', isEnd: true }, - ]; - +export default function BusInfoContainer({ + route = {}, + dontShowTakeIt, + fillHeart, +}) { + const { way = [], bus = { working_hours: ' ', working_days: [] } } = route; + const time = bus.working_hours[0] + .split('-') + .map((time) => time.substr(0, 2) + ':' + time.substr(2)) + .join(' - '); return (
- Bus No. (473824 EBL - IRQ) + Bus No. ({bus.plate_number} EBL - IRQ)
@@ -56,7 +56,7 @@ export default function BusInfoContainer({ dontShowTakeIt, fillHeart }) {
bus icon
-
6:22 PM - 7:09 PM
+
{time}
Wifi available @@ -71,12 +71,12 @@ export default function BusInfoContainer({ dontShowTakeIt, fillHeart }) {
- +
-

Work days: Saturday - Thusday

+

Work days: {bus.working_days.join(', ')}.

{!dontShowTakeIt && ( @@ -95,6 +95,7 @@ export default function BusInfoContainer({ dontShowTakeIt, fillHeart }) { ); } BusInfoContainer.propTypes = { + route: PropTypes.node, dontShowTakeIt: PropTypes.bool.isRequired, fillHeart: PropTypes.bool.isRequired, }; diff --git a/src/components/BusInformations.js b/src/components/BusInformations.js index 36cf234..e18ed13 100644 --- a/src/components/BusInformations.js +++ b/src/components/BusInformations.js @@ -1,6 +1,7 @@ import React from 'react'; +import PropTypes from 'prop-types'; -export default function BusInformations() { +export default function BusInformations({ time }) { return (
@@ -10,8 +11,12 @@ export default function BusInformations() {

Date: 23/07/2020

-

Work time: 9:00 Am - 9:00 PM

+

Work time: {time}

); } + +BusInformations.propTypes = { + time: PropTypes.string, +}; diff --git a/src/pages/route.js b/src/pages/route.js index 0fb7d61..48df2fe 100644 --- a/src/pages/route.js +++ b/src/pages/route.js @@ -1,4 +1,167 @@ -import React from 'react'; -export default function route() { - return
@todo return route details page
; +import React, { useState, useRef, useEffect } from 'react'; +import ReactMapGL, { Source, Layer } from 'react-map-gl'; +import BusInfoContainer from '../components/BusInfoContainer'; +import { useParams } from 'react-router-dom'; +import { routesRef, bussesRef } from '../api/firebase'; +import pointMark from '../assets/point-marker.png'; +import destMark from '../assets/destination-marker.png'; + +export default function Routedetails() { + const { id } = useParams(); + const [viewport, setViewport] = useState({ + latitude: 36.206291, + longitude: 44.008869, + width: '100%', + height: '250px', + zoom: 11, + }); + + const [route, setRoute] = useState(); + + useEffect(() => { + getRoute(id).then((route) => { + if (route) { + _mapRef.current + .getMap() + .fitBounds([ + route.path.features[1].geometry.coordinates, + route.path.features[2].geometry.coordinates, + ]); + setRoute(route); + } + }); + }, [id]); + + const _mapRef = useRef(); + + useEffect(() => { + // Load all markers and images + const map = _mapRef.current.getMap(); + if (map) { + map.loadImage(pointMark, (error, image) => { + if (error) throw error; + if (!map.hasImage('pointMark')) map.addImage('pointMark', image); + }); + + map.loadImage(destMark, (error, image) => { + if (error) throw error; + if (!map.hasImage('destMark')) map.addImage('destMark', image); + }); + } + }, [_mapRef]); + + return ( +
+ { + setViewport({ ...viewport, width: '100%', height: '250px' }); + }} + mapStyle="mapbox://styles/shna/ckd4x2xmy02kh1ir3hihcr36m" + > + {/* {route && ( + + + + )} */} + + {/* Show Route */} + + {route && ( + + + + + + + )} + + {route && ( +
+ +
+ )} +
+ ); +} + +async function getRoute(id) { + if (window.isJest) return null; + const document = await routesRef.doc(id).get(); + const route = document.data(); + const path = JSON.parse(route.path); + const coordinates = path.geometry.coordinates; + const start = { + type: 'Feature', + properties: { type: 'start' }, + geometry: { + type: 'Point', + coordinates: coordinates[0], + }, + }; + const end = { + type: 'Feature', + properties: { type: 'end' }, + geometry: { + type: 'Point', + coordinates: coordinates[coordinates.length - 1], + }, + }; + route.path = { + type: 'FeatureCollection', + features: [path, start, end], + }; + + route.buses = []; + + const busesDocs = await bussesRef.where('route_id', '==', id).get(); + + busesDocs.forEach((doc) => { + route.buses.push({ id: doc.id, ...doc.data() }); + }); + + return route; }