Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(harvest): Add AccountModalWithoutTabs component
This component allows to display the error alert or the content of the modal. Same behavior as the AccountModal component
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/cozy-harvest-lib/src/components/AccountModalWithoutTabs/AccountModalWithoutTabs.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { Outlet } from 'react-router-dom' | ||
import DialogContent from '@material-ui/core/DialogContent' | ||
|
||
import { useQuery, isQueryLoading } from 'cozy-client' | ||
import Spinner from 'cozy-ui/transpiled/react/Spinner' | ||
|
||
import { buildAccountQueryById } from '../../connections/accounts' | ||
import { withMountPointProps } from '../MountPointContext' | ||
import { getMatchingTrigger } from './helpers' | ||
import AccountModalHeader from './AccountModalHeader' | ||
import Error from './Error' | ||
|
||
const AccountModalWithoutTabs = ({ | ||
accountsAndTriggers, | ||
konnector, | ||
accountId | ||
}) => { | ||
const matchingTrigger = getMatchingTrigger(accountsAndTriggers, accountId) | ||
const matchingAccountId = matchingTrigger ? accountId : undefined | ||
|
||
const { definition, options } = buildAccountQueryById(matchingAccountId) | ||
const { data: accounts, ...accountQueryResult } = useQuery( | ||
definition, | ||
options | ||
) | ||
|
||
const isLoading = | ||
isQueryLoading(accountQueryResult) || accountQueryResult.hasMore | ||
|
||
const isError = | ||
!isLoading && (!matchingTrigger || !accounts || accounts?.length === 0) | ||
|
||
const account = accounts?.[0] | ||
|
||
return ( | ||
<> | ||
<AccountModalHeader | ||
konnector={konnector} | ||
account={account} | ||
accountsAndTriggers={accountsAndTriggers} | ||
/> | ||
{(isError || isLoading) && ( | ||
<DialogContent className="u-pb-2"> | ||
{isError && ( | ||
<Error | ||
accountId={accountId} | ||
accountsAndTriggers={accountsAndTriggers} | ||
trigger={matchingTrigger} | ||
lastError={accountQueryResult.lastError} | ||
/> | ||
)} | ||
{isLoading && ( | ||
<Spinner className="u-flex u-flex-justify-center" size="xxlarge" /> | ||
)} | ||
</DialogContent> | ||
)} | ||
{!isError && !isLoading && ( | ||
<Outlet context={{ trigger: matchingTrigger, account, konnector }} /> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
AccountModalWithoutTabs.propTypes = { | ||
konnector: PropTypes.object.isRequired, | ||
/** | ||
* @type {{ account: 'io.cozy.accounts', trigger: 'io.cozy.triggers' }[]} - An array of objects containing an account and its associated trigger | ||
*/ | ||
accountsAndTriggers: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
account: PropTypes.object.isRequired, | ||
trigger: PropTypes.object.isRequired | ||
}) | ||
).isRequired, | ||
accountId: PropTypes.string.isRequired | ||
} | ||
|
||
export default withMountPointProps(AccountModalWithoutTabs) |
10 changes: 10 additions & 0 deletions
10
packages/cozy-harvest-lib/src/components/AccountModalWithoutTabs/helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import get from 'lodash/get' | ||
|
||
export const getMatchingTrigger = (accountsAndTriggers, accountId) => { | ||
return get( | ||
accountsAndTriggers.find( | ||
accountAndTrigger => accountAndTrigger.account._id === accountId | ||
), | ||
'trigger' | ||
) | ||
} |