From 726668f71635e95d63dd25be42b22e9916e62cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Wed, 18 May 2022 15:46:18 +0200 Subject: [PATCH] Merge pull request #7941 from overleaf/msm-use-async-typing-fix Fix typescript errors related to `useAsync()` hook GitOrigin-RevId: eacf7ec7e9c5da38ad88fde225ffedbfd1c2ff61 --- .../components/account-info-section.tsx | 7 +++-- .../emails/add-email/institution-fields.tsx | 4 +-- .../components/emails/add-email/layout.tsx | 3 ++- .../emails/institution-and-role.tsx | 6 ++--- .../settings/components/password-section.tsx | 16 +++++++++--- .../settings/context/user-email-context.tsx | 5 ++-- .../frontend/js/infrastructure/fetch-json.ts | 9 +++++++ .../web/frontend/js/shared/hooks/use-async.ts | 24 ++++++++++------- .../infrastructure/fetch-json.test.js | 26 +++++++++++++++++++ .../frontend/shared/hooks/use-async.test.ts | 2 +- 10 files changed, 77 insertions(+), 25 deletions(-) diff --git a/services/web/frontend/js/features/settings/components/account-info-section.tsx b/services/web/frontend/js/features/settings/components/account-info-section.tsx index b158e8d5f8..11a027cc0f 100644 --- a/services/web/frontend/js/features/settings/components/account-info-section.tsx +++ b/services/web/frontend/js/features/settings/components/account-info-section.tsx @@ -7,7 +7,10 @@ import { FormGroup, } from 'react-bootstrap' import { useTranslation } from 'react-i18next' -import { postJSON } from '../../../infrastructure/fetch-json' +import { + getUserFacingMessage, + postJSON, +} from '../../../infrastructure/fetch-json' import getMeta from '../../../utils/meta' import { ExposedSettings } from '../../../../../types/exposed-settings' import useAsync from '../../../shared/hooks/use-async' @@ -109,7 +112,7 @@ function AccountInfoSection() { ) : null} {isError ? ( - {error.getUserFacingMessage()} + {getUserFacingMessage(error)} ) : null} {canUpdateEmail || canUpdateNames ? ( diff --git a/services/web/frontend/js/features/settings/components/emails/add-email/institution-fields.tsx b/services/web/frontend/js/features/settings/components/emails/add-email/institution-fields.tsx index 1fa409e341..a4de478da1 100644 --- a/services/web/frontend/js/features/settings/components/emails/add-email/institution-fields.tsx +++ b/services/web/frontend/js/features/settings/components/emails/add-email/institution-fields.tsx @@ -49,7 +49,7 @@ function InstitutionFields({ const [isInstitutionFieldsVisible, setIsInstitutionFieldsVisible] = useState(false) const [isUniversityDirty, setIsUniversityDirty] = useState(false) - const { runAsync: institutionRunAsync } = useAsync() + const { runAsync: institutionRunAsync } = useAsync() useEffect(() => { if (isInstitutionFieldsVisible && countryRef.current) { @@ -93,7 +93,7 @@ function InstitutionFields({ return } - institutionRunAsync( + institutionRunAsync( getJSON(`/institutions/list?country_code=${countryCode}`) ) .then(data => { diff --git a/services/web/frontend/js/features/settings/components/emails/add-email/layout.tsx b/services/web/frontend/js/features/settings/components/emails/add-email/layout.tsx index dfde8e7cb9..2ca36f0d19 100644 --- a/services/web/frontend/js/features/settings/components/emails/add-email/layout.tsx +++ b/services/web/frontend/js/features/settings/components/emails/add-email/layout.tsx @@ -1,6 +1,7 @@ import { Row, Alert } from 'react-bootstrap' import Icon from '../../../../../shared/components/icon' import { UseAsyncReturnType } from '../../../../../shared/hooks/use-async' +import { getUserFacingMessage } from '../../../../../infrastructure/fetch-json' type LayoutProps = { children: React.ReactNode @@ -14,7 +15,7 @@ function Layout({ isError, error, children }: LayoutProps) { {children} {isError && ( - {error.getUserFacingMessage()} + {getUserFacingMessage(error)} )} diff --git a/services/web/frontend/js/features/settings/components/emails/institution-and-role.tsx b/services/web/frontend/js/features/settings/components/emails/institution-and-role.tsx index e7ecdcee76..d93e726598 100644 --- a/services/web/frontend/js/features/settings/components/emails/institution-and-role.tsx +++ b/services/web/frontend/js/features/settings/components/emails/institution-and-role.tsx @@ -18,7 +18,7 @@ type InstitutionAndRoleProps = { function InstitutionAndRole({ userEmailData }: InstitutionAndRoleProps) { const { t } = useTranslation() const { isLoading, isError, runAsync } = useAsync() - const changeAffiliationAsync = useAsync() + const changeAffiliationAsync = useAsync() const { affiliation } = userEmailData const { state, @@ -55,9 +55,7 @@ function InstitutionAndRole({ userEmailData }: InstitutionAndRoleProps) { } changeAffiliationAsync - .runAsync( - getJSON(`/institutions/list/${affiliation.institution.id}`) - ) + .runAsync(getJSON(`/institutions/list/${affiliation.institution.id}`)) .then(data => { if (data.departments.length) { setDepartments(data.departments) diff --git a/services/web/frontend/js/features/settings/components/password-section.tsx b/services/web/frontend/js/features/settings/components/password-section.tsx index 31dbe432a4..367a90732a 100644 --- a/services/web/frontend/js/features/settings/components/password-section.tsx +++ b/services/web/frontend/js/features/settings/components/password-section.tsx @@ -7,12 +7,21 @@ import { FormGroup, } from 'react-bootstrap' import { useTranslation } from 'react-i18next' -import { postJSON } from '../../../infrastructure/fetch-json' +import { + getUserFacingMessage, + postJSON, +} from '../../../infrastructure/fetch-json' import getMeta from '../../../utils/meta' import { ExposedSettings } from '../../../../../types/exposed-settings' import { PasswordStrengthOptions } from '../../../../../types/password-strength-options' import useAsync from '../../../shared/hooks/use-async' +type PasswordUpdateResult = { + message?: { + text: string + } +} + function PasswordSection() { const { t } = useTranslation() @@ -58,7 +67,8 @@ function PasswordForm() { const [currentPassword, setCurrentPassword] = useState('') const [newPassword1, setNewPassword1] = useState('') const [newPassword2, setNewPassword2] = useState('') - const { isLoading, isSuccess, isError, data, error, runAsync } = useAsync() + const { isLoading, isSuccess, isError, data, error, runAsync } = + useAsync() const [isNewPasswordValid, setIsNewPasswordValid] = useState(false) const [isFormValid, setIsFormValid] = useState(false) @@ -128,7 +138,7 @@ function PasswordForm() { ) : null} {isError ? ( - {error.getUserFacingMessage()} + {getUserFacingMessage(error)} ) : null}