Skip to content

Commit

Permalink
refactor(LaunchTriggerAlert): Remove adaptive router
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Nov 28, 2024
1 parent fb6e817 commit ead73e6
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 50 deletions.
8 changes: 3 additions & 5 deletions packages/cozy-harvest-lib/src/components/cards/ErrorAlert.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types'
import React from 'react'
import { useNavigate } from 'react-router-dom'

import Icon from 'cozy-ui/transpiled/react/Icon'
import InfoIcon from 'cozy-ui/transpiled/react/Icons/Info'
Expand All @@ -22,7 +23,6 @@ import TriggerErrorDescription from '../infos/TriggerErrorDescription'
* @param {string} props.konnectorRoot - The root URL of the konnector.
* @param {Object} props.trigger - The trigger object.
* @param {boolean} props.isRunning - Indicates if the konnector is running.
* @param {string} props.historyAction - The history action.
* @param {Object} props.flow - The flow object.
* @param {Object} props.account - The account object.
* @param {Object} props.intentsApi - The intents API object.
Expand All @@ -35,12 +35,12 @@ function ErrorAlert({
konnectorRoot,
trigger,
isRunning,
historyAction,
flow,
account,
intentsApi,
isRunnable
}) {
const navigate = useNavigate()
const { konnector } = flow
const isKonnectorDisconnected = isDisconnected(konnector, trigger)

Expand All @@ -50,7 +50,7 @@ function ErrorAlert({
account,
intentsApi,
error,
historyAction,
navigate,
konnectorRoot,
trigger,
isRunning,
Expand Down Expand Up @@ -79,7 +79,6 @@ function ErrorAlert({
trigger={trigger}
isInError
error={error}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
Expand Down Expand Up @@ -110,7 +109,6 @@ ErrorAlert.propTypes = {
konnectorRoot: PropTypes.string,
trigger: PropTypes.object.isRequired,
isRunning: PropTypes.bool.isRequired,
historyAction: PropTypes.func.isRequired,
flow: PropTypes.object,
account: PropTypes.object,
intentsApi: PropTypes.object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const meta = {
error: new KonnectorJobError('LOGIN_FAILED'),
konnectorRoot: '/konnector/dummy',
trigger: {},
historyAction: () => {},
flow: {
konnector: {
name: 'Dummy',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check on
import React from 'react'
import { useNavigate } from 'react-router-dom'

import { triggers as triggersModel } from 'cozy-client/dist/models/trigger'
import Button from 'cozy-ui/transpiled/react/Buttons'
Expand All @@ -15,12 +16,12 @@ function LaunchButton({
isInError = false,
isRunning = false,
error,
historyAction,
flow,
account,
intentsApi
} = {}) {
const { t } = useI18n()
const navigate = useNavigate()

const { launch, konnector } = flow

Expand All @@ -29,13 +30,12 @@ function LaunchButton({

const onSync = () => {
if (konnectorPolicy.shouldLaunchRedirectToEdit(error)) {
return historyAction(
return navigate(
konnectorRoot
? `${konnectorRoot}/accounts/${triggersModel.getAccountId(
trigger
)}/edit`
: '/edit',
'push'
: '/edit'
)
} else {
launch({ autoSuccessTimer: false })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import { intentsApiProptype } from '../../helpers/proptypes'
import { findKonnectorPolicy } from '../../konnector-policies'
import { SUCCESS } from '../../models/flowEvents'
import { useFlowState } from '../../models/withConnectionFlow'
import withAdaptiveRouter from '../hoc/withRouter'
import useMaintenanceStatus from '../hooks/useMaintenanceStatus'

export const LaunchTriggerAlert = ({
flow,
t,
konnectorRoot,
historyAction,
intentsApi,
account,
withMaintenanceDescription
Expand Down Expand Up @@ -58,7 +56,6 @@ export const LaunchTriggerAlert = ({
maintenanceMessages={maintenanceMessages}
account={account}
konnectorRoot={konnectorRoot}
historyAction={historyAction}
intentsApi={intentsApi}
flow={flow}
/>
Expand Down Expand Up @@ -93,10 +90,9 @@ LaunchTriggerAlert.propTypes = {
flow: PropTypes.object,
t: PropTypes.func,
konnectorRoot: PropTypes.string,
historyAction: PropTypes.func,
withDescription: PropTypes.bool,
intentsApi: intentsApiProptype,
account: PropTypes.object
}

export default withAdaptiveRouter(LaunchTriggerAlert)
export default LaunchTriggerAlert
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, waitFor, fireEvent } from '@testing-library/react'
import React from 'react'
import { useNavigate } from 'react-router-dom'

import { LaunchTriggerAlert } from './LaunchTriggerAlert'
import AppLike from '../../../test/AppLike'
Expand All @@ -19,6 +20,11 @@ jest.mock('../../models/ConnectionFlow', () => {
return mockConnectionFlow
})

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: jest.fn()
}))

jest.mock('cozy-flags')

jest.mock('../hooks/useMaintenanceStatus', () => () => ({
Expand Down Expand Up @@ -67,6 +73,10 @@ const reconnectErrorTriggerFixture = {

describe('LaunchTriggerAlert', () => {
const setup = ({ trigger, konnector }) => {
const navigateMock = jest.fn()

useNavigate.mockReturnValue(navigateMock)

const client = {
collection: () => {
return {
Expand All @@ -83,18 +93,12 @@ describe('LaunchTriggerAlert', () => {

const flow = new ConnectionFlow(client, trigger, konnector)
flow.launch = jest.fn()
const historyAction = jest.fn()
const root = render(
<AppLike client={client}>
<LaunchTriggerAlert
t={key => key}
flow={flow}
historyAction={historyAction}
account={{}}
/>
<LaunchTriggerAlert t={key => key} flow={flow} account={{}} />
</AppLike>
)
return { root, flow, historyAction, client }
return { root, flow, navigateMock, client }
}
// eslint-disable-next-line no-console
let originalConsoleWarn = console.warn
Expand Down Expand Up @@ -142,13 +146,13 @@ describe('LaunchTriggerAlert', () => {
})

it('should redirect when there is an error which is solvable via reconnect', async () => {
const { root, flow, historyAction } = setup({
const { root, navigateMock, flow } = setup({
trigger: reconnectErrorTriggerFixture,
konnector: konnectorFixture
})
await waitFor(() => root.getByText('Synchronize'))
fireEvent.click(root.getByText('Synchronize'))
expect(historyAction).toHaveBeenCalledWith('/edit', 'push')
expect(navigateMock).toHaveBeenCalledWith('/edit')
expect(flow.launch).not.toHaveBeenCalled()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types'
import React from 'react'
import { useNavigate } from 'react-router-dom'

import Icon from 'cozy-ui/transpiled/react/Icon'
import WrenchCircleIcon from 'cozy-ui/transpiled/react/Icons/WrenchCircle'
Expand All @@ -19,7 +20,6 @@ import TriggerMaintenanceDescription from '../infos/TriggerMaintenanceDescriptio
* @param {boolean} props.withDescription - Indicates if the alert has a description.
* @param {Array} props.messages - The maintenance messages to display.
* @param {string} props.label - The label for the alert.
* @param {string} props.historyAction - The history action for the alert.
* @param {string} props.konnectorRoot - The konnector root for the alert.
* @param {string} props.trigger - The trigger for the alert.
* @param {string} props.konnector - The konnector for the alert.
Expand All @@ -30,19 +30,20 @@ function MaintenanceAlert({
withDescription,
messages,
label,
historyAction,
konnectorRoot,
trigger,
konnector
}) {
const navigate = useNavigate()

const isKonnectorDisconnected = isDisconnected(konnector, trigger)

const isBlock = !!withDescription
const actions = [connectAction, configureAction]
const options = {
historyAction,
konnectorRoot,
trigger,
navigate,
isDisconnected: isKonnectorDisconnected
}

Expand All @@ -68,7 +69,6 @@ MaintenanceAlert.propTypes = {
withDescription: PropTypes.bool,
messages: PropTypes.object,
label: PropTypes.string,
historyAction: PropTypes.func,
konnectorRoot: PropTypes.string,
trigger: PropTypes.object,
konnector: PropTypes.object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types'
import React from 'react'
import { useNavigate } from 'react-router-dom'

import Spinner from 'cozy-ui/transpiled/react/Spinner'
import { useBreakpoints } from 'cozy-ui/transpiled/react/providers/Breakpoints'
Expand All @@ -23,7 +24,6 @@ import KonnectorIcon from '../KonnectorIcon'
* @param {string} props.konnectorRoot - The root of the konnector.
* @param {Object} props.trigger - The trigger object.
* @param {Object} props.error - The error object.
* @param {Function} props.historyAction - The history action function.
* @param {Object} props.flow - The flow object.
* @param {Object} props.account - The account object.
* @param {Object} props.intentsApi - The intents API object.
Expand All @@ -37,13 +37,13 @@ function RunnableAlert({
konnectorRoot,
trigger,
error,
historyAction,
flow,
account,
intentsApi,
isRunnable
}) {
const { isMobile } = useBreakpoints()
const navigate = useNavigate()
const { konnector } = flow
const isKonnectorDisconnected = isDisconnected(konnector, trigger)

Expand All @@ -53,7 +53,7 @@ function RunnableAlert({
account,
intentsApi,
error,
historyAction,
navigate,
konnectorRoot,
trigger,
isRunning,
Expand Down Expand Up @@ -95,7 +95,6 @@ function RunnableAlert({
trigger={trigger}
isRunning={isRunning}
error={error}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
Expand All @@ -112,7 +111,6 @@ RunnableAlert.propTypes = {
konnectorRoot: PropTypes.string,
trigger: PropTypes.object.isRequired,
error: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
historyAction: PropTypes.func.isRequired,
flow: PropTypes.object,
account: PropTypes.object,
intentsApi: PropTypes.object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const meta = {
konnectorRoot: '/konnector/dummy',
trigger: {},
error: undefined,
historyAction: () => {},
flow: {
konnector: {
name: 'Dummy',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const TriggerAlert = ({
maintenanceMessages,
account,
konnectorRoot,
historyAction,
intentsApi,
flow
}) => {
Expand All @@ -40,7 +39,6 @@ const TriggerAlert = ({
withDescription={withMaintenanceDescription}
messages={maintenanceMessages}
isRunnable={isRunnable}
historyAction={historyAction}
konnectorRoot={konnectorRoot}
trigger={trigger}
konnector={konnector}
Expand All @@ -57,7 +55,6 @@ const TriggerAlert = ({
trigger={trigger}
isRunning={isRunning}
isRunnable={isRunnable}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
Expand All @@ -73,7 +70,6 @@ const TriggerAlert = ({
konnectorRoot={konnectorRoot}
trigger={trigger}
error={error}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const meta = {
},
account: {},
konnectorRoot: '/konnector/dummy',
historyAction: () => {},
intentsApi: {},
flow: {
konnector: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

const configureAction = ({
isDisconnected,
historyAction,
navigate,
konnectorRoot,
trigger
}) => ({
name: 'configureAction',
action: () => {
historyAction(
navigate(
konnectorRoot
? `${konnectorRoot}/accounts/${triggersModel.getAccountId(
trigger
)}/config`
: '/config',
'push'
: './config',
{
relative: 'path'
}
)
},
displayCondition: () => !isDisconnected,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import ListItemIcon from 'cozy-ui/transpiled/react/ListItemIcon'
import ListItemText from 'cozy-ui/transpiled/react/ListItemText'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

const connectAction = ({ isDisconnected, konnectorRoot, historyAction }) => ({
const connectAction = ({ isDisconnected, konnectorRoot, navigate }) => ({
name: 'connectAction',
action: () => {
historyAction(`${konnectorRoot}/new`, 'push')
navigate(`${konnectorRoot}/new`)
},
displayCondition: () => isDisconnected,
Component: forwardRef(function ConnectAction(props, ref) {
Expand Down
Loading

0 comments on commit ead73e6

Please sign in to comment.