Skip to content
Snippets Groups Projects
Commit abde7f40 authored by Yoan VALLET's avatar Yoan VALLET
Browse files

feat: handle LOGIN_FAILED case

parent 0a993e5d
Branches
Tags
3 merge requests!28Merge fix from dev,!27Dev,!25Features/us150 review of konnectors
import React, { useState } from 'react' import React from 'react'
import { translate } from 'cozy-ui/react/I18n' import { translate } from 'cozy-ui/react/I18n'
import IFluidConfig from 'services/IFluidConfig' import IFluidConfig from 'services/IFluidConfig'
...@@ -6,46 +6,42 @@ import { Konnector, Trigger } from 'doctypes' ...@@ -6,46 +6,42 @@ import { Konnector, Trigger } from 'doctypes'
import KonnectorLoginForm from 'components/ContentComponents/Konnector/KonnectorLoginForm' import KonnectorLoginForm from 'components/ContentComponents/Konnector/KonnectorLoginForm'
import KonnectorOAuthForm from 'components/ContentComponents/Konnector/KonnectorOAuthForm' import KonnectorOAuthForm from 'components/ContentComponents/Konnector/KonnectorOAuthForm'
import KonnectorLoading from 'components/ContentComponents/Konnector/KonnectorLoading'
interface KonnectorFormProps { interface KonnectorFormProps {
fluidConfig: IFluidConfig fluidConfig: IFluidConfig
konnector: Konnector konnector: Konnector
handleConnexion: Function account: Account | null
trigger: Trigger | null
handleSuccessForm: Function
} }
const KonnectorForm: React.FC<KonnectorFormProps> = ({ const KonnectorForm: React.FC<KonnectorFormProps> = ({
fluidConfig, fluidConfig,
konnector, konnector,
handleConnexion, account,
trigger,
handleSuccessForm,
}: KonnectorFormProps) => { }: KonnectorFormProps) => {
const oAuth: boolean = fluidConfig.konnectorConfig.oauth const oAuth: boolean = fluidConfig.konnectorConfig.oauth
const [account, setAccount] = useState<Account | null>(null)
const [trigger, setTrigger] = useState<Trigger | null>(null)
const handleForm = (_account: Account, _trigger: Trigger) => { const handleSuccess = (_account: Account, _trigger: Trigger) => {
setAccount(_account) handleSuccessForm(_account, _trigger)
setTrigger(_trigger)
}
const handleKonnectorLoading = () => {
handleConnexion(account)
} }
return ( return (
<> <>
{account && trigger ? ( {!oAuth ? (
<KonnectorLoading <KonnectorLoginForm
fluidConfig={fluidConfig}
onSuccess={handleSuccess}
account={account}
trigger={trigger} trigger={trigger}
handleKonnectorLoading={handleKonnectorLoading}
/> />
) : !oAuth ? (
<KonnectorLoginForm fluidConfig={fluidConfig} onSuccess={handleForm} />
) : ( ) : (
<KonnectorOAuthForm <KonnectorOAuthForm
konnector={konnector} konnector={konnector}
siteLink={fluidConfig.siteLink} siteLink={fluidConfig.siteLink}
onSuccess={handleForm} onSuccess={handleSuccess}
/> />
)} )}
</> </>
......
...@@ -23,21 +23,21 @@ const loadingOptions = { ...@@ -23,21 +23,21 @@ const loadingOptions = {
}, },
} }
interface KonnectorLoadingProps { interface KonnectorLaunchProps {
trigger: Trigger trigger: Trigger
handleKonnectorLoading: Function handleKonnectorLaunch: Function
client: Client client: Client
t: Function t: Function
} }
const KonnectorLoading: React.FC<KonnectorLoadingProps> = ({ const KonnectorLaunch: React.FC<KonnectorLaunchProps> = ({
trigger, trigger,
handleKonnectorLoading, handleKonnectorLaunch,
client, client,
t, t,
}: KonnectorLoadingProps) => { }: KonnectorLaunchProps) => {
const callbackResponse = () => { const callbackResponse = () => {
handleKonnectorLoading() handleKonnectorLaunch()
} }
useEffect(() => { useEffect(() => {
...@@ -63,6 +63,7 @@ const KonnectorLoading: React.FC<KonnectorLoadingProps> = ({ ...@@ -63,6 +63,7 @@ const KonnectorLoading: React.FC<KonnectorLoadingProps> = ({
} }
}, []) }, [])
// TODO - SUCCESS SCREEN
return ( return (
<div className="kload-content"> <div className="kload-content">
<Lottie options={loadingOptions} height={50} width={50} /> <Lottie options={loadingOptions} height={50} width={50} />
...@@ -76,4 +77,4 @@ const KonnectorLoading: React.FC<KonnectorLoadingProps> = ({ ...@@ -76,4 +77,4 @@ const KonnectorLoading: React.FC<KonnectorLoadingProps> = ({
) )
} }
export default translate()(withClient(KonnectorLoading)) export default translate()(withClient(KonnectorLaunch))
...@@ -8,10 +8,13 @@ import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton' ...@@ -8,10 +8,13 @@ import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton'
import StyledButton from 'components/CommonKit/Button/StyledButton' import StyledButton from 'components/CommonKit/Button/StyledButton'
import TrailingIcon from 'assets/icons/ico/trailing-icon.svg' import TrailingIcon from 'assets/icons/ico/trailing-icon.svg'
import { ConnectionService } from 'services/connectionService' import { ConnectionService } from 'services/connectionService'
import { Account, Trigger } from 'doctypes'
interface KonnectorLoginFormProps { interface KonnectorLoginFormProps {
fluidConfig: IFluidConfig fluidConfig: IFluidConfig
onSuccess: Function onSuccess: Function
account: Account
trigger: Trigger
client: Client client: Client
t: Function t: Function
} }
...@@ -19,6 +22,8 @@ interface KonnectorLoginFormProps { ...@@ -19,6 +22,8 @@ interface KonnectorLoginFormProps {
const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({
fluidConfig, fluidConfig,
onSuccess, onSuccess,
account,
trigger,
client, client,
t, t,
}: KonnectorLoginFormProps) => { }: KonnectorLoginFormProps) => {
...@@ -42,6 +47,27 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({ ...@@ -42,6 +47,27 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({
} }
} }
const connect = async () => {
const connectionService = new ConnectionService(
client,
fluidConfig.konnectorConfig.slug,
login,
password
)
const { account, trigger } = await connectionService.connectNewUser()
if (!trigger) {
setError(t('KONNECTORCONFIG.ERROR_ACCOUNT_CREATION'))
setLoading(false)
return null
}
onSuccess(account, trigger)
}
const update = async () => {
// TODO - update account
onSuccess(account, trigger)
}
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
try { try {
...@@ -52,24 +78,17 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({ ...@@ -52,24 +78,17 @@ const KonnectorLoginForm: React.FC<KonnectorLoginFormProps> = ({
setLoading(false) setLoading(false)
return null return null
} }
const connectionService = new ConnectionService( if (!account) {
client, await connect()
fluidConfig.konnectorConfig.slug, } else {
login, await update()
password
)
const { account, trigger } = await connectionService.connectNewUser()
if (!trigger) {
setError(t('KONNECTORCONFIG.ERROR_ACCOUNT_CREATION'))
setLoading(false)
return null
} }
onSuccess(account, trigger)
} catch (error) { } catch (error) {
setLoading(false) setLoading(false)
} }
} }
// TODO - if received account from props = display error login failed
return ( return (
<form <form
className="form" className="form"
......
...@@ -19,9 +19,11 @@ import IFluidConfig from 'services/IFluidConfig' ...@@ -19,9 +19,11 @@ import IFluidConfig from 'services/IFluidConfig'
import KonnectorNotFound from 'components/ContentComponents/Konnector/KonnectorNotFound' import KonnectorNotFound from 'components/ContentComponents/Konnector/KonnectorNotFound'
import KonnectorForm from 'components/ContentComponents/Konnector/KonnectorForm' import KonnectorForm from 'components/ContentComponents/Konnector/KonnectorForm'
import KonnectorResult from 'components/ContentComponents/Konnector/KonnectorResult' import KonnectorResult from 'components/ContentComponents/Konnector/KonnectorResult'
import KonnectorLaunch from 'components/ContentComponents/Konnector/KonnectorLaunch'
import { Konnector } from 'doctypes' import { Konnector, Trigger, TriggerState } from 'doctypes'
import { JobState } from 'services/jobsService' import { JobState } from 'services/jobsService'
import { TriggerService } from 'services/triggersService'
interface KonnectorViewerCardProps { interface KonnectorViewerCardProps {
fluidConfig: IFluidConfig fluidConfig: IFluidConfig
...@@ -39,6 +41,9 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -39,6 +41,9 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
t, t,
}: KonnectorViewerCardProps) => { }: KonnectorViewerCardProps) => {
const [account, setAccount] = useState<Account | null>(null) const [account, setAccount] = useState<Account | null>(null)
const [trigger, setTrigger] = useState<Trigger | null>(null)
const [triggerState, setTriggerState] = useState<TriggerState | null>(null)
const [shouldLaunch, setLaunch] = useState<boolean>(false)
const [setActive, setActiveState] = useState('') const [setActive, setActiveState] = useState('')
const [setHeight, setHeightState] = useState('0px') const [setHeight, setHeightState] = useState('0px')
...@@ -51,6 +56,10 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -51,6 +56,10 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
const iconType = getPicto(fluid) const iconType = getPicto(fluid)
const iconAddType = isParam ? getParamPicto(fluid) : getAddPicto(fluid) const iconAddType = isParam ? getParamPicto(fluid) : getAddPicto(fluid)
const loginFailed: boolean =
triggerState != null &&
triggerState.last_error != undefined &&
triggerState.last_error === 'LOGIN_FAILED'
const toggleAccordion = () => { const toggleAccordion = () => {
setActiveState(setActive === '' ? 'active' : '') setActiveState(setActive === '' ? 'active' : '')
...@@ -72,8 +81,24 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -72,8 +81,24 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
) )
} }
const handleConnexion = (_account: Account) => { const updateState = async (trigger: Trigger) => {
const triggerState = await TriggerService.fetchTriggerState(client, trigger)
if (triggerState) {
setTriggerState(triggerState)
}
}
const handleSuccessForm = (_account: Account, _trigger: Trigger) => {
setAccount(_account) setAccount(_account)
setTrigger(_trigger)
setLaunch(true)
}
const handleKonnectorLaunch = () => {
if (trigger) {
updateState(trigger)
}
setLaunch(false)
} }
const handleJobState = (_jobState: JobState) => { const handleJobState = (_jobState: JobState) => {
...@@ -90,6 +115,14 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -90,6 +115,14 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
) )
if (subscribed && _account) { if (subscribed && _account) {
setAccount(_account) setAccount(_account)
const _trigger = await TriggerService.fetchTriggerFromAccount(
client,
_account
)
if (subscribed && _trigger) {
setTrigger(_trigger)
await updateState(_trigger)
}
} }
} }
} }
...@@ -108,18 +141,18 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -108,18 +141,18 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
onClick={toggleAccordion} onClick={toggleAccordion}
> >
<div className="accordion-icon"> <div className="accordion-icon">
{!account ? ( {account && !loginFailed ? (
<StyledIcon className="icon" icon={iconAddType} size={49} />
) : (
<StyledIcon className="icon" icon={iconType} size={49} /> <StyledIcon className="icon" icon={iconType} size={49} />
) : (
<StyledIcon className="icon" icon={iconAddType} size={49} />
)} )}
</div> </div>
<div className="state-picto">{getKonnectorStateMarkup()}</div> <div className="state-picto">{getKonnectorStateMarkup()}</div>
<div className="accordion-info"> <div className="accordion-info">
<div className="accordion-title text-16-normal"> <div className="accordion-title text-16-normal">
{!account {account && !loginFailed
? t('KONNECTORCONFIG.LABEL_CONNECTTO_' + FluidType[fluid]) ? t('FLUID.' + FluidType[fluid] + '.LABEL')
: t('FLUID.' + FluidType[fluid] + '.LABEL')} : t('KONNECTORCONFIG.LABEL_CONNECTTO_' + FluidType[fluid])}
</div> </div>
</div> </div>
<StyledIconButton icon={setActive ? chevronUp : chevronDown} /> <StyledIconButton icon={setActive ? chevronUp : chevronDown} />
...@@ -134,17 +167,22 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -134,17 +167,22 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
<KonnectorNotFound <KonnectorNotFound
konnectorSlug={fluidConfig.konnectorConfig.slug} konnectorSlug={fluidConfig.konnectorConfig.slug}
/> />
) : !account ? ( ) : shouldLaunch && trigger ? (
<KonnectorForm <KonnectorLaunch
fluidConfig={fluidConfig} trigger={trigger}
konnector={konnector} handleKonnectorLaunch={handleKonnectorLaunch}
handleConnexion={handleConnexion}
/> />
) : ( ) : account && !loginFailed ? (
<KonnectorResult <KonnectorResult
account={account} account={account}
handleJobState={handleJobState} handleJobState={handleJobState}
/> />
) : (
<KonnectorForm
fluidConfig={fluidConfig}
konnector={konnector}
handleSuccessForm={handleSuccessForm}
/>
)} )}
</div> </div>
</div> </div>
......
...@@ -13,6 +13,7 @@ import StyledBlackSpinner from 'components/CommonKit/Spinner/StyledBlackSpinner' ...@@ -13,6 +13,7 @@ import StyledBlackSpinner from 'components/CommonKit/Spinner/StyledBlackSpinner'
interface OAuthFormProps { interface OAuthFormProps {
konnector: Konnector konnector: Konnector
onSuccess: Function onSuccess: Function
loginFailed: boolean
client: Client client: Client
t: Function t: Function
} }
......
...@@ -11,6 +11,18 @@ export type Trigger = { ...@@ -11,6 +11,18 @@ export type Trigger = {
} }
} }
export type TriggerState = {
trigger_id: string
status: string
last_error?: string
last_executed_job_id: string
last_execution: string
last_failed_job_id: string
last_failure: string
last_manual_execution: string
last_manual_job_id: string
}
export function isTrigger(trigger: any): trigger is Trigger { export function isTrigger(trigger: any): trigger is Trigger {
return ( return (
trigger && trigger &&
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment