overleaf/services/web/frontend/js/features/settings/components/emails/actions.tsx
Timothée Alby cf2dfc6bf1 Merge pull request #7593 from overleaf/ta-settings-migration
[SettingsPage] Integration Branch

GitOrigin-RevId: 5a3c26b2a02d716c4ae3981e3f08b811ae307725
2022-04-25 08:05:12 +00:00

62 lines
1.8 KiB
TypeScript

import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import MakePrimary from './actions/make-primary'
import Remove from './actions/remove'
import Icon from '../../../../shared/components/icon'
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">
<Icon type="exclamation-triangle" fw />{' '}
{t('error_performing_request')}
</div>
)}
</>
)
}
export default Actions