From af12f841b10fbb458bcf7d6c150e86040dae35da Mon Sep 17 00:00:00 2001 From: David Arranz Date: Tue, 17 Sep 2024 17:00:33 +0200 Subject: [PATCH] Login con idiomas --- client/package.json | 1 + .../auth/LoginPageWithLanguageSelector.tsx | 200 ++++++++++++++++++ client/yarn.lock | 5 + 3 files changed, 206 insertions(+) create mode 100644 client/src/app/auth/LoginPageWithLanguageSelector.tsx diff --git a/client/package.json b/client/package.json index f95abdd..ad36a47 100644 --- a/client/package.json +++ b/client/package.json @@ -50,6 +50,7 @@ "cmdk": "^1.0.0", "date-fns": "^3.6.0", "embla-carousel-react": "^8.2.0", + "flag-icons": "^7.2.3", "i18next": "^23.12.2", "i18next-browser-languagedetector": "^8.0.0", "joi": "^17.13.1", diff --git a/client/src/app/auth/LoginPageWithLanguageSelector.tsx b/client/src/app/auth/LoginPageWithLanguageSelector.tsx new file mode 100644 index 0000000..e602a28 --- /dev/null +++ b/client/src/app/auth/LoginPageWithLanguageSelector.tsx @@ -0,0 +1,200 @@ +import { Container, FormTextField } from "@/components"; +import { FormSubmitButton } from "@/components/Forms/FormSubmitButton"; +import { UeckoLogo } from "@/components/UeckoLogo/UeckoLogo"; +import { useLogin } from "@/lib/hooks"; +import { + Alert, + AlertDescription, + AlertTitle, + Button, + Card, + CardContent, + CardDescription, + CardHeader, + CardTitle, + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, + Form, +} from "@/ui"; +import { joiResolver } from "@hookform/resolvers/joi"; +import { ILogin_DTO } from "@shared/contexts"; +import Joi from "joi"; + +import { AlertCircleIcon, ChevronDownIcon } from "lucide-react"; +import { useEffect, useState } from "react"; +import { SubmitHandler, useForm } from "react-hook-form"; +import { Trans, useTranslation } from "react-i18next"; +import { Link } from "react-router-dom"; +import SpanishJoiMessages from "../../spanish-joi-messages.json"; +import "/node_modules/flag-icons/css/flag-icons.min.css"; + +type LoginDataForm = ILogin_DTO; + +const languages = [ + { code: "en", name: "English", flag: () => }, + { code: "es", name: "Español", flag: () => }, + { code: "fr", name: "Français", flag: () => }, + { code: "it", name: "Italiano", flag: () => }, +]; + +export const LoginPageWithLanguageSelector = () => { + const { t, i18n } = useTranslation(); + const [language, setLanguage] = useState(i18n.language); + + useEffect(() => { + const browserLang = navigator.language.split("-")[0]; + const supportedLang = languages.find((lang) => lang.code === browserLang); + if (supportedLang) { + changeLanguage(supportedLang.code); + } + }, []); + + const changeLanguage = (lng: string) => { + i18n.changeLanguage(lng); + setLanguage(lng); + }; + + const { mutate: login } = useLogin({ + onSuccess: (data) => { + const { success, error } = data; + if (!success && error) { + form.setError("root", error); + } + }, + onError: (error) => { + console.debug(error); + }, + }); + + const form = useForm({ + mode: "onBlur", + defaultValues: { + email: "", + password: "", + }, + resolver: joiResolver( + Joi.object({ + email: Joi.string() + .email({ tlds: { allow: false } }) + .required(), + password: Joi.string().min(4).alphanum().required(), + }), + { + messages: SpanishJoiMessages, + } + ), + }); + + const onSubmit: SubmitHandler = async (data) => { + login({ email: data.email, password: data.password }, {}); + }; + + return ( + +
+
+
+ + + + + + {languages.map((lang) => ( + changeLanguage(lang.code)}> + {lang.flag()} + {lang.name} + + ))} + + +
+ + + + + + + + + + + +
+ +
+
+ +
+
+ +
+ +
+ + + +
+
+ + {form.formState.errors.root?.message && ( + + + + + + {form.formState.errors.root?.message} + + )} + + + +
+ +
+ + + +
+
+
+ +
+
+ +
+
+ +
+ Image +
+
+ ); +}; diff --git a/client/yarn.lock b/client/yarn.lock index 7423693..7836b34 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -3308,6 +3308,11 @@ find-up@^5.0.0: locate-path "^6.0.0" path-exists "^4.0.0" +flag-icons@^7.2.3: + version "7.2.3" + resolved "https://registry.yarnpkg.com/flag-icons/-/flag-icons-7.2.3.tgz#b67f379fa0ef28c4e605319a78035131bdd8ced7" + integrity sha512-X2gUdteNuqdNqob2KKTJTS+ZCvyWeLCtDz9Ty8uJP17Y4o82Y+U/Vd4JNrdwTAjagYsRznOn9DZ+E/Q52qbmqg== + flat-cache@^3.0.4: version "3.2.0" resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.2.0.tgz#2c0c2d5040c99b1632771a9d105725c0115363ee"