Skip to content

Commit

Permalink
feat: Add TriggerAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Dec 6, 2023
1 parent 28a9516 commit 0570f44
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 85 deletions.
85 changes: 85 additions & 0 deletions packages/cozy-harvest-lib/src/components/cards/TriggerAlert.jsx
@@ -0,0 +1,85 @@
import React from 'react'

import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { ErrorAlert } from './ErrorAlert'
import { MaintenanceAlert } from './MaintenanceAlert'
import { RunnableAlert } from './RunnableAlert'
import { makeLabel } from './helpers'

const TriggerAlert = ({
trigger,
isInMaintenance,
isInError,
isRunning,
isRunnable,
withMaintenanceDescription,
error,
maintenanceMessages,
account,
konnectorRoot,
historyAction,
intentsApi,
flow
}) => {
const { t } = useI18n()
const { konnector } = flow

const label = makeLabel({
t,
konnector,
trigger,
isRunning,
isInMaintenance
})

if (isInMaintenance) {
return (
<MaintenanceAlert
label={label}
withDescription={withMaintenanceDescription}
messages={maintenanceMessages}
isRunnable={isRunnable}
historyAction={historyAction}
konnectorRoot={konnectorRoot}
trigger={trigger}
konnector={konnector}
/>
)
}

if (isInError) {
return (
<ErrorAlert
label={label}
error={error}
konnectorRoot={konnectorRoot}
trigger={trigger}
isRunning={isRunning}
isRunnable={isRunnable}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
/>
)
}

return (
<RunnableAlert
label={label}
isRunning={isRunning}
konnectorSlug={konnector.slug}
konnectorRoot={konnectorRoot}
trigger={trigger}
error={error}
historyAction={historyAction}
flow={flow}
account={account}
intentsApi={intentsApi}
isRunnable={isRunnable}
/>
)
}

export { TriggerAlert }
@@ -0,0 +1,52 @@
import { TriggerAlert } from './TriggerAlert'
import { KonnectorJobError } from '../../helpers/konnectors'

const meta = {
component: TriggerAlert,
args: {
trigger: {},
isInMaintenance: false,
isInError: false,
isRunnable: true,
isRunning: false,
expectingTriggerLaunch: false,
withMaintenanceDescription: false,
error: new KonnectorJobError('LOGIN_FAILED'),
maintenanceMessages: {
en: {
long_message:
"We're currently working on this service. Please try again later."
}
},
account: {},
konnectorRoot: '/konnector/dummy',
historyAction: () => {},
intentsApi: {},
flow: {
konnector: {
name: 'Dummy',
type: 'konnector',
slug: 'dummy',
locales: {
en: {
description: 'Konnector used for debug purpose',
fields: {
date: {
label: 'Date'
},
error: {
label: 'Intended error'
}
}
}
}
}
}
}
}

export default meta

