Skip to content

Commit

Permalink
refactor: try to encapsulate logic for rendering TransLangPrefBanner …
Browse files Browse the repository at this point in the history
…a bit more
  • Loading branch information
nsantacruz committed Sep 18, 2023
1 parent c8bca12 commit 971c3de
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions static/js/TextColumnBanner.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,45 @@ const cookie = Sefaria._inBrowser ? $.cookie : Sefaria.util.cookie;
const { translation_language_preference_suggestion } = Sefaria;

export const TextColumnBannerChooser = ({ setTranslationLanguagePreference, openTranslations, openTransBannerApplies }) => {
const [bannerAccepted, setBannerAccepted] = useState(false);
const [transLangPrefAccepted, setTransLangPrefAccepted] = useState(false);
const shouldTransPrefBannerRender = () => {
// we haven't suggested yet and we have a suggestion
return !cookie("translation_language_preference_suggested") && translation_language_preference_suggestion
return transLangPrefAccepted || (!cookie("translation_language_preference_suggested") && translation_language_preference_suggestion);
};
const shouldOpenTransBannerRender = () => {
return openTransBannerApplies() && !cookie("open_trans_banner_shown"); // && textMode in (bilingual, english) && category in (Tanakh, Mishnah, Bavli)
}

if (shouldTransPrefBannerRender()) {
return (<TransLangPrefBanner
setAccepted={setBannerAccepted}
setTranslationLanguagePreference={setTranslationLanguagePreference}
/>);
} else if (bannerAccepted) {
return <TransLangPrefAcceptedBanner />;
return (
<TransLangPrefBanner
accepted={transLangPrefAccepted}
setAccepted={setTransLangPrefAccepted}
setTranslationLanguagePreference={setTranslationLanguagePreference}
/>
);
}
if (shouldOpenTransBannerRender()) {
else if (shouldOpenTransBannerRender()) {
return <OpenTransBanner openTranslations={openTranslations} />;
}
return null;
};


const TransLangPrefBanner = ({accepted, setAccepted, setTranslationLanguagePreference}) => {
if (accepted) {
return <TransLangPrefAcceptedBanner />;
}

return (
<TransLangPrefAskBanner
setAccepted={setAccepted}
setTranslationLanguagePreference={setTranslationLanguagePreference}
/>
);
}


const TransLangPrefAcceptedBanner = () => {
const lang = Sefaria.translateISOLanguageCode(translation_language_preference_suggestion);
return (
Expand All @@ -45,7 +61,7 @@ const TransLangPrefAcceptedBanner = () => {
}


const TransLangPrefBanner = ({ setAccepted, setTranslationLanguagePreference }) => {
const TransLangPrefAskBanner = ({ setAccepted, setTranslationLanguagePreference }) => {
const reject = () => {
cookie("translation_language_preference_suggested", JSON.stringify(1), {path: "/"});
Sefaria.editProfileAPI({settings: {translation_language_preference_suggested: true}});
Expand Down

0 comments on commit 971c3de

Please sign in to comment.