Skip to content

Commit

Permalink
Update path on duration change (initial)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasGLund99 committed Nov 7, 2024
1 parent c517414 commit 87385f6
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions src/components/vesselDetailsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,68 @@ export default function VesselDetailsBox() {
const [pathDuration, setPathDuration] = useState<number>(1)
const [isCollapsed, setIsCollapsed] = useState<boolean>(false)

// Fetch vessel details on selected vessel change
useEffect(() => {
const fetchDetails = async () => {
if (!selectedVesselmmsi) return
const details = await clientHandler.getVesselInfo({
mmsi: selectedVesselmmsi,
timestamp: myDateTimeRef.current.getTime(),
})
setVesselDetails(details)

try {
const details = await clientHandler.getVesselInfo({
mmsi: selectedVesselmmsi,
timestamp: myDateTimeRef.current.getTime(),
})
setVesselDetails(details)
} catch (e) {
console.error(e)
}
}

fetchDetails()
setLoading(false)
}, [clientHandler, selectedVesselmmsi])

async function controlVesselPath() {
async function tryGetVesselPath() {
if (!selectedVesselmmsi) return
if (selectedVesselPath.length > 0) {
try {
const res = await clientHandler.getVesselPath({
mmsi: selectedVesselmmsi,
endtime: myDateTimeRef.current.getTime() / 1000,
starttime: myDateTimeRef.current.getTime() / 1000 - pathDuration * 60 * 60,
}) //time is in seconds
setSelectedVesselPath(res.pathHistory.locations)
} catch (e) {
console.error(e)
}
}

const pathIsShown = selectedVesselPath.length > 0

async function controlVesselPath() {
if (pathIsShown) {
setSelectedVesselPath([])
return
}
const res = await clientHandler.getVesselPath({
mmsi: selectedVesselmmsi,
endtime: myDateTimeRef.current.getTime() / 1000,
starttime: myDateTimeRef.current.getTime() / 1000 - pathDuration * 60 * 60,
}) //time is in seconds
setSelectedVesselPath(res.pathHistory.locations)
await tryGetVesselPath()
}

function handleDurationChange(val: string) {
async function handleDurationChange(val: string) {
try {
const parsed = parseFloat(val)
setPathDuration(parsed)
setPathDuration(Math.max(0, parsed))
} catch (e) {
console.error(e)
setPathDuration(1)
}
}

// Fetch path history on path duration change
useEffect(() => {
async function doSomething() {
await tryGetVesselPath()
}
if (pathIsShown) doSomething()
}, [pathDuration, pathIsShown, tryGetVesselPath])

const vesselDetailsContent = [
{ displayName: 'Name', value: vesselDetails?.name },
{ displayName: 'MMSI', value: vesselDetails?.mmsi },
Expand Down Expand Up @@ -99,7 +123,9 @@ export default function VesselDetailsBox() {
<input
className="w-12 rounded-md text-center bg-gray-600"
type="number"
placeholder="Path duration Hours"
min="0"
max="24"
placeholder="Path duration Hours"
value={pathDuration}
onChange={(e) => handleDurationChange(e.target.value)}
></input>
Expand All @@ -114,7 +140,7 @@ export default function VesselDetailsBox() {
<button
title="Hide/show vessel details"
onClick={() => setIsCollapsed(!isCollapsed)}
className={`w-full flex flex-cols items-center justify-center bottom-0 ${!isCollapsed && "mt-4"}`}
className={`w-full flex flex-cols items-center justify-center bottom-0 ${!isCollapsed && 'mt-4'}`}
>
{isCollapsed ? <ChevronSVG rotate={180} /> : <ChevronSVG />}
</button>
Expand Down

0 comments on commit 87385f6

Please sign in to comment.