refactoring component

This commit is contained in:
Antoine M 2024-02-15 18:55:31 +01:00
parent 0eebb2be4d
commit 23050427c7

View File

@ -1,11 +1,16 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { useUser } from "../../hooks/useUser"; import { useUser } from "../../hooks/useUser";
import translationsJSON from "../../data/profile.json";
export default function CountrySelect({ options }) { export default function CountrySelect({ options }) {
const [isSubmenuOpen, setIsSubmenuOpen] = useState(false); const [isSubmenuOpen, setIsSubmenuOpen] = useState(false);
const { profile, changeProfile } = useUser(); const { profile, changeProfile } = useUser();
const { language } = useUser();
const languageTranslationExists = translationsJSON.hasOwnProperty(language);
const screenTranslations = languageTranslationExists ? translationsJSON[language] : translationsJSON["en"];
function handleShowHideSubmenu() { function handleShowHideSubmenu() {
setIsSubmenuOpen(!isSubmenuOpen); setIsSubmenuOpen(!isSubmenuOpen);
} }
@ -14,21 +19,25 @@ export default function CountrySelect({ options }) {
changeProfile("subcontractor_employee"); changeProfile("subcontractor_employee");
} }
if (!profile) return <div className='profile-select dropdown-select'> Loading...</div>; console.log(screenTranslations);
const selectOptions = Object.keys(options).map((key) => ( // console.log(options);
<li key={uuidv4()} className='dropdown-select__option' onClick={() => handleChangeProfile()}>
{options[key]}
</li>
));
return ( return (
<div className='profile-select dropdown-select'> <div className='profile-select'>
<button className='dropdown-select__toggle' onClick={handleShowHideSubmenu}> <ul className='' aria-hidden={!isSubmenuOpen}>
{profile} <li value='civilian' onClick={handleChangeProfile}>
</button> {screenTranslations.civilian}
<ul className='dropdown-select__select-list' aria-hidden={!isSubmenuOpen}> </li>
{selectOptions} <li value='driver' onClick={handleChangeProfile}>
{screenTranslations.driver}
</li>
<li value='lhoist_employee' onClick={handleChangeProfile}>
{screenTranslations.lhoist_employee}
</li>
<li value='subcontractor_employee' onClick={handleChangeProfile}>
{screenTranslations.subcontractor_employee}
</li>
</ul> </ul>
</div> </div>
); );