Skip to content

Commit

Permalink
Merge pull request #5 from Palmabit-IT/ref-getLink
Browse files Browse the repository at this point in the history
Ref (Routes): getLink
  • Loading branch information
Giuseppe Aremare committed Sep 27, 2018
2 parents 61f48c7 + d8e683d commit 7d45b31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ npm-debug.log
coverage

TARGET.md
.vscode
.vscode
.idea
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"babel-polyfill": "^6.26.0",
"eslint": "^5.5.0",
"eslint-plugin-jest": "^21.22.0",
"lodash.pick": "^4.4.0",
"path-to-regexp": "^1.7.0",
"prop-types": "^15.6.1",
"remove-trailing-separator": "^1.1.0"
Expand Down
40 changes: 16 additions & 24 deletions src/Routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse } from 'url'
import pick from 'lodash.pick'
import NextLink from 'next/link'
import NextRouter from 'next/router'
import Route from './Route'
Expand Down Expand Up @@ -148,32 +149,23 @@ export default class Routes {

getLink(Link) {
const LinkRoutes = props => {
const { href, route, locale, params, ...newProps } = props
const locale2 = locale || this.locale

if (href) {
const parsedUrl = parse(href)

if ((parsedUrl.hostname !== null || href[0] === '/' || href[0] === '#')) {
let propsToPass
if (Link.propTypes) {
const allowedKeys = Object.keys(Link.propTypes)
propsToPass = allowedKeys.reduce((obj, key) => {
// @TODO remove me
props.hasOwnProperty(key) && (obj[key] = props[key]) // eslint-disable-line no-unused-expressions
return obj
}, {})
} else {
propsToPass = props
}
return <Link {...propsToPass} />
}

const { href, locale = this.locale, route, params, ...newProps } = props
const { hostname } = href ? parse(href) : {}
const firstRef = href && href[0]
const mustPassProps = hostname || ['/', '#'].includes(firstRef)
let propsToPass

if (mustPassProps) {
const { propTypes } = Link
const ownProps = propTypes && pick(props, Object.keys(props))
propsToPass = propTypes ? pick(ownProps, Object.keys(propTypes)) : props
}
else {
const { urls } = this.findAndGetUrls(route, locale, params)
propsToPass = { ...newProps, ...urls }
}

Object.assign(newProps, this.findAndGetUrls(route, locale2, params).urls)

return <Link {...newProps} />
return <Link {...propsToPass} />
}
return LinkRoutes
}
Expand Down

0 comments on commit 7d45b31

Please sign in to comment.