From 85f731110ca8e142767ce296c13a4da94f642718 Mon Sep 17 00:00:00 2001 From: ilkin-overleaf <100852799+ilkin-overleaf@users.noreply.github.com> Date: Fri, 29 Apr 2022 14:09:53 +0300 Subject: [PATCH] Merge pull request #7786 from overleaf/ii-departments-override Override default departments GitOrigin-RevId: 23061bc8c083bb8099ca62bd0cdb3c796e49979d --- .../settings/components/emails/add-email.tsx | 19 ++++++++++-- .../emails/institution-and-role.tsx | 28 ++++++++++++++--- .../stories/settings/helpers/emails.js | 29 ++++++++++++++++-- .../emails-section-add-new-email.test.tsx | 22 ++++++++++---- ...ails-section-institution-and-role.test.tsx | 30 +++++++++++++++++++ services/web/types/university.ts | 2 +- 6 files changed, 114 insertions(+), 16 deletions(-) diff --git a/services/web/frontend/js/features/settings/components/emails/add-email.tsx b/services/web/frontend/js/features/settings/components/emails/add-email.tsx index 584ccb0fb2..906e65c034 100644 --- a/services/web/frontend/js/features/settings/components/emails/add-email.tsx +++ b/services/web/frontend/js/features/settings/components/emails/add-email.tsx @@ -9,8 +9,8 @@ import { AddEmailInput, InstitutionInfo } from './add-email-input' import useAsync from '../../../../shared/hooks/use-async' import { useUserEmailsContext } from '../../context/user-email-context' import { getJSON, postJSON } from '../../../../infrastructure/fetch-json' -import { defaults as roles } from '../../roles' -import { defaults as departments } from '../../departments' +import { defaults as defaultRoles } from '../../roles' +import { defaults as defaultDepartments } from '../../departments' import { University } from '../../../../../../types/university' import { CountryCode } from '../../../../../../types/country' import { ExposedSettings } from '../../../../../../types/exposed-settings' @@ -49,6 +49,7 @@ function AddEmail() { const [university, setUniversity] = useState('') const [role, setRole] = useState('') const [department, setDepartment] = useState('') + const [departments, setDepartments] = useState(defaultDepartments) const [isInstitutionFieldsVisible, setIsInstitutionFieldsVisible] = useState(false) const [isUniversityDirty, setIsUniversityDirty] = useState(false) @@ -70,6 +71,18 @@ function AddEmail() { } }, [setIsUniversityDirty, university]) + useEffect(() => { + const selectedKnownUniversity = countryCode + ? universities[countryCode]?.find(({ name }) => name === university) + : undefined + + if (selectedKnownUniversity && selectedKnownUniversity.departments.length) { + setDepartments(selectedKnownUniversity.departments) + } else { + setDepartments(defaultDepartments) + } + }, [countryCode, universities, university]) + // Fetch country institution useEffect(() => { // Skip if country not selected or universities for @@ -222,7 +235,7 @@ function AddEmail() { <>