daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest May 16th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const connectedRoute = connect(state => ({
  2.   activePostcodeType: getActivePostcodeType(state),
  3.   activePostcode: getActivePostcode(state),
  4.   postcodePrimary: getPostcodePrimary(state),
  5.   postcodeSecondary: getPostcodeSecondary(state),
  6.   postcodeTertiary: getPostcodeTertiary(state),
  7.   chosenTownsPrimary: getChosenTownsPrimary(state),
  8.   chosenTownsSecondary: getChosenTownsSecondary(state),
  9.   chosenTownsTertiary: getChosenTownsTertiary(state)
  10. }), {
  11.   setActiveLocation,
  12.   setChosenTowns,
  13.   fetchLocation,
  14.   fetchFeed
  15. }, ({
  16.   postcodePrimary,
  17.   postcodeSecondary,
  18.   postcodeTertiary,
  19.   chosenTownsPrimary,
  20.   chosenTownsSecondary,
  21.   chosenTownsTertiary
  22. }, {
  23.   setActiveLocation,
  24.   setChosenTowns,
  25.   fetchLocation,
  26.   fetchFeed
  27. }, {
  28.   version,
  29.   feedType,
  30.   ...ownProps
  31. }) => ({
  32.   setInitialLocation ({ postcode, locations }) {
  33.     const formattedUrlPostcode: string = postcode.replace(/ /g, '').toUpperCase()
  34.     const formattedLocations: Array<string> = locations.split(',')
  35.     const chosenTownsTable: ChosenTownsTable = {
  36.       [POSTCODE_TYPES.PRIMARY]: chosenTownsPrimary,
  37.       [POSTCODE_TYPES.SECONDARY]: chosenTownsSecondary,
  38.       [POSTCODE_TYPES.TERTIARY]: chosenTownsTertiary
  39.     }
  40.  
  41.     function switchToPostcode ({ type }: { type: string }): void {
  42.       setChosenTowns({ chosenTowns: filterUnique([...formattedLocations, ...chosenTownsTable[type]]), type })
  43.       fetchFeed({ postcode, locations: formattedLocations, version, type })
  44.       setActiveLocation({ active: type })
  45.     }
  46.  
  47.     function setNewPostcode ({ type }: { type: string }): void {
  48.       setActiveLocation({ active: type })
  49.       setChosenTowns({ chosenTowns: formattedLocations, type })
  50.       fetchLocation({ postcode: formattedUrlPostcode, version, type })
  51.       fetchFeed({ postcode: formattedUrlPostcode, locations: formattedLocations, version, type })
  52.     }
  53.  
  54.     // If postcode in URL matches a saved one, switch to that and update locations
  55.     switch (formattedUrlPostcode) {
  56.       case postcodePrimary: return switchToPostcode({ type: POSTCODE_TYPES.PRIMARY })
  57.       case postcodeSecondary: return switchToPostcode({ type: POSTCODE_TYPES.SECONDARY })
  58.       case postcodeTertiary: return switchToPostcode({ type: POSTCODE_TYPES.TERTIARY })
  59.  
  60.       // Add the new postcode if there is room
  61.       default:
  62.         if (!postcodePrimary) setNewPostcode({ type: POSTCODE_TYPES.PRIMARY })
  63.         else if (!postcodeSecondary) setNewPostcode({ type: POSTCODE_TYPES.SECONDARY })
  64.         else if (!postcodeTertiary) setNewPostcode({ type: POSTCODE_TYPES.TERTIARY })
  65.     }
  66.   },
  67.   version,
  68.   ...ownProps
  69. }))
  70.  
  71. const LiveFeedRoute = connectedRoute(LiveFeed)
  72. const NewsFeedRoute = connectedRoute(NewsFeed)
  73. const DiscoverFeedRoute = connectedRoute(DiscoverFeed)
  74.  
  75. export {
  76.   LiveFeedRoute,
  77.   NewsFeedRoute,
  78.   DiscoverFeedRoute
  79. }
  80.    
  81. export default function LiveFeedRoute ({ version, setInitialLocation }: Props): Element<typeof Route> {
  82.   return (
  83.     <Route
  84.       exact
  85.       path='/feed/:postcode/:locations'
  86.       render={({ match: { params: { postcode, locations } } }) => (
  87.           <Feed
  88.             enableNewsItemControls
  89.             enableMenu
  90.             sideBarStickyHeight={77}
  91.             postcode={postcode}
  92.             locations={locations}
  93.             version={version}
  94.             setInitialLocation={() => setInitialLocation({ postcode, locations })}
  95.             head={
  96.               <CreatePost />
  97.             }
  98.           />
  99.         )}
  100.     />
  101.   )
  102. }
  103.    
  104. export default function FeedApp ({ version = '0-0-5' }: Props): Element<typeof VersionProvider> {
  105.   return (
  106.     <VersionProvider value={version}>
  107.       <Header control sticky />
  108.       <LiveFeedRoute version={version} />
  109.       <NewsFeedRoute version={version} />
  110.       <DiscoverFeedRoute version={version} />
  111.       <Footer />
  112.       <Modal />
  113.       <Drawer />
  114.       <Notification />
  115.     </VersionProvider>
  116.   )
  117. }
  118.    
  119. hydrate((
  120.   <BrowserRouter>
  121.     <Provider store={store}>
  122.       <PersistGate persistor={persistor}>
  123.         <App />
  124.       </PersistGate>
  125.     </Provider>
  126.   </BrowserRouter>
  127. ), document.getElementById('root'))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top