Skip to content

Commit

Permalink
Added Inline Hooks version
Browse files Browse the repository at this point in the history
  • Loading branch information
Seaneeee committed Jun 30, 2020
1 parent 80d4a97 commit aeb7c39
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 12 deletions.
1 change: 1 addition & 0 deletions WarehouseTransport/ClientApp/src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_GOOGLE_KEY="AIzaSyD1uCZ65ceA_IbL-_cGa4ATNola0934TbE";
4 changes: 3 additions & 1 deletion WarehouseTransport/ClientApp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import Transport from "./components/Transport";
import Transport from "./components/Transport";
import TransportHooks from "./components/TransportWithHooks";

import './custom.css'

Expand All @@ -13,6 +14,7 @@ export default class App extends Component {
return (
<Layout>
<Route exact path='/' component={Home} />
<Route path='/transportHooks' component={TransportHooks} />
<Route path='/transport' component={Transport} />
</Layout>
);
Expand Down
8 changes: 3 additions & 5 deletions WarehouseTransport/ClientApp/src/components/Home.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import React, { Component } from 'react';

export class Home extends Component {
static displayName = Home.name;
//console.log(process.env.PUBLIC_URL);
render() {
return (
<div>
<h1>Ninja Delivery Notes</h1>
return (
<div>
<h1>Warehouse Prototype</h1>
<p>Basic delivery calculator between warehouses and destinations:</p>
<ul>
<li>Click <b>Route Calculator</b> in menu to view route for each driver.</li>
Expand Down
17 changes: 14 additions & 3 deletions WarehouseTransport/ClientApp/src/components/NavMenu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { Component } from 'react';
import { Collapse, Container, Navbar, NavbarBrand, NavbarToggler, NavItem, NavLink } from 'reactstrap';
import { LinkContainer } from 'react-router-bootstrap';
import { Link } from 'react-router-dom';
import { withRouter } from 'react-router';
import './NavMenu.css';

export class NavMenu extends Component {
Expand All @@ -26,15 +28,24 @@ export class NavMenu extends Component {
<header>
<Navbar className="navbar-expand-sm navbar-toggleable-sm ng-white border-bottom box-shadow mb-3" light>
<Container>
<NavbarBrand tag={Link} to="/">Ninja Transport Routes</NavbarBrand>
<NavbarBrand tag={Link} to="/">Transport Routes</NavbarBrand>
<NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
<Collapse className="d-sm-inline-flex flex-sm-row-reverse" isOpen={!this.state.collapsed} navbar>
<ul className="navbar-nav flex-grow">
<NavItem>
<NavLink tag={Link} className="text-dark" to="/">Notes</NavLink>
<LinkContainer to={'/'} className="text-dark" exact>
<a className="nav-link">Notes</a>
</LinkContainer>
</NavItem>
<NavItem>
<NavLink tag={Link} className="text-dark" to="/transport">Route Calculator</NavLink>
<LinkContainer to={'/transportHooks'} className="text-dark" exact>
<a className="nav-link">Route Calculator using hooks (TS)</a>
</LinkContainer>
</NavItem>
<NavItem>
<LinkContainer to={'/transport'} className="text-dark" exact>
<a className="nav-link">Route Calculator (ES6)</a>
</LinkContainer>
</NavItem>
</ul>
</Collapse>
Expand Down
6 changes: 3 additions & 3 deletions WarehouseTransport/ClientApp/src/components/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class MapDirectionsRenderer extends React.Component {
this.setState({ loaded: true, driver: driver });
}

componentDidMount() {
}

render() {
let newDriver = this.props.driver;
let loadedDriver = this.state.loaded;
Expand Down Expand Up @@ -117,6 +114,9 @@ class Transport extends React.Component {

return (
<div>
<div>
<i>Older version using Classes and ES6</i>
</div>
<div className="driverSelection">
<span><b>Select driver</b> to view route</span>
<select value={this.state.driver} onChange={this.handleChange}>
Expand Down
149 changes: 149 additions & 0 deletions WarehouseTransport/ClientApp/src/components/TransportWithHooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React from "react";
import {
withGoogleMap,
GoogleMap,
withScriptjs,
Marker,
DirectionsRenderer
} from "react-google-maps";

function MapDirectionsRenderer(props) {

const [state, setState] = React.useState({
directions: null,
error: null,
places: [],
loaded: false,
driver: 0
})

async function getDirections() {
const { travelMode, driver } = props;
const response = await fetch('api/directions/' + driver);
const data = await response.json();
let places = data;
const waypoints = places.map(p => ({
location: { lat: p.latitude, lng: p.longitude },
stopover: true
}))
const origin = waypoints.shift().location;
const destination = waypoints.pop().location;

const directionsService = new window.google.maps.DirectionsService();
directionsService.route(
{
origin: origin,
destination: destination,
travelMode: travelMode,
waypoints: waypoints
},
(result, status) => {
if (status === window.google.maps.DirectionsStatus.OK) {
setState({
...state,
directions: result
})

} else {
setState({
...state,
error: result
})
}
}
);
setState({
...state,
loaded: true, driver: driver
})
}

let newDriver = props.driver;
let loadedDriver = state.loaded;
React.useEffect(() => {
if (newDriver !== state.driver) {
getDirections();
}
}, [newDriver]);


if (state.error && state.driver) {
return <h1>{state.error}</h1>;
}
if (!loadedDriver && state.driver!==0) {
return <h1 className="loadingRoute">Loading route</h1>;
}
return (state.directions && <DirectionsRenderer directions={state.directions} />)
}

const Map = withScriptjs(
withGoogleMap(props => (
<GoogleMap
defaultCenter={props.defaultCenter}
defaultZoom={props.defaultZoom}
>
{props.markers.map((marker, index) => {
const position = { lat: marker.latitude, lng: marker.longitude };
return <Marker key={index} position={position} />;
})}
<MapDirectionsRenderer
places={props.markers}
travelMode={window.google.maps.TravelMode.DRIVING}
driver={props.driver}
/>
</GoogleMap>
))
);

const googleMapsApiKey = "AIzaSyD1uCZ65ceA_IbL-_cGa4ATNola0934TbE";

const TransportHooks = (props) => {
const [driver, setDriver] = React.useState("1");

const handleChange = (e) => {
setDriver(e.target.value)
}
const places = [];

const {
loadingElement,
containerElement,
mapElement,
defaultCenter,
defaultZoom
} = props;

return (
<div>
<div>
<i>Typescript version using Hooks</i>
</div>
<div className="driverSelection">
<span><b>Select driver</b> to view route</span>
<select value={driver} onChange={(e) => handleChange(e)}>
<option value="1">Driver A</option>
<option value="2">Driver B</option>
<option value="3">Driver C</option>
<option value="4">Driver D</option>
<option value="5">Driver E</option>
</select>
</div>
<Map
googleMapURL={
'https://maps.googleapis.com/maps/api/js?key=' +
googleMapsApiKey +
'&libraries=geometry,drawing,places'
}
markers={places}
driver={driver}
loadingElement={loadingElement || <div style={{ height: "100%" }} />}
containerElement={containerElement || <div style={{ height: "80vh" }} />}
mapElement={mapElement || <div style={{ height: "100%" }} />}
defaultCenter={defaultCenter || { lat: -37.762553, lng: 144.954236 }}
defaultZoom={defaultZoom || 11}
/>
</div>
);
};

export default TransportHooks;
9 changes: 9 additions & 0 deletions WarehouseTransport/ClientApp/src/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ code {
.driverSelection select {
margin-left:10px;
}

.navbar-nav .nav-link {
border-radius: 5px;
}

.navbar-nav .nav-link.active {
color: blue !important;
background-color: #f2f2f2;
}
1 change: 1 addition & 0 deletions WarehouseTransport/ClientApp/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
25 changes: 25 additions & 0 deletions WarehouseTransport/ClientApp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
9 changes: 9 additions & 0 deletions WarehouseTransport/WarehouseTransport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="3.1.5" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="3.9.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand All @@ -21,9 +25,14 @@
</ItemGroup>

<ItemGroup>
<None Remove="ClientApp\src\components\TransportWithHooks.tsx" />
<None Remove="master_wharhousex1.yml" />
</ItemGroup>

<ItemGroup>
<TypeScriptCompile Include="ClientApp\src\components\TransportWithHooks.tsx" />
</ItemGroup>

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
Expand Down

0 comments on commit aeb7c39

Please sign in to comment.