export const Default = {
name: 'Default'
}
33 changes: 7 additions & 26 deletions packages/cozy-harvest-lib/src/components/cards/helpers.js
Expand Up @@ -19,42 +19,23 @@ const getDifferenceInMillisecondes = date => {
* @param {function} options.t - i18n function
* @param {import('cozy-client/types/types').IOCozyKonnector} options.konnector - Associated Connector
* @param {object} options.trigger - Associated trigger
* @param {object} options.running - If the connector is running
* @param {object} options.expectingTriggerLaunch - If the trigger is waiting to be launched
* @param {object} options.lastSuccessDate - The last date when the trigger was successfully executed
* @param {object} options.isKonnectorRunnable - If the konnector is runnable
* @param {object} options.isInMaintenance - If the konnector is in maintenance
* @param {object} options.isRunning - If the connector is running
* @returns {string}
*/
export const makeLabel = ({
t,
konnector,
trigger,
running,
expectingTriggerLaunch,
isInMaintenance,
isKonnectorRunnable
}) => {
export const makeLabel = ({ t, konnector, trigger, isRunning }) => {
const lastSuccessDate = getLastSuccessDate(trigger)
if (!isKonnectorRunnable) {
return t('accountForm.notClientSide', { name: konnector.name })
}
const mantenanceSuffix = isInMaintenance
? ` · ${t('konnectorBlock.inMaintenance')}`
: ''

if (running || expectingTriggerLaunch) {
return `${t('card.launchTrigger.lastSync.syncing')}${mantenanceSuffix}`
if (isRunning) {
return t('card.launchTrigger.lastSync.syncing')
}

if (lastSuccessDate) {
if (getDifferenceInMillisecondes(lastSuccessDate) < DEFAULT_TIME) {
return `${t('card.launchTrigger.lastSync.justNow')}${mantenanceSuffix}`
return t('card.launchTrigger.lastSync.justNow')
}

return `${t('card.launchTrigger.lastSync.afterSomeTimes', {
return t('card.launchTrigger.lastSync.afterSomeTimes', {
times: formatLocallyDistanceToNow(lastSuccessDate)
})}${mantenanceSuffix}`
})
}

if (isDisconnected(konnector, trigger)) {
Expand Down
66 changes: 7 additions & 59 deletions packages/cozy-harvest-lib/src/components/cards/helpers.spec.js
Expand Up @@ -11,31 +11,7 @@ describe('makeLabel', () => {
it('when running is true', () => {
const res = makeLabel({
t,
running: true,
expectingTriggerLaunch: false,
isKonnectorRunnable: true
})

expect(res).toBe('Data recovery…')
})

it('when runggin and expectingTriggerLaunch are true', () => {
const res = makeLabel({
t,
running: true,
expectingTriggerLaunch: true,
isKonnectorRunnable: true
})

expect(res).toBe('Data recovery…')
})

it('when expectingTriggerLaunch is true', () => {
const res = makeLabel({
t,
running: false,
expectingTriggerLaunch: true,
isKonnectorRunnable: true
isRunning: true
})

expect(res).toBe('Data recovery…')
Expand All @@ -44,16 +20,14 @@ describe('makeLabel', () => {
it('when lastSuccessDate is null', () => {
const res = makeLabel({
t,
running: true,
expectingTriggerLaunch: true,
isKonnectorRunnable: true
isRunning: true
})

expect(res).toBe('Data recovery…')
})
})

describe('when running and expectingTriggerLaunch are false', () => {
describe('when isRunning', () => {
beforeEach(() => {
MockDate.set('2020-12-25T12:00:00.000Z')
})
Expand All @@ -67,9 +41,7 @@ describe('makeLabel', () => {
trigger: {
current_state: { last_success: '2020-12-25T11:55:00.000Z' }
},
running: false,
expectingTriggerLaunch: false,
isKonnectorRunnable: true
isRunning: false
})

expect(res).toContain(
Expand All @@ -83,9 +55,7 @@ describe('makeLabel', () => {
trigger: {
current_state: { last_success: '2020-12-25T11:55:01.000Z' }
},
running: false,
expectingTriggerLaunch: false,
isKonnectorRunnable: true
isRunning: false
})

expect(res).toBe(`${t('card.launchTrigger.lastSync.justNow')}`)
Expand All @@ -97,9 +67,7 @@ describe('makeLabel', () => {
trigger: {
current_state: { last_success: null }
},
running: false,
expectingTriggerLaunch: false,
isKonnectorRunnable: true
isRunning: false
})

expect(res).toBe('Unknown')
Expand All @@ -109,30 +77,10 @@ describe('makeLabel', () => {
const res = makeLabel({
t,
konnector: {},
running: false,
expectingTriggerLaunch: false,
isKonnectorRunnable: true
isRunning: false
})

expect(res).toBe('Disconnected')
})
})

describe('not runnable konnector', () => {
it('should display the message when not runnable', () => {
const t = x => x

const res = makeLabel({
t,
konnector: {
name: 'foo'
},
running: false,
expectingTriggerLaunch: false,
isKonnectorRunnable: false
})

expect(res).toBe('accountForm.notClientSide')
})
})
})

0 comments on commit 0570f44

Please sign in to comment.