Skip to content

Commit

Permalink
Merge pull request #974 from cozy/feat/OverrideAppLinker
Browse files Browse the repository at this point in the history
feat: Use static method for AppLinker
  • Loading branch information
Crash-- authored May 13, 2019
2 parents 16e6b14 + 6530685 commit 18a1251
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 60 deletions.
117 changes: 61 additions & 56 deletions react/AppLinker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ export class AppLinker extends React.Component {

constructor(props) {
super(props)
this.openWeb = this.openWeb.bind(this)
this.openNativeFromNative = this.openNativeFromNative.bind(this)
this.openNativeFromWeb = this.openNativeFromWeb.bind(this)
}

componentDidMount() {
Expand All @@ -50,81 +47,88 @@ export class AppLinker extends React.Component {
}
}

onAppSwitch() {
const { onAppSwitch } = this.props
if (typeof onAppSwitch === 'function') {
onAppSwitch()
}
}

openNativeFromNative(ev) {
if (ev) {
ev.preventDefault()
}
const { slug } = this.props
const appInfo = NATIVE_APP_INFOS[slug]
this.onAppSwitch()
startApp(appInfo).catch(err => {
console.error('AppLinker: Could not open native app', err)
})
}

//Will be removed soon when Android universallink will be tested
openNativeFromWeb(ev) {
if (ev) {
ev.preventDefault()
}

const { href, slug, nativePath } = this.props
const appInfo = NATIVE_APP_INFOS[slug]

this.onAppSwitch()

openDeeplinkOrRedirect(
appInfo.uri + (nativePath === '/' ? '' : nativePath),
function() {
window.location.href = href
}
)
}

openWeb() {
this.onAppSwitch()
}

render() {
const { children, slug, nativePath } = this.props
const { nativeAppIsAvailable } = this.state

const appInfo = NATIVE_APP_INFOS[slug]
let href = this.props.href
static getOnClickHref(props, nativeAppIsAvailable) {
const { slug, nativePath } = props
let href = props.href
let onClick = null
const usingNativeApp = isMobileApp()

const appInfo = NATIVE_APP_INFOS[slug]
if (usingNativeApp) {
if (nativeAppIsAvailable) {
// If we are on the native app and the other native app is available,
// we open the native app
onClick = this.openNativeFromNative
onClick = AppLinker.openNativeFromNative.bind(this, props)
href = '#'
} else {
// If we are on a native app, but the other native app is not available
// we open the web link, this is done by the href prop. We still
// have to call the prop callback
onClick = this.openWeb
onClick = AppLinker.openWeb.bind(this, props)
}
} else if (isMobile() && appInfo) {
// If we are on the "mobile web version", we try to open the native app
// if it exists with an universal links. If it fails, we redirect to the web
// version of the requested app
// Only on iOS ATM
if (isAndroid()) {
onClick = this.openNativeFromWeb
onClick = AppLinker.openNativeFromWeb.bind(this, props)
} else {
href = generateUniversalLink({ slug, nativePath, fallbackUrl: href })
}
}

return {
href,
onClick
}
}
static openNativeFromWeb(props, ev) {
const { href, slug, nativePath, onAppSwitch } = props
const appInfo = NATIVE_APP_INFOS[slug]

if (ev) {
ev.preventDefault()
}

AppLinker.onAppSwitch(onAppSwitch)
openDeeplinkOrRedirect(
appInfo.uri + (nativePath === '/' ? '' : nativePath),
function() {
window.location.href = href
}
)
}

static onAppSwitch(onAppSwitchFn) {
if (typeof onAppSwitchFn === 'function') {
onAppSwitchFn()
}
}

static openNativeFromNative(props, ev) {
const { slug, onAppSwitch } = props
if (ev) {
ev.preventDefault()
}
const appInfo = NATIVE_APP_INFOS[slug]
AppLinker.onAppSwitch(onAppSwitch)
startApp(appInfo).catch(err => {
console.error('AppLinker: Could not open native app', err)
})
}

static openWeb(props) {
AppLinker.onAppSwitch(props.onAppSwitch)
}

render() {
const { children, slug } = this.props
const { nativeAppIsAvailable } = this.state
const appInfo = NATIVE_APP_INFOS[slug]
const { href, onClick } = AppLinker.getOnClickHref(
this.props,
nativeAppIsAvailable
)
return children({ ...appInfo, onClick: onClick, href })
}
}
Expand All @@ -143,7 +147,8 @@ AppLinker.propTypes = {
/*
Path used for "native link"
*/
nativePath: PropTypes.string
nativePath: PropTypes.string,
onAppSwitch: PropTypes.func
}

export default AppLinker
Expand Down
5 changes: 1 addition & 4 deletions react/AppLinker/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ describe('app icon', () => {
throw new Error(message)
}
})
openNativeFromNativeSpy = jest.spyOn(
AppLinker.prototype,
'openNativeFromNative'
)
openNativeFromNativeSpy = jest.spyOn(AppLinker, 'openNativeFromNative')
isMobileApp.mockReturnValue(false)
isMobile.mockReturnValue(false)
isAndroid.mockReturnValue(false)
Expand Down

0 comments on commit 18a1251

Please sign in to comment.