Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Diverse forbedring i navning og lesbarhet
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeofnorway committed Feb 6, 2023
1 parent 6600788 commit 073c5a9
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/komponenter/header/common/sprakvelger/SprakVelger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ export const SprakVelger = (props: Props) => {
const store = useStore();
const availableLanguages = props.languages;
const { language } = useSelector((state: AppState) => state.language);
const [isOpen, setIsOpen] = React.useState(false);
const [selectedItem, setSelectedItem] = React.useState<LocaleOption | null>(null);
const [isMenuOpen, setIsMenuOpen] = React.useState(false);
const [selectedLocale, setSelectedLocale] = React.useState<LocaleOption | null>(null);
const [, setCookie] = useCookies([decoratorLanguageCookie]);
const options = transformOptions(availableLanguages).sort((a, b) => (a.label > b.label ? -1 : 1));
const languageOptions = transformOptions(availableLanguages).sort((a, b) => (a.label > b.label ? -1 : 1));

useEffect(() => {
if (selectedItem === null) {
const selected = options.find((option) => option.locale === language);
setSelectedItem(selected || null);
if (selectedLocale === null) {
const selected = languageOptions.find((option) => option.locale === language);
setSelectedLocale(selected || null);
}
}, []);

const onChange = (selected: LocaleOption) => {
const { label, ...selectedLanguage } = selected;
setSelectedItem(selected);
setSelectedLocale(selected);
setCookie(decoratorLanguageCookie, selectedLanguage.locale, cookieOptions);
store.dispatch(languageDuck.actionCreator({ language: selectedLanguage.locale }));

Expand All @@ -66,25 +66,25 @@ export const SprakVelger = (props: Props) => {
}
};

const onMouseClick = (e: MouseEvent) => {
const onClick = (e: MouseEvent) => {
const isClickOutside = !e.composedPath().some((el) => el === document.querySelector(`.${style.sprakvelger}`));
if (isClickOutside) {
toggleMenu(false);
}
};

const toggleMenu = (open?: boolean) => {
const intentionToOpen = open !== undefined ? open : !isOpen;
const isIntentToOpen = open !== undefined ? open : !isMenuOpen;

if (intentionToOpen) {
if (isIntentToOpen) {
window.addEventListener('keyup', onKeyUp);
window.addEventListener('click', onMouseClick);
window.addEventListener('click', onClick);
} else {
window.removeEventListener('keyup', onKeyUp);
window.removeEventListener('click', onMouseClick);
window.removeEventListener('click', onClick);
}

setIsOpen(intentionToOpen);
setIsMenuOpen(isIntentToOpen);
};

return (
Expand All @@ -93,7 +93,7 @@ export const SprakVelger = (props: Props) => {
<button
className={`${style.knapp} skjemaelement__input`}
type="button"
aria-expanded={isOpen}
aria-expanded={isMenuOpen}
onClick={() => toggleMenu()}
>
<div className={style.knappTekst}>
Expand All @@ -104,13 +104,13 @@ export const SprakVelger = (props: Props) => {
</div>
<Expand className={style.chevronNed} aria-hidden />
</button>
<ul className={classNames(style.menu, isOpen && style.menuOpenState)}>
{options.map((option, index) => (
<ul className={classNames(style.menu, isMenuOpen && style.menuOpenState)}>
{languageOptions.map((option, index) => (
<SprakVelgerItem
key={index}
item={option}
index={index}
selectedItem={selectedItem}
selectedItem={selectedLocale}
onSelectedItemChange={() => onChange(option as LocaleOption)}
/>
))}
Expand Down

0 comments on commit 073c5a9

Please sign in to comment.