Skip to content
Snippets Groups Projects
Commit b770fef9 authored by Yoan Vallet's avatar Yoan Vallet
Browse files

WIP: handle OAuth konnector

parent 20d9a7e8
No related branches found
No related tags found
2 merge requests!15Merge Dev to Master,!10Features/handle o auth konnector
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
"name": "enedis", "name": "enedis",
"type": "ELECTRICITY", "type": "ELECTRICITY",
"oauth": true, "oauth": true,
"slug": "enedis-konnector", "slug": "enedis",
"id": "io.cozy.konnectors/enedis-konnector" "id": "io.cozy.konnectors/enedis"
} }
}, },
{ {
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
"name": "eau du grand lyon", "name": "eau du grand lyon",
"type": "WATER", "type": "WATER",
"oauth": false, "oauth": false,
"slug": "egl-api-connector", "slug": "egl",
"id": "io.cozy.konnectors/egl-api-connector" "id": "io.cozy.konnectors/egl"
} }
}, },
{ {
...@@ -35,8 +35,8 @@ ...@@ -35,8 +35,8 @@
"name": "grdf", "name": "grdf",
"type": "GAS", "type": "GAS",
"oauth": false, "oauth": false,
"slug": "grdf-scraping-connector", "slug": "grdf",
"id": "io.cozy.konnectors/grdf-scraping-connector" "id": "io.cozy.konnectors/grdf"
} }
} }
] ]
......
import React from 'react'
import { translate } from 'cozy-ui/react/I18n'
import IFluidConfig from 'services/IFluidConfig'
import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton'
import StyledButton from 'components/CommonKit/Button/StyledButton'
import TrailingIcon from 'assets/icons/ico/trailing-icon.svg'
interface KonnectorFormProps {
fluidConfig: IFluidConfig
login: string
setLogin: Function
password: string
setPassword: Function
loading: boolean
error: string
handleSubmit: Function
t: Function
}
const KonnectorForm: React.FC<KonnectorFormProps> = ({
fluidConfig,
login,
setLogin,
password,
setPassword,
loading,
error,
handleSubmit,
t,
}: KonnectorFormProps) => {
const konnectorName: string = fluidConfig.konnectorConfig.name
const konnectorType: string = fluidConfig.konnectorConfig.type
function revealPassword(idInput: string) {
const input = document.getElementById(idInput)
if (input) {
if (input.getAttribute('type') === 'password') {
input.setAttribute('type', 'text')
} else {
input.setAttribute('type', 'password')
}
}
}
return (
<form
className="form"
onSubmit={(e: React.FormEvent<HTMLFormElement>) => handleSubmit(e)}
>
{t('KONNECTORCONFIG.LABEL_FILLIN')} {konnectorName}
<div className="form-group">
<div className="form-message">{error}</div>
<input
id={'idFieldLogin' + konnectorType}
type="text"
className="form-control form-input"
aria-describedby="emailHelp"
placeholder="Adresse e-mail"
name="login"
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setLogin(e.target.value)
}
value={login}
></input>
</div>
<div className="form-group">
<div className="form-message">{error}</div>
<input
id={'idFieldPassword' + konnectorType}
type="password"
className="form-control form-input"
aria-describedby="PasswordHelp"
placeholder="Mot de passe"
name="password"
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPassword(e.target.value)
}
value={password}
/>
<span>
<StyledIconButton
icon={TrailingIcon}
className="form-trailing-icon"
size={22}
onClick={() => revealPassword('idFieldPassword' + konnectorType)}
/>
</span>
</div>
<StyledButton type="submit" color="primary" disabled={loading}>
{t('KONNECTORCONFIG.BTN_CONNECTION')}
</StyledButton>
<StyledButton type="button" color="secondary" disabled={loading}>
{t('KONNECTORCONFIG.BTN_NOACCOUNT')}
</StyledButton>
</form>
)
}
export default translate()(KonnectorForm)
import React from 'react'
const KonnectorLoading = () => {
return (
<div>
Vos données sont en cours de rappatriement. Ce traitement peut prendre
plusieurs minutes, merci de patienter.
</div>
)
}
export default KonnectorLoading
import React from 'react'
import { translate } from 'cozy-ui/react/I18n'
import OAuthForm from 'components/ContentComponents/OAuth/OAuthForm'
import { Konnector } from 'doctypes'
interface KonnectorOAuthFormProps {
konnector: Konnector
onSuccess: Function
loading: boolean
t: Function
}
const KonnectorOAuthForm: React.FC<KonnectorOAuthFormProps> = ({
konnector,
onSuccess,
loading,
t,
}: KonnectorOAuthFormProps) => {
return (
<div>
{t('oauth.connect.' + konnector.slug)}
<OAuthForm konnector={konnector} onSuccess={onSuccess} />
</div>
)
}
export default translate()(KonnectorOAuthForm)
import React from 'react'
import { translate } from 'cozy-ui/react/I18n'
import StyledButton from 'components/CommonKit/Button/StyledButton'
import Spinner from 'components/CommonKit/Spinner/Spinner'
interface KonnectorResultProps {
date: string
updating: boolean
errored: boolean
updateKonnector: (event: any) => void
deleteAccount: (event: any) => void
t: Function
}
const KonnectorResult: React.FC<KonnectorResultProps> = ({
date,
updating,
errored,
updateKonnector,
deleteAccount,
t,
}) => {
return (
<div>
<div className="accordion-update">
<div
className={errored ? 'accordion-caption-red' : 'accordion-caption'}
>
{t('KONNECTORCONFIG.LABEL_UPDATEDAT')}
</div>
<div>{date}</div>
</div>
<div className="inline-buttons">
<StyledButton type="button" color="primary" onClick={updateKonnector}>
{updating ? (
<Spinner size="2em" />
) : (
<div>{t('KONNECTORCONFIG.BTN_UPDATE')}</div>
)}
</StyledButton>
<StyledButton
type="button"
color="secondary"
onClick={deleteAccount}
disabled={updating}
>
{t('KONNECTORCONFIG.BTN_DELETE')}
</StyledButton>
</div>
</div>
)
}
export default translate()(KonnectorResult)
import React, { useState, useEffect } from 'react' import React, { useState, useEffect } from 'react'
import { withClient, Client } from 'cozy-client' import { withClient, Client } from 'cozy-client'
import { Konnector } from 'doctypes'
import KonnectorService from 'services/konnectorService'
import KonnectorStatusService from 'services/konnectorStatusService' import KonnectorStatusService from 'services/konnectorStatusService'
import KonnectorViewerCard from 'components/ContentComponents/KonnectorViewer/KonnectorViewerCard' import KonnectorViewerCard from 'components/ContentComponents/KonnectorViewer/KonnectorViewerCard'
import IFluidConfig from 'services/IFluidConfig' import IFluidConfig from 'services/IFluidConfig'
...@@ -15,55 +19,61 @@ const KonnectorViewer: React.FC<KonnectorViewerProps> = ({ ...@@ -15,55 +19,61 @@ const KonnectorViewer: React.FC<KonnectorViewerProps> = ({
client, client,
isParam = false, isParam = false,
}: KonnectorViewerProps) => { }: KonnectorViewerProps) => {
const [Konnectors, setKonnectors] = useState(null) const [konnector, setKonnector] = useState<Konnector | null>(null)
const [Triggers, setTriggers] = useState(null) const [lastTrigger, setLastTrigger] = useState<any>(null)
const setAllKonnectors = async () => { useEffect(() => {
let subscribed = true
const konnectorService = new KonnectorService(
client,
fluidConfig.konnectorConfig.slug
)
const kss = new KonnectorStatusService(client) const kss = new KonnectorStatusService(client)
const allKonnectors = await kss.getAllKonnectors()
setKonnectors(allKonnectors)
//console.log('All Konnectors',allKonnectors);
}
const setAllTriggers = async () => { async function getData() {
const kss = new KonnectorStatusService(client) const _konnector: Konnector = await konnectorService.fetchKonnector()
const allTriggers = await kss.getAllTriggers() if (!_konnector || !_konnector.slug) {
setTriggers(allTriggers) throw new Error(
//console.log('All Triggers',allTriggers) `Could not find konnector for ${fluidConfig.konnectorConfig.slug}`
} )
}
if (subscribed && _konnector) {
setKonnector(_konnector)
}
const fetchKonnectorFromTrigger = trigger => { const allTriggers = await kss.getAllTriggers()
let konnectorName = '' if (allTriggers) {
try { const lastTrigger = allTriggers
konnectorName = trigger.message.konnector .filter(
} catch (error) { trigger =>
return konnectorName trigger.worker === 'konnector' &&
trigger.message.konnector === fluidConfig.konnectorConfig.slug
)
.sort((a, b) => (new Date(a) > new Date(b) ? 1 : -1))
.shift()
if (subscribed && lastTrigger) {
setLastTrigger(lastTrigger)
}
}
} }
return konnectorName
}
const getLastTrigger = konnector => { getData()
if (!Triggers) return null return () => {
const lastTrigger = Triggers.filter( subscribed = false
trigger => }
fetchKonnectorFromTrigger(trigger) === konnector.id.split('/').pop()
)
.sort((a, b) => (new Date(a) > new Date(b) ? 1 : -1))
.shift()
return lastTrigger
}
useEffect(() => {
setAllKonnectors()
setAllTriggers()
}, []) }, [])
return ( return (
<KonnectorViewerCard <>
fluidConfig={fluidConfig} {!konnector ? null : (
trigger={getLastTrigger(fluidConfig.konnectorConfig)} <KonnectorViewerCard
isParam={isParam} fluidConfig={fluidConfig}
/> konnector={konnector}
trigger={lastTrigger}
isParam={isParam}
/>
)}
</>
) )
} }
......
...@@ -14,43 +14,36 @@ import { translate } from 'cozy-ui/react/I18n' ...@@ -14,43 +14,36 @@ import { translate } from 'cozy-ui/react/I18n'
import { getPicto, getAddPicto, getFuildType, getParamPicto } from 'utils/utils' import { getPicto, getAddPicto, getFuildType, getParamPicto } from 'utils/utils'
import chevronDown from 'assets/icons/ico/chevron-down.svg' import chevronDown from 'assets/icons/ico/chevron-down.svg'
import chevronUp from 'assets/icons/ico/chevron-up.svg' import chevronUp from 'assets/icons/ico/chevron-up.svg'
import TrailingIcon from 'assets/icons/ico/trailing-icon.svg'
import StyledIcon from 'components/CommonKit/Icon/StyledIcon' import StyledIcon from 'components/CommonKit/Icon/StyledIcon'
import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton' import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton'
import StyledButton from 'components/CommonKit/Button/StyledButton'
import failurePicto from '../KonnectorViewer/picto-failure.png' import failurePicto from '../KonnectorViewer/picto-failure.png'
import successPicto from '../KonnectorViewer/picto-success.png' import successPicto from '../KonnectorViewer/picto-success.png'
import pendingPicto from '../KonnectorViewer/picto-pending.png' import pendingPicto from '../KonnectorViewer/picto-pending.png'
import KonnectorService from 'services/konnectorService' import KonnectorService from 'services/konnectorService'
import KonnectorStatusService from 'services/konnectorStatusService' import KonnectorStatusService from 'services/konnectorStatusService'
import Spinner from 'components/CommonKit/Spinner/Spinner'
import IFluidConfig from 'services/IFluidConfig' import IFluidConfig from 'services/IFluidConfig'
import OAuthKonnector from '../OAuthKonnector/OAuthKonnector'
import KonnectorForm from 'components/ContentComponents/Konnector/KonnectorForm'
import KonnectorOAuthForm from 'components/ContentComponents/Konnector/KonnectorOAuthForm'
import KonnectorLoading from 'components/ContentComponents/Konnector/KonnectorLoading'
import KonnectorResult from 'components/ContentComponents/Konnector/KonnectorResult'
import { Account, Konnector, Trigger } from 'doctypes' import { Account, Konnector, Trigger } from 'doctypes'
interface KonnectorViewerCardProps { interface KonnectorViewerCardProps {
fluidConfig: IFluidConfig fluidConfig: IFluidConfig
konnector: Konnector
trigger: any | null trigger: any | null
client: Client client: Client
isParam: boolean isParam: boolean
t: Function t: Function
} }
function revealPassword(idInput: string) {
const input = document.getElementById(idInput)
if (input) {
if (input.getAttribute('type') === 'password') {
input.setAttribute('type', 'text')
} else {
input.setAttribute('type', 'password')
}
}
}
const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
fluidConfig, fluidConfig,
konnector,
trigger, trigger,
client, client,
isParam, isParam,
...@@ -59,6 +52,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -59,6 +52,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
const [login, setLogin] = useState<string>('') const [login, setLogin] = useState<string>('')
const [password, setPassword] = useState<string>('') const [password, setPassword] = useState<string>('')
const [loading, setLoading] = useState<boolean>(false) const [loading, setLoading] = useState<boolean>(false)
const [updating, setUpdating] = useState<boolean>(false)
const [konnectorAccountId, setAccountId] = useState<string>('') const [konnectorAccountId, setAccountId] = useState<string>('')
const [isKonnectorAcc, setAccount] = useState<boolean>(false) const [isKonnectorAcc, setAccount] = useState<boolean>(false)
const [setActive, setActiveState] = useState('') const [setActive, setActiveState] = useState('')
...@@ -104,7 +98,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -104,7 +98,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
setJobState(JobState.Running) setJobState(JobState.Running)
const connectionService = new ConnectionService( const connectionService = new ConnectionService(
client, client,
fluidConfig.konnectorConfig.id, fluidConfig.konnectorConfig.slug,
login, login,
password password
) )
...@@ -126,7 +120,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -126,7 +120,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
setAccountId(data.message.account) setAccountId(data.message.account)
setError('Connected') setError('Connected')
setAccount(true) setAccount(true)
await context.refreshFluidTypes() // await context.refreshFluidTypes()
return data return data
} catch (error) { } catch (error) {
setLoading(false) setLoading(false)
...@@ -154,7 +148,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -154,7 +148,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
toggleAccordion() toggleAccordion()
trigger = null trigger = null
setAccount(false) setAccount(false)
context.refreshFluidTypes() await context.refreshFluidTypes()
} catch (error) { } catch (error) {
setLoading(false) setLoading(false)
setError(error.message) setError(error.message)
...@@ -164,17 +158,17 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -164,17 +158,17 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
const updateKonnector = async () => { const updateKonnector = async () => {
setError('') setError('')
setJobState('') setJobState('')
setLoading(true) setUpdating(true)
try { try {
const account = await AccountService.getAccount( const account = await AccountService.getAccount(
client, client,
konnectorAccountId konnectorAccountId
) )
const konnectorService = new KonnectorService( // const konnectorService = new KonnectorService(
client, // client,
fluidConfig.konnectorConfig.slug // fluidConfig.konnectorConfig.slug
) // )
const konnector = await konnectorService.fetchKonnector() // const konnector = await konnectorService.fetchKonnector()
const triggersServices = new TriggerService(client, account, konnector) const triggersServices = new TriggerService(client, account, konnector)
if (updateTrigger !== null) { if (updateTrigger !== null) {
triggersServices.setTrigger(updateTrigger) triggersServices.setTrigger(updateTrigger)
...@@ -186,27 +180,27 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -186,27 +180,27 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
throw new Error(`Error during trigger launching`) throw new Error(`Error during trigger launching`)
} }
setlaunchedJob(job) setlaunchedJob(job)
setLoading(false) // setUpdating(false)
setUpdateDate(new Date(job.queued_at).toDateString()) setUpdateDate(new Date(job.queued_at).toDateString())
} catch (error) { } catch (error) {
setLoading(false) setUpdating(false)
setError(error.message) setError(error.message)
} }
} }
const initOauthAccount = async oauthAccountId => { const initOauthAccount = async (oauthAccountId: string) => {
console.log('initOauthAccount', oauthAccountId) console.log('initOauthAccount', oauthAccountId)
setError('') setError('')
setJobState('') setJobState('')
setLoading(true) setLoading(true)
try { try {
const account = await AccountService.getAccount(client, oauthAccountId) const account = await AccountService.getAccount(client, oauthAccountId)
const konnectorService = new KonnectorService( // const konnectorService = new KonnectorService(
client, // client,
fluidConfig.konnectorConfig.slug // fluidConfig.konnectorConfig.slug
) // )
console.log(account) // console.log(account)
const konnector: Konnector = await konnectorService.fetchKonnector() // const konnector: Konnector = await konnectorService.fetchKonnector()
console.log(konnector) console.log(konnector)
const triggersServices = new TriggerService(client, account, konnector) const triggersServices = new TriggerService(client, account, konnector)
const trigger: Trigger = await triggersServices.createTrigger() const trigger: Trigger = await triggersServices.createTrigger()
...@@ -226,7 +220,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -226,7 +220,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
setAccountId(job.message.account) setAccountId(job.message.account)
setError('Connected') setError('Connected')
setAccount(true) setAccount(true)
await context.refreshFluidTypes() // await context.refreshFluidTypes()
} catch (error) { } catch (error) {
setLoading(false) setLoading(false)
setError(error.message) setError(error.message)
...@@ -260,7 +254,14 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -260,7 +254,14 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
} }
const jobStateCallBack = async (state: string) => { const jobStateCallBack = async (state: string) => {
console.log('JobCallBack', state)
setJobState(state) setJobState(state)
if (state !== JobState.Running) {
setLoading(false)
setUpdating(false)
setJobState(state)
context.refreshFluidTypes()
}
} }
useEffect(() => { useEffect(() => {
...@@ -271,13 +272,12 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -271,13 +272,12 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
let runningJob = null let runningJob = null
if (trigger) { if (trigger) {
let triggerState = '' let triggerState = null
triggerState = TriggerService.fetchStateFromTrigger(trigger) triggerState = TriggerService.fetchStateFromTrigger(trigger)
const konnectorAcc = TriggerService.fetchKonnectorAccountFromTrigger( const konnectorAcc = TriggerService.fetchKonnectorAccountFromTrigger(
trigger trigger
) )
const konnectorId = fluidConfig.konnectorConfig.id.split('/') if (subscribed && konnectorAcc.konnector === konnector.slug) {
if (subscribed && konnectorAcc.konnector === konnectorId[1]) {
setAccountId(konnectorAcc.account) setAccountId(konnectorAcc.account)
setAccount(true) setAccount(true)
} }
...@@ -286,7 +286,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -286,7 +286,7 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
runningJob = jobService.fetchJob(triggerState.last_executed_job_id) runningJob = jobService.fetchJob(triggerState.last_executed_job_id)
} }
if (launchedJob) runningJob = launchedJob if (launchedJob) runningJob = launchedJob
if (runningJob) jobService.watch(runningJob, setJobStateAfterTimer) if (runningJob) jobService.watch(runningJob, jobStateCallBack)
} }
} }
getTriggerData() getTriggerData()
...@@ -328,119 +328,38 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({ ...@@ -328,119 +328,38 @@ const KonnectorViewerCard: React.FC<KonnectorViewerCardProps> = ({
className={`accordion-content ${setActive}`} className={`accordion-content ${setActive}`}
> >
{!isKonnectorAcc ? ( {!isKonnectorAcc ? (
<div> !fluidConfig.konnectorConfig.oauth ? (
{!fluidConfig.konnectorConfig.oauth ? ( <KonnectorForm
<form fluidConfig={fluidConfig}
className="form" login={login}
onSubmit={(e: React.FormEvent<HTMLFormElement>) => setLogin={setLogin}
handleSubmit(e) password={password}
} setPassword={setPassword}
> loading={loading}
{t('KONNECTORCONFIG.LABEL_FILLIN')}{' '} error={error}
{fluidConfig.konnectorConfig.name} handleSubmit={handleSubmit}
<div className="form-group"> />
<div className="form-message">{error}</div> ) : (
<input <KonnectorOAuthForm
id={'idFieldLogin' + type} konnector={konnector}
type="text" onSuccess={initOauthAccount}
className="form-control form-input" loading={loading}
aria-describedby="emailHelp" />
placeholder="Adresse e-mail" )
name="login" ) : loading ? (
onChange={(e: React.ChangeEvent<HTMLInputElement>) => <KonnectorLoading />
setLogin(e.target.value)
}
value={login}
></input>
</div>
<div className="form-group">
<div className="form-message">{error}</div>
<input
id={'idFieldPassword' + type}
type="password"
className="form-control form-input"
aria-describedby="PasswordHelp"
placeholder="Mot de passe"
name="password"
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPassword(e.target.value)
}
value={password}
/>
<span>
<StyledIconButton
icon={TrailingIcon}
className="form-trailing-icon"
size={22}
onClick={() =>
revealPassword('idFieldPassword' + type)
}
/>
</span>
</div>
<StyledButton
type="submit"
color="primary"
disabled={loading}
>
{t('KONNECTORCONFIG.BTN_CONNECTION')}
</StyledButton>
<StyledButton
type="button"
color="secondary"
disabled={loading}
>
{t('KONNECTORCONFIG.BTN_NOACCOUNT')}
</StyledButton>
</form>
) : (
<div>
<OAuthKonnector setKonnectorAccount={initOauthAccount} />
</div>
)}
</div>
) : ( ) : (
<div> <KonnectorResult
<div> date={
<div className="accordion-update"> trigger
<div ? TriggerService.fetchDateFromTrigger(trigger)
className={ : updateDate
JobState.Errored }
? 'accordion-caption-red' updating={updating}
: 'accordion-caption' errored={jobState === JobState.Errored}
} updateKonnector={updateKonnector}
> deleteAccount={deleteAccount}
{t('KONNECTORCONFIG.LABEL_UPDATEDAT')} />
</div>
{trigger ? (
<div>{TriggerService.fetchDateFromTrigger(trigger)}</div>
) : (
<div>{updateDate}</div>
)}
</div>
<div className="inline-buttons">
<StyledButton
type="button"
color="primary"
onClick={updateKonnector}
>
{loading ? (
<Spinner size="2em" />
) : (
<div>{t('KONNECTORCONFIG.BTN_UPDATE')}</div>
)}
</StyledButton>
<StyledButton
type="button"
color="secondary"
onClick={deleteAccount}
disabled={loading}
>
{t('KONNECTORCONFIG.BTN_DELETE')}
</StyledButton>
</div>
</div>
</div>
)} )}
</div> </div>
</div> </div>
......
...@@ -3,34 +3,28 @@ import { Client, withClient } from 'cozy-client' ...@@ -3,34 +3,28 @@ import { Client, withClient } from 'cozy-client'
import { translate } from 'cozy-ui/react/I18n' import { translate } from 'cozy-ui/react/I18n'
import { Konnector } from 'doctypes' import { Konnector } from 'doctypes'
import KonnectorService from 'services/konnectorService' import KonnectorService from 'services/konnectorService'
import { OAuthForm } from 'cozy-harvest-lib/dist/components/OAuthForm'
import { OAuthWindow } from 'cozy-harvest-lib/dist/components/OAuthWindow' import { OAuthWindow } from 'cozy-harvest-lib/dist/components/OAuthWindow'
import triggersMutations from 'cozy-harvest-lib/dist/connections/triggers'
import StyledButton from 'components/CommonKit/Button/StyledButton' import StyledButton from 'components/CommonKit/Button/StyledButton'
interface OAuthKonnectorProps { interface OAuthFormProps {
// account: any konnector: Konnector
// konnector: any onSuccess: Function
setKonnectorAccount: any
client: Client client: Client
t: Function t: Function
} }
const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ const OAuthForm: React.FC<OAuthFormProps> = ({
konnector,
onSuccess,
client, client,
setKonnectorAccount,
t, t,
}: OAuthKonnectorProps) => { }: OAuthFormProps) => {
const konnectorId = 'enedis-konnector'
const IDLE = 'idle' const IDLE = 'idle'
const WAITING = 'waiting' const WAITING = 'waiting'
const [konnector, setKonnector] = useState<any>(null)
const [initialValues, setInitialValues] = useState<any>(null)
const [status, setStatus] = useState<string>(IDLE) const [status, setStatus] = useState<string>(IDLE)
function endOAuth() { function endOAuth() {
...@@ -39,12 +33,9 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ ...@@ -39,12 +33,9 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({
function startOAuth() { function startOAuth() {
setStatus(WAITING) setStatus(WAITING)
} }
function handleAccountId(accountId) { function handleAccountId(accountId: string) {
// const { onSuccess } = this.props
console.log('handleaccountid', accountId)
setKonnectorAccount(accountId)
endOAuth() endOAuth()
// if (typeof onSuccess === 'function') onSuccess(accountId) onSuccess(accountId)
} }
function handleSubmit() { function handleSubmit() {
startOAuth() startOAuth()
...@@ -53,25 +44,6 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ ...@@ -53,25 +44,6 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({
endOAuth() endOAuth()
} }
useEffect(() => {
let subscribed = true
const konnectorService = new KonnectorService(client, konnectorId)
async function getKonnector() {
const _konnector: Konnector = await konnectorService.fetchKonnector()
if (!_konnector || !_konnector.slug) {
throw new Error(`Could not find konnector for ${konnectorId}`)
}
if (subscribed && _konnector) {
setKonnector(_konnector)
}
}
getKonnector()
return () => {
subscribed = false
}
}, [])
const isWaiting = status === WAITING const isWaiting = status === WAITING
return !konnector ? null : ( return !konnector ? null : (
<> <>
...@@ -96,4 +68,4 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({ ...@@ -96,4 +68,4 @@ const OAuthKonnector: React.FC<OAuthKonnectorProps> = ({
) )
} }
export default translate()(withClient(OAuthKonnector)) export default translate()(withClient(OAuthForm))
...@@ -172,7 +172,8 @@ ...@@ -172,7 +172,8 @@
}, },
"oauth": { "oauth": {
"connect": { "connect": {
"label": "Connect" "enedis": "En cliquant sur ce bouton, vous allez accéder à votre compte personnel Enedis où vous pourrez donner votre accord pour qu’Enedis nous transmette vos données.",
"label": "Connexion"
}, },
"window": { "window": {
"title": "OAuth" "title": "OAuth"
......
...@@ -105,7 +105,7 @@ export class AccountService { ...@@ -105,7 +105,7 @@ export class AccountService {
.find('io.cozy.accounts') .find('io.cozy.accounts')
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type }) .where({ account_type: type })
.sortBy([{ 'cozyMetadata.updatedAt': 'desc' }]) // .sortBy([{ 'cozyMetadata.updatedAt': 'desc' }])
.limitBy(1) .limitBy(1)
return await client.query(query) return await client.query(query)
} catch (error) { } catch (error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment