Skip to content

Commit

Permalink
Merge pull request #86 from pturchik/master
Browse files Browse the repository at this point in the history
fix(app): set $render params before route render
  • Loading branch information
pturchik committed Apr 23, 2020
2 parents 422ecee + a61aac8 commit 9f33362
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
9 changes: 1 addition & 8 deletions packages/app/client/Router/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React, { useMemo, useState, useEffect } from 'react'
import {
$root,
observer,
emit,
initLocalCollection
emit
} from 'startupjs'
import { Route } from 'react-router'
import { Dimensions, Platform, View } from 'react-native'
Expand All @@ -18,7 +17,6 @@ export default observer(function Routes ({
...props
}) {
function render (route, props) {
setRenderParams(props)
return pug`
//- TODO: We can remove passing props because
//- in pages we can use react-router hooks for this
Expand Down Expand Up @@ -101,8 +99,3 @@ function getOrientation () {
const dim = Dimensions.get('screen')
return dim.width >= dim.height ? 'landscape' : 'portrait'
}

function setRenderParams ({ location, match }) {
if (!$root.get('$render')) initLocalCollection('$render')
$root.setDiffDeep('$render.params', match.params)
}
24 changes: 21 additions & 3 deletions packages/app/client/Router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useMemo } from 'react'
import RouterComponent from './RouterComponent'
import { withRouter, useHistory } from 'react-router-native'
import { matchRoutes } from 'react-router-config'
import { $root, observer, useSyncEffect, initLocalCollection } from 'startupjs'
import { Linking, Platform } from 'react-native'
import { matchPath } from 'react-router'
Expand Down Expand Up @@ -32,8 +33,12 @@ const AppsFactoryWithRouter = withRouter(observer(function AppsFactory ({
}, [location.pathname])

useSyncEffect(() => {
initRoute(location)
const unlisten = history.listen(initRoute)
initRoute(location, routes, goTo)

const unlisten = history.listen((location) => {
initRoute(location, routes, goTo)
})

$root.on('url', goTo)

return () => {
Expand Down Expand Up @@ -90,7 +95,7 @@ function getApp (url, routes) {
return route ? route.app : null
}

function initRoute (location) {
function initRoute (location, routes, goTo) {
// Check if url or search changed between page rerenderings
const prevUrl = $root.get('$render.url')
const prevSearch = $root.get('$render.search')
Expand All @@ -103,7 +108,20 @@ function initRoute (location) {
$root.setDiff('$render.url', url)
$root.setDiff('$render.search', search)
$root.setDiffDeep('$render.query', query)

if (url !== prevUrl) {
const matched = matchRoutes(routes, url)
if (matched.length) {
const lastRoute = matched[matched.length - 1]
const redirect = lastRoute.route.redirect

if (redirect) {
setTimeout(() => $root.emit('url', redirect, { replace: true }))
return
}

$root.setDiffDeep('$render.params', lastRoute.match.params)
}
$root.setDiff('_session.url', location.pathname) // TODO: DEPRECATED
$root.silent().destroy('_page')
initLocalCollection('_page')
Expand Down
2 changes: 1 addition & 1 deletion packages/routes-middleware/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function matchAppRoutes (location, appRoutes, cb) {

function matchUrl (location, routes, cb) {
const matched = matchRoutes(routes, location.replace(/\?.*/, ''))
if (matched && matched.length) {
if (matched.length) {
// check if the last route has redirect
const lastRoute = matched[matched.length - 1]
if (lastRoute.route.redirect) {
Expand Down
6 changes: 5 additions & 1 deletion styleguide/Root/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { BASE_URL } from '@env'
import init from 'startupjs/init'
import orm from '../model'
import React, { useMemo } from 'react'
import { Platform } from 'react-native'
import App from 'startupjs/app'
import { observer, useModel, useSession, useDoc } from 'startupjs'
import { $root, observer, useModel, useSession, useDoc } from 'startupjs'

// Frontend micro-services
import * as main from '../main'
Expand All @@ -19,6 +20,9 @@ import docs from '../docs'
// Initialization must start before doing any subscribes to data.
init({ baseUrl: BASE_URL, orm })

// TODO: Remove in prod
if (Platform.OS === 'web') window.model = $root

// Global initialization logic
function useGlobalInit (session) {
let $session = useModel('_session')
Expand Down
9 changes: 1 addition & 8 deletions styleguide/main/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import { useEffect } from 'react'
import { emit } from 'startupjs'
import getRoutes from './routes'

export const Layout = ({ children }) => children
export const routes = getRoutes({
PHome: () => {
useEffect(() => { emit('url', '/docs', { replace: true }) }, [])
return null
}
})
export const routes = getRoutes({})
2 changes: 1 addition & 1 deletion styleguide/main/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export default (components = {}) => [
{
path: '/',
exact: true,
component: components.PHome
redirect: '/docs'
}
]

0 comments on commit 9f33362

Please sign in to comment.