|
| 1 | +import { createForm } from "@modular-forms/solid"; |
| 2 | +import { useNavigate } from "@solidjs/router"; |
| 3 | +import { createSignal, For, Show } from "solid-js"; |
| 4 | + |
| 5 | +import { Button, ExternalLink, InfoBox, NiceP, VStack } from "~/components"; |
| 6 | +import { useI18n } from "~/i18n/context"; |
| 7 | +import { useMegaStore } from "~/state/megaStore"; |
| 8 | +import { |
| 9 | + Language, |
| 10 | + eify, |
| 11 | + timeout, |
| 12 | + LANGUAGE_OPTIONS, |
| 13 | + EN_OPTION |
| 14 | +} from "~/utils"; |
| 15 | + |
| 16 | +type ChooseLanguageForm = { |
| 17 | + selectedLanguage: string; |
| 18 | +}; |
| 19 | + |
| 20 | +const COMBINED_OPTIONS: Language[] = [...LANGUAGE_OPTIONS]; |
| 21 | + |
| 22 | +export function ChooseLanguage() { |
| 23 | + const i18n = useI18n(); |
| 24 | + const [error, setError] = createSignal<Error>(); |
| 25 | + const [_state, actions] = useMegaStore(); |
| 26 | + const [loading, setLoading] = createSignal(false); |
| 27 | + const navigate = useNavigate(); |
| 28 | + |
| 29 | + function findLanguageByValue(value: string) { |
| 30 | + return ( |
| 31 | + COMBINED_OPTIONS.find((language) => language.value === value) ?? |
| 32 | + EN_OPTION |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const [_chooseLanguageForm, { Form, Field }] = |
| 37 | + createForm<ChooseLanguageForm>({ |
| 38 | + initialValues: { |
| 39 | + selectedLanguage: "" |
| 40 | + }, |
| 41 | + validate: (values) => { |
| 42 | + const errors: Record<string, string> = {}; |
| 43 | + if (values.selectedLanguage === undefined) { |
| 44 | + errors.selectedLanguage = i18n.t( |
| 45 | + "settings.currency.error_unsupported_currency" |
| 46 | + ); |
| 47 | + } |
| 48 | + return errors; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + const handleFormSubmit = async (f: ChooseLanguageForm) => { |
| 53 | + setLoading(true); |
| 54 | + try { |
| 55 | + actions.saveLanguage(findLanguageByValue(f.selectedLanguage).label); |
| 56 | + |
| 57 | + await timeout(1000); |
| 58 | + navigate("/"); |
| 59 | + } catch (e) { |
| 60 | + console.error(e); |
| 61 | + setError(eify(e)); |
| 62 | + setLoading(false); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + return ( |
| 67 | + <VStack> |
| 68 | + <Form onSubmit={handleFormSubmit} class="flex flex-col gap-4"> |
| 69 | + <NiceP>{i18n.t("settings.language.caption")}</NiceP> |
| 70 | + <ExternalLink href="https://github.com/MutinyWallet/mutiny-web/issues/new"> |
| 71 | + {i18n.t("settings.language.request_language_support_link")} |
| 72 | + </ExternalLink> |
| 73 | + <div /> |
| 74 | + <VStack> |
| 75 | + <Field name="selectedLanguage"> |
| 76 | + {(field, props) => ( |
| 77 | + <select |
| 78 | + {...props} |
| 79 | + value={field.value} |
| 80 | + class="w-full rounded-lg bg-m-grey-750 py-2 pl-4 pr-12 text-base font-normal text-white" |
| 81 | + > |
| 82 | + <For each={COMBINED_OPTIONS}> |
| 83 | + {({ value, label }) => ( |
| 84 | + <option value={value}>{value}</option> |
| 85 | + )} |
| 86 | + </For> |
| 87 | + </select> |
| 88 | + )} |
| 89 | + </Field> |
| 90 | + <Show when={error()}> |
| 91 | + <InfoBox accent="red">{error()?.message}</InfoBox> |
| 92 | + </Show> |
| 93 | + <div /> |
| 94 | + <Button intent="blue" loading={loading()}> |
| 95 | + {i18n.t("settings.language.select_language")} |
| 96 | + </Button> |
| 97 | + </VStack> |
| 98 | + </Form> |
| 99 | + </VStack> |
| 100 | + ); |
| 101 | +} |
0 commit comments