import React from 'react'
import { useSelector } from 'react-redux'
import { Switch, Route } from 'react-router-dom'

import { selectTrack } from 'redux/tracks'
import { usePageContext } from 'components/PageContext'
import TrackInsights from 'components/TrackInsights'
import TrackMap from 'components/TrackMap'
import TrackGlobe from 'components/TrackGlobe'
import TrackVideoForm from 'components/TrackVideoForm'
import TrackVideo from 'components/TrackVideo'
import TrackWindData from 'components/TrackWindData'
import TrackResults from 'components/TrackResults'

import Header from './Header'
import Navbar from './Navbar'
import { PageContainer, ContentContainer } from './elements'

const TrackShow = () => {
  const { trackId } = usePageContext()

  const track = useSelector(state => selectTrack(state, trackId))

  if (!track || track.status === 'loading') return null

  return (
    <PageContainer>
      <Header track={track} />
      <Navbar track={track} />

      <ContentContainer>
        <Switch>
          <Route path="/tracks/:id/map" component={TrackMap} />
          <Route path="/tracks/:id/globe" component={TrackGlobe} />
          <Route path="/tracks/:id/video" exact component={TrackVideo} />
          <Route path="/tracks/:id/video/edit" component={TrackVideoForm} />
          <Route path="/tracks/:id/wind_data" component={TrackWindData} />
          <Route path="/tracks/:id/results" component={TrackResults} />
          <Route path="/tracks/:id" component={TrackInsights} />
        </Switch>
      </ContentContainer>
    </PageContainer>
  )
}

export default TrackShow

@@ -83,16 +83,26 @@
 h, Route
+, Redirect
  %7D from 
@@ -1014,24 +1014,95 @@
    %3CSwitch%3E%0A
+          %3CRoute path=%22/tracks/:id%22 exact component=%7BTrackInsights%7D /%3E%0A
           %3CR
@@ -1260,20 +1260,18 @@
 :id/
-video%22 exact
+wind_data%22
  com
@@ -1287,90 +1287,16 @@
 rack
-Video%7D /%3E%0A          %3CRoute path=%22/tracks/:id/video/edit%22 component=%7BTrackVideoForm
+WindData
 %7D /%3E
@@ -1327,33 +1327,31 @@
 /tracks/:id/
-wind_data
+results
 %22 component=
@@ -1352,37 +1352,69 @@
 onent=%7BTrack
-WindData
+Results
 %7D /%3E%0A
+          %7Btrack.hasVideo && (%0A  
           %3CR
@@ -1432,32 +1432,36 @@
 /tracks/:id/
-results%22
+video%22 exact
  component=%7B
@@ -1461,36 +1461,80 @@
 onent=%7BTrack
-Results
+Video
 %7D /%3E%0A
+          )%7D%0A          %7Btrack.editable && (%0A  
           %3CR
@@ -1555,16 +1555,27 @@
 acks/:id
+/video/edit
 %22 compon
@@ -1580,32 +1580,124 @@
 onent=%7BTrack
-Insights
+VideoForm%7D /%3E%0A          )%7D%0A%0A          %3CRoute component=%7B() =%3E %3CRedirect to=%7B%60/tracks/$%7BtrackId%7D%60%7D /%3E
 %7D /%3E%0A       

Disable video routes and catch route