2022-04-22 09:49:26 -04:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2023-03-20 07:45:44 -04:00
|
|
|
import MakePrimary from './actions/make-primary/make-primary'
|
2022-04-22 09:49:26 -04:00
|
|
|
import Remove from './actions/remove'
|
|
|
|
import useAsync from '../../../../shared/hooks/use-async'
|
|
|
|
import { useUserEmailsContext } from '../../context/user-email-context'
|
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
|
|
|
|
|
|
|
type ActionsProps = {
|
|
|
|
userEmailData: UserEmailData
|
|
|
|
}
|
|
|
|
|
|
|
|
function Actions({ userEmailData }: ActionsProps) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { setLoading: setUserEmailsContextLoading } = useUserEmailsContext()
|
|
|
|
const makePrimaryAsync = useAsync()
|
|
|
|
const deleteEmailAsync = useAsync()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setUserEmailsContextLoading(
|
|
|
|
makePrimaryAsync.isLoading || deleteEmailAsync.isLoading
|
|
|
|
)
|
|
|
|
}, [
|
|
|
|
setUserEmailsContextLoading,
|
|
|
|
makePrimaryAsync.isLoading,
|
|
|
|
deleteEmailAsync.isLoading,
|
|
|
|
])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (makePrimaryAsync.isLoading && !deleteEmailAsync.isIdle) {
|
|
|
|
deleteEmailAsync.reset()
|
|
|
|
}
|
|
|
|
}, [makePrimaryAsync.isLoading, deleteEmailAsync])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (deleteEmailAsync.isLoading && !makePrimaryAsync.isIdle) {
|
|
|
|
makePrimaryAsync.reset()
|
|
|
|
}
|
|
|
|
}, [deleteEmailAsync.isLoading, makePrimaryAsync])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<MakePrimary
|
|
|
|
userEmailData={userEmailData}
|
|
|
|
makePrimaryAsync={makePrimaryAsync}
|
|
|
|
/>{' '}
|
|
|
|
<Remove
|
|
|
|
userEmailData={userEmailData}
|
|
|
|
deleteEmailAsync={deleteEmailAsync}
|
|
|
|
/>
|
|
|
|
{(makePrimaryAsync.isError || deleteEmailAsync.isError) && (
|
|
|
|
<div className="text-danger small">
|
2022-04-29 07:10:10 -04:00
|
|
|
{t('generic_something_went_wrong')}
|
2022-04-22 09:49:26 -04:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Actions
|