|
| 1 | +import { useRef } from 'react'; |
| 2 | +import { FormattedMessage } from 'react-intl'; |
| 3 | +import { useSearchParams } from 'react-router-dom'; |
| 4 | + |
| 5 | +import { PROFILE_SETTINGS_DEFAULT_OPTION } from '@/common/constants/profileSettings.constants'; |
| 6 | +import { QueryParams } from '@/common/constants/routes.constants'; |
| 7 | +import { getRecordProfileId } from '@/common/helpers/record.helper'; |
| 8 | +import { useDismissMenu } from '@/common/hooks/useDismissMenu'; |
| 9 | +import { Button, ButtonType } from '@/components/Button'; |
| 10 | + |
| 11 | +import { useLoadProfileSettingsMeta } from '@/features/profiles'; |
| 12 | + |
| 13 | +import { useInputsState, useProfileState } from '@/store'; |
| 14 | + |
| 15 | +import Settings from '@/assets/settings.svg?react'; |
| 16 | + |
| 17 | +export const ProfileSettingsSelector = () => { |
| 18 | + const ref = useRef<HTMLDivElement>(null); |
| 19 | + const [searchParams] = useSearchParams(); |
| 20 | + const { record } = useInputsState(['record']); |
| 21 | + const { setSelectedProfileSettingsId } = useProfileState(['setSelectedProfileSettingsId']); |
| 22 | + const recordProfileId = getRecordProfileId(record); |
| 23 | + const profileIdParam = searchParams.get(QueryParams.ProfileId); |
| 24 | + const selectedProfileId = recordProfileId ?? profileIdParam; |
| 25 | + const { data: profileSettings } = useLoadProfileSettingsMeta(selectedProfileId); |
| 26 | + |
| 27 | + const { isOpen: isMenuEnabled, setIsOpen: setIsMenuEnabled, toggle: toggleIsMenuEnabled } = useDismissMenu(ref); |
| 28 | + |
| 29 | + const handleSettingClick = async (profileSettingsId: string | number) => { |
| 30 | + setIsMenuEnabled(false); |
| 31 | + setSelectedProfileSettingsId(profileSettingsId.toString()); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="profile-settings-select" ref={ref}> |
| 36 | + <Button ariaHaspopup="menu" ariaExpanded={isMenuEnabled} onClick={toggleIsMenuEnabled}> |
| 37 | + <Settings /> |
| 38 | + </Button> |
| 39 | + {isMenuEnabled && ( |
| 40 | + <ul |
| 41 | + data-testid="profile-settings-select-menu" |
| 42 | + className="profile-settings-select-menu" |
| 43 | + role="menu" // NOSONAR |
| 44 | + aria-labelledby="selector-title" |
| 45 | + > |
| 46 | + <li id="selector-title" role="none"> |
| 47 | + <FormattedMessage id="ld.applyProfileSettings" /> |
| 48 | + </li> |
| 49 | + <li role="none"> |
| 50 | + <Button |
| 51 | + type={ButtonType.Text} |
| 52 | + label={<FormattedMessage id="ld.profileDefaults" />} |
| 53 | + role="menuitem" |
| 54 | + onClick={() => handleSettingClick(PROFILE_SETTINGS_DEFAULT_OPTION)} |
| 55 | + tabbable={isMenuEnabled} |
| 56 | + /> |
| 57 | + </li> |
| 58 | + {profileSettings?.map(profileSetting => { |
| 59 | + return ( |
| 60 | + <li key={profileSetting.id} role="none"> |
| 61 | + <Button |
| 62 | + type={ButtonType.Text} |
| 63 | + label={profileSetting.name} |
| 64 | + role="menuitem" |
| 65 | + onClick={() => handleSettingClick(profileSetting.id)} |
| 66 | + tabbable={isMenuEnabled} |
| 67 | + /> |
| 68 | + </li> |
| 69 | + ); |
| 70 | + })} |
| 71 | + </ul> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + ); |
| 75 | +}; |
0 commit comments