Skip to content

Commit

Permalink
feat(backend): persist trackname to the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
devshred committed Jun 11, 2024
1 parent 107cf60 commit fe8ede7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
16 changes: 2 additions & 14 deletions src/components/track/DownloadLink.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import React from 'react'
import { FiDownload } from 'react-icons/fi'
import { GeoJsonObject } from 'geojson'
import { encodeToBase64 } from '../../utils/tools'
import useLanguage from '../../hooks/useLanguage'
import { Link } from 'react-router-dom'

type DownloadLinkProps = {
trackId: string
type: string
trackname: string
optimizeWaypoints: boolean
geoJson?: GeoJsonObject | null
}

const DownloadLink: React.FC<DownloadLinkProps> = ({
trackId,
type,
trackname,
optimizeWaypoints,
geoJson,
}) => {
const DownloadLink: React.FC<DownloadLinkProps> = ({ trackId, type, optimizeWaypoints }) => {
const { getMessage } = useLanguage()
const baseUrl = import.meta.env.VITE_BACKEND_BASE_URL
const linkTo: string =
Expand All @@ -28,9 +18,7 @@ const DownloadLink: React.FC<DownloadLinkProps> = ({
trackId +
'?mode=dl&type=' +
type +
(optimizeWaypoints ? '&mode=opt' : '') +
(trackname.length > 0 ? `&name=${encodeToBase64(trackname)}` : '') +
(geoJson != null ? `&wp=${encodeToBase64(JSON.stringify(geoJson))}` : '')
(optimizeWaypoints ? '&mode=opt' : '')

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/components/track/MarkerSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type MarkerSearchProps = {

const MarkerSearch: React.FC<MarkerSearchProps> = ({ setMarkerPositions }) => {
const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebounce(searchTerm, 1000)
const debouncedSearchTerm = useDebounce(searchTerm, 500)
const [results, setResults] = useState<Feature[]>([])
const dropdownDivRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLInputElement>(null)
Expand Down
22 changes: 3 additions & 19 deletions src/components/track/TrackHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import DownloadLink from './DownloadLink'
import { FaCircleInfo, FaEye, FaEyeSlash, FaPenToSquare } from 'react-icons/fa6'
import ContentEditable, { ContentEditableEvent } from 'react-contenteditable'
import React, { useMemo, useRef, useState } from 'react'
import { WayPoint } from '../../@types/gps'
import { generateFeatureCollection, sanitizeFilename } from '../../utils/tools'
import { sanitizeFilename } from '../../utils/tools'
import useLanguage from '../../hooks/useLanguage'

type TrackHeaderProps = {
trackId: string
markerPositions: WayPoint[]
trackname: string
setTrackname: React.Dispatch<React.SetStateAction<string>>
showPolyline: boolean
Expand All @@ -17,7 +15,6 @@ type TrackHeaderProps = {

const TrackHeader: React.FC<TrackHeaderProps> = ({
trackId,
markerPositions,
trackname,
setTrackname,
showPolyline,
Expand All @@ -27,7 +24,6 @@ const TrackHeader: React.FC<TrackHeaderProps> = ({
const tracknameInputFieldRef: React.RefObject<HTMLElement> = useRef(null)
const tracknameRef = useRef('')
const { getMessage } = useLanguage()
const markerGeoJson = useMemo(() => generateFeatureCollection(markerPositions), [markerPositions])

useMemo(() => {
tracknameRef.current = trackname
Expand Down Expand Up @@ -73,20 +69,8 @@ const TrackHeader: React.FC<TrackHeaderProps> = ({
</div>
<div className="flex-1 flex mb-4 justify-end items-center">
<div className="flex flex-row flex-1 justify-end items-center">
<DownloadLink
trackId={trackId}
type="gpx"
trackname={trackname}
optimizeWaypoints={optimizeWaypoints}
geoJson={markerGeoJson}
/>
<DownloadLink
trackId={trackId}
type="tcx"
trackname={trackname}
optimizeWaypoints={optimizeWaypoints}
geoJson={markerGeoJson}
/>
<DownloadLink trackId={trackId} type="gpx" optimizeWaypoints={optimizeWaypoints} />
<DownloadLink trackId={trackId} type="tcx" optimizeWaypoints={optimizeWaypoints} />
</div>
<div className="ml-3 mr-5">
<input
Expand Down
26 changes: 24 additions & 2 deletions src/pages/TrackScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const TrackScreen = () => {
})
}, [trackId])

const throttledMarkerPositions = useThrottle(markerPositions, 0)
const throttledMarkerPositions = useThrottle(markerPositions, 500)
useEffect(() => {
if (throttledMarkerPositions !== undefined && !isLoading) {
const featureCollection = generateFeatureCollection(markerPositions)
Expand All @@ -148,6 +148,29 @@ const TrackScreen = () => {
}
}, [throttledMarkerPositions])

const throttledTrackname = useThrottle(trackname, 500)
useEffect(() => {
if (throttledTrackname !== undefined && !isLoading) {
const feature = {
type: 'Feature',
properties: {
name: throttledTrackname,
},
geometry: {
type: 'LineString',
coordinates: [],
},
}
API.patch(`/tracks/${trackId}`, feature, {
headers: {
'Content-Type': 'application/geo+json',
},
}).catch((error) => {
console.error('Failed to update waypoint', error)
})
}
}, [throttledTrackname])

return (
<>
{isLoading ? (
Expand All @@ -156,7 +179,6 @@ const TrackScreen = () => {
<div className="flex flex-col" style={{ height: '100%' }}>
<TrackHeader
trackId={trackId}
markerPositions={markerPositions}
trackname={trackname}
setTrackname={setTrackname}
showPolyline={showPolyline}
Expand Down

0 comments on commit fe8ede7

Please sign in to comment.