Skip to content

Commit

Permalink
fix: Force the update of the current konnector data
Browse files Browse the repository at this point in the history
To be sure to have updated konnector fields displayed to the user.
This is a step backward regarding time optimization but we need to be
sure to have updated konnector fields or this can lead to konnector
errors.
  • Loading branch information
doubleface authored and doubleface committed Jan 16, 2025
1 parent a00baa1 commit 23370de
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/cozy-harvest-lib/src/helpers/useKonnectorWithTriggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,35 @@ const KONNECTORS_DOCTYPE = 'io.cozy.konnectors'

export const useKonnectorWithTriggers = (slug, injectedKonnector) => {
const client = useClient()
const [isFetching, setIsFetching] = useState(true)
const [isFetchingKonnector, setIsFetchingKonnector] = useState(true)
const [isFetchingTriggers, setIsFetchingTriggers] = useState(true)
const [triggers, setTriggers] = useState({ data: [] })
const [konnector, setKonnector] = useState({})

useEffect(() => {
async function load() {
async function _getKonnector() {
// need to do getKonnector anyway to force the update of the konnector to be
// sure to have updated konnector fields
const konnector = await getKonnector(client, slug)
setKonnector(konnector)
setIsFetchingKonnector(false)
}

_getKonnector()
}, [client, slug])

useEffect(() => {
async function _getTriggers() {
if (injectedKonnector) {
setKonnector(injectedKonnector)
setTriggers(injectedKonnector.triggers)
} else {
const [konnector, triggers] = await Promise.all([
getKonnector(client, slug),
getTriggers(client, slug)
])
setKonnector(konnector)
const triggers = await getTriggers(client, slug)
setTriggers({ data: triggers })
}
setIsFetching(false)
setIsFetchingTriggers(false)
}

load()
_getTriggers()
}, [client, injectedKonnector, slug])

useEffect(() => {
Expand All @@ -52,8 +60,8 @@ export const useKonnectorWithTriggers = (slug, injectedKonnector) => {
}
return {
konnectorWithTriggers,
fetching: isFetching,
notFoundError: !isFetching && !konnector
fetching: isFetchingKonnector || isFetchingTriggers,
notFoundError: !(isFetchingKonnector || isFetchingTriggers) && !konnector
}
}

Expand Down

0 comments on commit 23370de

Please sign in to comment.