-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test #1
base: main
Are you sure you want to change the base?
Test #1
Changes from 3 commits
403a21e
77d7782
47cf1a1
e38e259
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,55 @@ | ||
import React, {useState, useEffect} from "react" | ||
import { Port } from '../services/ports-reducer' | ||
import { Loader } from "@googlemaps/js-api-loader" | ||
import React, { useState, useEffect } from "react"; | ||
import { Port } from "../services/ports-reducer"; | ||
import { Loader } from "@googlemaps/js-api-loader"; | ||
|
||
declare global { | ||
interface Window { | ||
initMap: () => void; | ||
} | ||
interface Window { | ||
initMap: () => void; | ||
} | ||
} | ||
|
||
interface RouteMapParams{ | ||
ports: Port[] | ||
interface RouteMapParams { | ||
ports: Port[]; | ||
} | ||
function RouteMap({ports}: RouteMapParams){ | ||
const [theMap, setTheMap] = useState<google.maps.Map>() | ||
function RouteMap({ ports }: RouteMapParams) { | ||
const [theMap, setTheMap] = useState<google.maps.Map>(); | ||
|
||
const loader = new Loader({ | ||
apiKey: "AIzaSyDckokR5ozbjFB7hC0yip1S5mbPYcgoQv8", | ||
version: "weekly" | ||
}) | ||
const loader = new Loader({ | ||
apiKey: "AIzaSyDckokR5ozbjFB7hC0yip1S5mbPYcgoQv8", | ||
version: "weekly", | ||
}); | ||
|
||
useEffect(() => { | ||
loader | ||
.load() | ||
.then((google) => { | ||
const mapOptions = { | ||
center: { | ||
lat: 0, | ||
lng: 0 | ||
}, | ||
zoom: 3 | ||
} | ||
const map = new google.maps.Map(document.getElementById("route-map") as HTMLElement, mapOptions) | ||
setTheMap(map); | ||
}) | ||
}, []); | ||
return ( | ||
<div id="map-container"> | ||
<div id="route-map"></div> | ||
useEffect(() => { | ||
loader.load().then((google) => { | ||
const mapOptions = { | ||
center: { | ||
lat: 0, | ||
lng: 0, | ||
}, | ||
zoom: 3, | ||
}; | ||
const map = new google.maps.Map( | ||
document.getElementById("route-map") as HTMLElement, | ||
mapOptions | ||
); | ||
const infowindow = new google.maps.InfoWindow(); | ||
for (let i = 0; i < ports.length; i++) { | ||
const singlePort = ports[i]; | ||
const myLatLng = { lat: singlePort["lat"], lng: singlePort["lng"] }; | ||
new google.maps.Marker({ | ||
position: myLatLng, | ||
map, | ||
title: singlePort["name"], | ||
}); | ||
} | ||
setTheMap(map); | ||
}); | ||
}, [ports]); | ||
return ( | ||
<div id="map-container"> | ||
<div id="route-map"></div> | ||
</div> | ||
) | ||
); | ||
} | ||
|
||
export default RouteMap; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,73 @@ | ||
import React from 'react' | ||
import RouteMap from "./route-map" | ||
import "./voyage-planner.css" | ||
import React, { useEffect, useState } from "react"; | ||
import RouteMap from "./route-map"; | ||
import "./voyage-planner.css"; | ||
import { fetchPorts, setPorts, Port } from "../services/ports-reducer"; | ||
import { addPorts } from "../services/voyage-reducer"; | ||
import { useAppDispatch, useAppSelector } from "../services/hooks"; | ||
import store, { RootState } from "../services/store"; | ||
|
||
interface VoyagePlannerParams{ | ||
interface VoyagePlannerParams {} | ||
|
||
} | ||
function VoyagePlanner(params: VoyagePlannerParams) { | ||
const [selectedPort, setSelectedPort] = useState(""); | ||
const [addedPorts, setAddedPorts] = useState<Port[]>([]); | ||
const [offset, setOffset] = useState(0); | ||
const [fetchData, setFetchData] = useState([]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This data is stored in the Redux store. Why are you duplicating it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your feedback John. I removed the duplicates from here. Please check the latest changes. |
||
|
||
const dispatch = useAppDispatch(); | ||
|
||
function VoyagePlanner(params: VoyagePlannerParams){ | ||
useEffect(() => { | ||
fetchPorts(offset).then((resp: any) => { | ||
setFetchData(fetchData.concat(resp)); | ||
dispatch(setPorts(fetchData)); | ||
setOffset(offset + resp.length); | ||
setSelectedPort(store.getState().ports.ports[0].name); | ||
}); | ||
}, [offset]); | ||
|
||
return ( | ||
<div className="voyage-planner-container"> | ||
<div className="voyage-route-container"> | ||
<h2>Route Planner</h2> | ||
<div className="port-locator"> | ||
<select id="port-select"/> <button>+</button> | ||
</div> | ||
<div className="voyage-listing"> | ||
<h2>Voyage</h2> | ||
</div> | ||
</div> | ||
<div className="voyage-map-container"> | ||
<h2>Route Map</h2> | ||
<RouteMap ports={[]}/> | ||
</div> | ||
const handleChange = (e: any) => { | ||
setSelectedPort(e.target.value); | ||
}; | ||
|
||
const addVoyage = () => { | ||
const portTobeAdded = | ||
store.getState().ports.ports && | ||
store | ||
.getState() | ||
.ports.ports.filter((item: Port) => item.name === selectedPort); | ||
dispatch(addPorts(portTobeAdded[0])); | ||
setAddedPorts(store.getState().voyage.ports); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed requirement to calculate estimated time of arrival, distance travelled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also added the functionality for distance and estimated time of arrival here. Please check the latest code. |
||
}; | ||
return ( | ||
<div className="voyage-planner-container"> | ||
<div className="voyage-route-container"> | ||
<h2>Route Planner</h2> | ||
<div className="port-locator"> | ||
<select id="port-select" value={selectedPort} onChange={handleChange}> | ||
{store.getState().ports.ports && | ||
store.getState().ports.ports.map((item: Port, key: number) => { | ||
return ( | ||
<option id={key.toString()} key={key} value={item.name}> | ||
{item.name} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
<button onClick={addVoyage}>Add</button> | ||
</div> | ||
<div className="voyage-listing"> | ||
<h2>Voyage</h2> | ||
{addedPorts && | ||
addedPorts.map((item, key) => { | ||
return <div key={key}>{item.name}</div>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low effort here. Remove port functionality? Re-arrange? No styling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this part as well. |
||
})} | ||
</div> | ||
) | ||
</div> | ||
<div className="voyage-map-container"> | ||
<h2>Route Map</h2> | ||
<RouteMap ports={addedPorts} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
export default VoyagePlanner | ||
export default VoyagePlanner; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ export interface Port { | |
uncode: string, | ||
name: string, | ||
lat: number, | ||
lon: number | ||
lng: number | ||
} | ||
|
||
interface PortsState { | ||
|
@@ -23,6 +23,16 @@ const initialState: PortsState = { | |
offset: 0, | ||
ports: [] | ||
} | ||
export interface SetPortAction { | ||
type: 'SETPORTACTION' | ||
portList: Port[] | ||
} | ||
|
||
export const setPorts = (portList: Port[]): SetPortAction => { | ||
return { type: 'SETPORTACTION', portList } | ||
} | ||
|
||
|
||
|
||
/** | ||
* TODO: implement the reducer function to manage the list of ports | ||
|
@@ -31,9 +41,13 @@ const initialState: PortsState = { | |
* @param action | ||
* @returns PortsState | ||
*/ | ||
export function portsReducer(state = initialState, action: AnyAction): PortsState{ | ||
|
||
return state | ||
export function portsReducer(state = initialState, action: AnyAction): PortsState { | ||
switch (action.type) { | ||
case 'SETPORTACTION': | ||
return { ...state, ports: action.portList } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are doing a paginated fetch - the ports should be appended, not replaced There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I solved the issue here. Please check the latest changes which I made and let me know any further improvements |
||
default: | ||
return { ...state } | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -42,7 +56,17 @@ export function portsReducer(state = initialState, action: AnyAction): PortsStat | |
* so you may need multiple calls to fetch all the data | ||
* @param offset: where to start pulling from | ||
*/ | ||
export function fetchPorts(offset: number){} | ||
export async function fetchPorts(offset: number) { | ||
return new Promise((resolve) => { | ||
fetch( | ||
`https://8u9tblsay0.execute-api.us-east-1.amazonaws.com/default/zn-frontend-challenge-port-service?offset=${offset}` | ||
) | ||
.then((response) => response.json()) | ||
.then((json) => { | ||
resolve(json.data); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added ports are in the redux store. Where are you duplicating here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also removed here. Please check the latest changes.