-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to fix lang select #875
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { createForm } from "@modular-forms/solid"; | ||
import { useNavigate } from "@solidjs/router"; | ||
import { createSignal, For, Show } from "solid-js"; | ||
|
||
import { Button, ExternalLink, InfoBox, NiceP, VStack } from "~/components"; | ||
import { useI18n } from "~/i18n/context"; | ||
import { useMegaStore } from "~/state/megaStore"; | ||
import { eify, EN_OPTION, Language, LANGUAGE_OPTIONS, timeout } from "~/utils"; | ||
|
||
type ChooseLanguageForm = { | ||
selectedLanguage: string; | ||
}; | ||
|
||
const COMBINED_OPTIONS: Language[] = [EN_OPTION, ...LANGUAGE_OPTIONS]; | ||
|
||
export function ChooseLanguage() { | ||
const i18n = useI18n(); | ||
const [error, setError] = createSignal<Error>(); | ||
const [state, actions] = useMegaStore(); | ||
const [loading, setLoading] = createSignal(false); | ||
const navigate = useNavigate(); | ||
|
||
function findLanguageByValue(value: string) { | ||
return ( | ||
COMBINED_OPTIONS.find((language) => language.value === value) ?? | ||
EN_OPTION | ||
); | ||
} | ||
|
||
const [_chooseLanguageForm, { Form, Field }] = | ||
createForm<ChooseLanguageForm>({ | ||
initialValues: { | ||
selectedLanguage: findLanguageByValue(state.lang ?? "").value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is selecting the default value to display the user in the select form, but since we are using the
|
||
}, | ||
validate: (values) => { | ||
const errors: Record<string, string> = {}; | ||
if (values.selectedLanguage === undefined) { | ||
errors.selectedLanguage = i18n.t( | ||
"settings.language.error_unsupported_language" | ||
); | ||
} | ||
return errors; | ||
} | ||
}); | ||
|
||
const handleFormSubmit = async (f: ChooseLanguageForm) => { | ||
setLoading(true); | ||
try { | ||
actions.saveLanguage(findLanguageByValue(f.selectedLanguage).value); | ||
|
||
await i18n.changeLanguage( | ||
findLanguageByValue(f.selectedLanguage).label | ||
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these values are saving the state and passing the new language to the i18n api and setting the new lang state so again they should be using the
|
||
); | ||
|
||
await timeout(1000); | ||
navigate("/"); | ||
} catch (e) { | ||
console.error(e); | ||
setError(eify(e)); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<VStack> | ||
<Form onSubmit={handleFormSubmit} class="flex flex-col gap-4"> | ||
<NiceP>{i18n.t("settings.language.caption")}</NiceP> | ||
<ExternalLink href="https://github.com/MutinyWallet/mutiny-web/issues/new"> | ||
{i18n.t("settings.language.request_language_support_link")} | ||
</ExternalLink> | ||
<div /> | ||
<VStack> | ||
<Field name="selectedLanguage"> | ||
{(field, props) => ( | ||
<select | ||
{...props} | ||
value={field.value} | ||
class="w-full rounded-lg bg-m-grey-750 py-2 pl-4 pr-12 text-base font-normal text-white" | ||
> | ||
<For each={COMBINED_OPTIONS}> | ||
{({ value }) => ( | ||
<option | ||
selected={field.value === value} | ||
value={value} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are already iterating over the COMBINED_OPTIONS object we already have access to both value and shortName and can simply select the shortname to be sent to the i18n api while displaying the values to the user
|
||
> | ||
{value} | ||
</option> | ||
)} | ||
</For> | ||
</select> | ||
)} | ||
</Field> | ||
<Show when={error()}> | ||
<InfoBox accent="red">{error()?.message}</InfoBox> | ||
</Show> | ||
<div /> | ||
<Button intent="blue" loading={loading()}> | ||
{i18n.t("settings.language.select_language")} | ||
</Button> | ||
</VStack> | ||
</Form> | ||
</VStack> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
BackLink, | ||
Card, | ||
ChooseLanguage, | ||
DefaultMain, | ||
LargeHeader, | ||
MutinyWalletGuard, | ||
NavBar, | ||
SafeArea | ||
} from "~/components"; | ||
import { useI18n } from "~/i18n/context"; | ||
|
||
export function Language() { | ||
const i18n = useI18n(); | ||
return ( | ||
<MutinyWalletGuard> | ||
<SafeArea> | ||
<DefaultMain> | ||
<BackLink | ||
href="/settings" | ||
title={i18n.t("settings.header")} | ||
/> | ||
<LargeHeader> | ||
{i18n.t("settings.language.title")} | ||
</LargeHeader> | ||
<Card title={i18n.t("settings.language.select_language")}> | ||
<ChooseLanguage /> | ||
</Card> | ||
<div class="h-full" /> | ||
</DefaultMain> | ||
<NavBar activeTab="settings" /> | ||
</SafeArea> | ||
</MutinyWalletGuard> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface Language { | ||
value: string; | ||
label: string; | ||
} | ||
|
||
export const EN_OPTION: Language = { | ||
value: "English", | ||
label: "en" | ||
}; | ||
|
||
export const LANGUAGE_OPTIONS: Language[] = [ | ||
{ | ||
value: "Português", | ||
label: "pt" | ||
}, | ||
{ | ||
value: "Korean", | ||
label: "ko" | ||
} | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is trying to find the
Language
object based on the value that is stored in localStorage or state which is acutally theshortName
but as you'll se below we are searching for the form value based on the shortname already we don't need this function anymore and it can be removed