Skip to content

Commit

Permalink
feat: Add intentsApi prop to TriggerManager
Browse files Browse the repository at this point in the history
To allow custom call to :
 - fetch session code
 - show inApp browser
 - close inApp browser

For cases outside of flagship app
  • Loading branch information
doubleface committed Jun 13, 2022
1 parent e2037ef commit eb232a0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
33 changes: 24 additions & 9 deletions packages/cozy-harvest-lib/src/components/InAppBrowser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ import PropTypes from 'prop-types'
import { useWebviewIntent } from 'cozy-intent'
import logger from '../logger'

const InAppBrowser = ({ url, onClose }) => {
const InAppBrowser = ({ url, onClose, intentsApi = {} }) => {
const webviewIntent = useWebviewIntent()
const fetchSessionCode = intentsApi?.fetchSessionCode
? intentsApi?.fetchSessionCode
: () => webviewIntent.call('fetchSessionCode')
const showInAppBrowser = intentsApi?.showInAppBrowser
? intentsApi?.showInAppBrowser
: url => webviewIntent.call('showInAppBrowser', { url })
const closeInAppBrowser = intentsApi?.closeInAppBrowser
? intentsApi?.closeInAppBrowser
: () => webviewIntent.call('closeInAppBrowser')

const ready = Boolean(
webviewIntent ||
(intentsApi?.fetchSessionCode &&
intentsApi?.showInAppBrowser &&
intentsApi?.closeInAppBrowser)
)

useEffect(() => {
async function insideEffect() {
if (webviewIntent) {
if (ready) {
try {
const sessionCode = await webviewIntent.call('fetchSessionCode')
const sessionCode = await fetchSessionCode()
logger.debug('got session code', sessionCode)
const iabUrl = new URL(url)
iabUrl.searchParams.append('session_code', sessionCode)
const result = await webviewIntent.call('showInAppBrowser', {
url: iabUrl.toString()
})
const result = await showInAppBrowser(iabUrl.toString())
if (result?.type !== 'dismiss' && result?.type !== 'cancel') {
logger.error('Unexpected InAppBrowser result', result)
}
Expand All @@ -30,15 +44,16 @@ const InAppBrowser = ({ url, onClose }) => {
}
insideEffect()
return function cleanup() {
webviewIntent.call('closeInAppBrowser')
closeInAppBrowser()
}
}, [webviewIntent, url, onClose])
}, [ready, url, onClose])
return null
}

InAppBrowser.propTypes = {
url: PropTypes.string.isRequired,
onClose: PropTypes.func
onClose: PropTypes.func,
intentsApi: PropTypes.object
}

export default InAppBrowser
23 changes: 23 additions & 0 deletions packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,27 @@ describe('InAppBrowser', () => {
unmount()
expect(webviewService.call).toHaveBeenNthCalledWith(2, 'closeInAppBrowser')
})

it('should work with custom intents api', async () => {
const url = 'https://test.url'
const intentsApi = {
fetchSessionCode: jest.fn().mockResolvedValue('custom_api_session_code'),
showInAppBrowser: jest.fn(),
closeInAppBrowser: jest.fn()
}
const { unmount } = render(
<InAppBrowser url={url} intentsApi={intentsApi} />
)

await waitFor(() => expect(intentsApi.showInAppBrowser).toHaveBeenCalled())

unmount()

await waitFor(() => expect(intentsApi.closeInAppBrowser).toHaveBeenCalled())
expect(intentsApi.fetchSessionCode).toHaveBeenCalledTimes(1)
expect(intentsApi.showInAppBrowser).toHaveBeenNthCalledWith(
1,
'https://test.url/?session_code=custom_api_session_code'
)
})
})
10 changes: 8 additions & 2 deletions packages/cozy-harvest-lib/src/components/OAuthForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ export class OAuthForm extends PureComponent {
}

render() {
const { konnector, t, flowState, reconnect, account } = this.props
const { konnector, t, flowState, reconnect, account, intentsApi } = this.props
const { showOAuthWindow, needExtraParams, extraParams } = this.state
const { konnector, t, flowState, intentsApi } = this.props
const { initialValues, showOAuthWindow, needExtraParams, extraParams } =
this.state
const isBusy =
showOAuthWindow === true ||
flowState.running ||
Expand All @@ -97,6 +100,7 @@ export class OAuthForm extends PureComponent {
onSuccess={this.handleAccountId}
onCancel={this.handleOAuthCancel}
account={account}
intentsApi={intentsApi}
/>
)}
</>
Expand All @@ -114,7 +118,9 @@ OAuthForm.propTypes = {
/** Translation function */
t: PropTypes.func.isRequired,
/** Is it a reconnection or not */
reconnect: PropTypes.bool
reconnect: PropTypes.bool,
/** custom intents api. Can have fetchSessionCode, showInAppBrowser, closeInAppBrowser at the moment */
intentsApi: PropTypes.object
}

export default compose(translate(), withConnectionFlow())(OAuthForm)
12 changes: 9 additions & 3 deletions packages/cozy-harvest-lib/src/components/OAuthWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class OAuthWindow extends PureComponent {
}

render() {
const { t } = this.props
const { t, intentsApi } = this.props
const { oAuthUrl, succeed } = this.state
return (
oAuthUrl &&
Expand All @@ -156,7 +156,11 @@ export class OAuthWindow extends PureComponent {
title={t(`oauth.window.title`)}
/>
) : (
<InAppBrowser url={oAuthUrl} onClose={this.handleClose} />
<InAppBrowser
url={oAuthUrl}
onClose={this.handleClose}
intentsApi={intentsApi}
/>
))
)
}
Expand All @@ -177,7 +181,9 @@ OAuthWindow.propTypes = {
/** Is it a reconnection or not */
reconnect: PropTypes.bool,
/** Existing account */
account: PropTypes.object
account: PropTypes.object,
/** custom intents api. Can have fetchSessionCode, showInAppBrowser, closeInAppBrowser at the moment */
intentsApi: PropTypes.object
}

export default translate()(withClient(OAuthWindow))
6 changes: 5 additions & 1 deletion packages/cozy-harvest-lib/src/components/TriggerManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ export class DumbTriggerManager extends Component {
flowState,
client,
OAuthFormWrapperComp,
reconnect
reconnect,
intentsApi
} = this.props

const submitting = flowState.running
Expand Down Expand Up @@ -349,6 +350,7 @@ export class DumbTriggerManager extends Component {
reconnect={reconnect}
konnector={konnector}
onSuccess={this.handleOAuthAccountId}
intentsApi={intentsApi}
/>
</Wrapper>
)
Expand Down Expand Up @@ -449,6 +451,8 @@ DumbTriggerManager.propTypes = {
OAuthFormWrapperComp: PropTypes.node,
/** Is it a reconnection or not */
reconnect: PropTypes.bool,
// custom intents api. Can have fetchSessionCode, showInAppBrowser, closeInAppBrowser at the moment
intentsApi: PropTypes.object
}

const TriggerManager = compose(
Expand Down

0 comments on commit eb232a0

Please sign in to comment.