From ead73e6af1a45e97eeae86e044ede4fd7dba79c9 Mon Sep 17 00:00:00 2001 From: cballevre Date: Mon, 25 Nov 2024 10:56:36 +0100 Subject: [PATCH] refactor(LaunchTriggerAlert): Remove adaptive router --- .../src/components/cards/ErrorAlert.jsx | 8 +++---- .../components/cards/ErrorAlert.stories.jsx | 1 - .../src/components/cards/LaunchButton.jsx | 8 +++---- .../components/cards/LaunchTriggerAlert.jsx | 6 +---- .../cards/LaunchTriggerAlert.spec.jsx | 24 +++++++++++-------- .../src/components/cards/MaintenanceAlert.jsx | 8 +++---- .../src/components/cards/RunnableAlert.jsx | 8 +++---- .../cards/RunnableAlert.stories.jsx | 1 - .../src/components/cards/TriggerAlert.jsx | 4 ---- .../components/cards/TriggerAlert.stories.jsx | 1 - .../cards/actions/configureAction.js | 10 ++++---- .../components/cards/actions/connectAction.js | 4 ++-- .../components/cards/actions/launchAction.js | 10 ++++---- 13 files changed, 43 insertions(+), 50 deletions(-) diff --git a/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.jsx b/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.jsx index f2f134dec9..b7c904ed47 100644 --- a/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.jsx @@ -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' @@ -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. @@ -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) @@ -50,7 +50,7 @@ function ErrorAlert({ account, intentsApi, error, - historyAction, + navigate, konnectorRoot, trigger, isRunning, @@ -79,7 +79,6 @@ function ErrorAlert({ trigger={trigger} isInError error={error} - historyAction={historyAction} flow={flow} account={account} intentsApi={intentsApi} @@ -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, diff --git a/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.stories.jsx b/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.stories.jsx index da448deee2..116a602ac7 100644 --- a/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.stories.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/ErrorAlert.stories.jsx @@ -8,7 +8,6 @@ const meta = { error: new KonnectorJobError('LOGIN_FAILED'), konnectorRoot: '/konnector/dummy', trigger: {}, - historyAction: () => {}, flow: { konnector: { name: 'Dummy', diff --git a/packages/cozy-harvest-lib/src/components/cards/LaunchButton.jsx b/packages/cozy-harvest-lib/src/components/cards/LaunchButton.jsx index 5f7cd0dd85..e481e88a74 100644 --- a/packages/cozy-harvest-lib/src/components/cards/LaunchButton.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/LaunchButton.jsx @@ -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' @@ -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 @@ -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 }) diff --git a/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.jsx b/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.jsx index 6461ea88b6..a57f8553db 100644 --- a/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.jsx @@ -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 @@ -58,7 +56,6 @@ export const LaunchTriggerAlert = ({ maintenanceMessages={maintenanceMessages} account={account} konnectorRoot={konnectorRoot} - historyAction={historyAction} intentsApi={intentsApi} flow={flow} /> @@ -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 diff --git a/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.spec.jsx b/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.spec.jsx index 8c34d95040..6f94ba948c 100644 --- a/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.spec.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/LaunchTriggerAlert.spec.jsx @@ -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' @@ -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', () => () => ({ @@ -67,6 +73,10 @@ const reconnectErrorTriggerFixture = { describe('LaunchTriggerAlert', () => { const setup = ({ trigger, konnector }) => { + const navigateMock = jest.fn() + + useNavigate.mockReturnValue(navigateMock) + const client = { collection: () => { return { @@ -83,18 +93,12 @@ describe('LaunchTriggerAlert', () => { const flow = new ConnectionFlow(client, trigger, konnector) flow.launch = jest.fn() - const historyAction = jest.fn() const root = render( - key} - flow={flow} - historyAction={historyAction} - account={{}} - /> + key} flow={flow} account={{}} /> ) - return { root, flow, historyAction, client } + return { root, flow, navigateMock, client } } // eslint-disable-next-line no-console let originalConsoleWarn = console.warn @@ -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() }) diff --git a/packages/cozy-harvest-lib/src/components/cards/MaintenanceAlert.jsx b/packages/cozy-harvest-lib/src/components/cards/MaintenanceAlert.jsx index ee269bf816..56919d7cdc 100644 --- a/packages/cozy-harvest-lib/src/components/cards/MaintenanceAlert.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/MaintenanceAlert.jsx @@ -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' @@ -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. @@ -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 } @@ -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 diff --git a/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.jsx b/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.jsx index 809255935b..f74c114221 100644 --- a/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.jsx @@ -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' @@ -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. @@ -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) @@ -53,7 +53,7 @@ function RunnableAlert({ account, intentsApi, error, - historyAction, + navigate, konnectorRoot, trigger, isRunning, @@ -95,7 +95,6 @@ function RunnableAlert({ trigger={trigger} isRunning={isRunning} error={error} - historyAction={historyAction} flow={flow} account={account} intentsApi={intentsApi} @@ -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, diff --git a/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.stories.jsx b/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.stories.jsx index 8d22d33915..e054c1dd6e 100644 --- a/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.stories.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/RunnableAlert.stories.jsx @@ -9,7 +9,6 @@ const meta = { konnectorRoot: '/konnector/dummy', trigger: {}, error: undefined, - historyAction: () => {}, flow: { konnector: { name: 'Dummy', diff --git a/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.jsx b/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.jsx index baf59277f2..9abf36e9ea 100644 --- a/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.jsx @@ -18,7 +18,6 @@ const TriggerAlert = ({ maintenanceMessages, account, konnectorRoot, - historyAction, intentsApi, flow }) => { @@ -40,7 +39,6 @@ const TriggerAlert = ({ withDescription={withMaintenanceDescription} messages={maintenanceMessages} isRunnable={isRunnable} - historyAction={historyAction} konnectorRoot={konnectorRoot} trigger={trigger} konnector={konnector} @@ -57,7 +55,6 @@ const TriggerAlert = ({ trigger={trigger} isRunning={isRunning} isRunnable={isRunnable} - historyAction={historyAction} flow={flow} account={account} intentsApi={intentsApi} @@ -73,7 +70,6 @@ const TriggerAlert = ({ konnectorRoot={konnectorRoot} trigger={trigger} error={error} - historyAction={historyAction} flow={flow} account={account} intentsApi={intentsApi} diff --git a/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.stories.jsx b/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.stories.jsx index 785d0d148e..874d1ada9d 100644 --- a/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.stories.jsx +++ b/packages/cozy-harvest-lib/src/components/cards/TriggerAlert.stories.jsx @@ -20,7 +20,6 @@ const meta = { }, account: {}, konnectorRoot: '/konnector/dummy', - historyAction: () => {}, intentsApi: {}, flow: { konnector: { diff --git a/packages/cozy-harvest-lib/src/components/cards/actions/configureAction.js b/packages/cozy-harvest-lib/src/components/cards/actions/configureAction.js index cb9a976f8c..0a8c88319a 100644 --- a/packages/cozy-harvest-lib/src/components/cards/actions/configureAction.js +++ b/packages/cozy-harvest-lib/src/components/cards/actions/configureAction.js @@ -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, diff --git a/packages/cozy-harvest-lib/src/components/cards/actions/connectAction.js b/packages/cozy-harvest-lib/src/components/cards/actions/connectAction.js index 37786e55b4..faaac094d9 100644 --- a/packages/cozy-harvest-lib/src/components/cards/actions/connectAction.js +++ b/packages/cozy-harvest-lib/src/components/cards/actions/connectAction.js @@ -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) { diff --git a/packages/cozy-harvest-lib/src/components/cards/actions/launchAction.js b/packages/cozy-harvest-lib/src/components/cards/actions/launchAction.js index fb8061c301..f7c21f850b 100644 --- a/packages/cozy-harvest-lib/src/components/cards/actions/launchAction.js +++ b/packages/cozy-harvest-lib/src/components/cards/actions/launchAction.js @@ -16,7 +16,7 @@ const launchAction = ({ account, intentsApi, error, - historyAction, + navigate, konnectorRoot, trigger, isDisconnected @@ -32,13 +32,15 @@ const launchAction = ({ const onSync = () => { if (konnectorPolicy.shouldLaunchRedirectToEdit(error)) { - return historyAction( + return navigate( konnectorRoot ? `${konnectorRoot}/accounts/${triggersModel.getAccountId( trigger )}/edit` - : '/edit', - 'push' + : './edit', + { + relative: 'path' + } ) } else { launch({ autoSuccessTimer: false })