This commit is contained in:
David Arranz 2024-07-12 19:29:28 +02:00
parent 738e9c9c69
commit adbe76fd09
6 changed files with 29 additions and 19 deletions

View File

@ -8,7 +8,7 @@ import { t } from "i18next";
import { SubmitButton } from "@/components"; import { SubmitButton } from "@/components";
import { useWarnAboutChange } from "@/lib/hooks"; import { useWarnAboutChange } from "@/lib/hooks";
import { Form } from "@/ui"; import { Button, Form } from "@/ui";
import { ICreateQuote_Request_DTO } from "@shared/contexts"; import { ICreateQuote_Request_DTO } from "@shared/contexts";
import { useEffect } from "react"; import { useEffect } from "react";
import { FieldErrors, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form"; import { FieldErrors, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form";
@ -104,12 +104,9 @@ export const QuoteCreate = () => {
/> />
<div className='flex items-center justify-around gap-2'> <div className='flex items-center justify-around gap-2'>
<BackHistoryButton <Button size='sm' variant={"outline"} url='/quotes'>
size='sm' {t("quotes.create.buttons.discard")}
variant={"outline"} </Button>
label={t("quotes.create.buttons.discard")}
url='/quotes'
/>
<SubmitButton size='sm' label={t("common.continue")}></SubmitButton> <SubmitButton size='sm' label={t("common.continue")}></SubmitButton>
</div> </div>

View File

@ -33,6 +33,7 @@ export const UserButton = () => {
}, },
}); });
const { data, status } = useGetProfile(); const { data, status } = useGetProfile();
console.log(data, status);
const openUserMenu = (event: SyntheticEvent) => { const openUserMenu = (event: SyntheticEvent) => {
event.preventDefault(); event.preventDefault();

View File

@ -1,5 +1,6 @@
import { useIsLoggedIn } from "@/lib/hooks"; import { useGetProfile, useIsLoggedIn } from "@/lib/hooks";
import React from "react"; import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Navigate } from "react-router-dom"; import { Navigate } from "react-router-dom";
import { LoadingOverlay } from "../LoadingOverlay"; import { LoadingOverlay } from "../LoadingOverlay";
@ -9,8 +10,17 @@ type ProctectRouteProps = {
export const ProtectedRoute = ({ children }: ProctectRouteProps) => { export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
const { isPending, isSuccess, data: { authenticated, redirectTo } = {} } = useIsLoggedIn(); const { isPending, isSuccess, data: { authenticated, redirectTo } = {} } = useIsLoggedIn();
const { data: profile, ...profileStatus } = useGetProfile();
const { i18n } = useTranslation();
if (isPending) { useEffect(() => {
if (profileStatus.isSuccess && i18n.language !== profile?.lang_code) {
console.log(profile);
i18n.changeLanguage(profile?.lang_code);
}
}, [profile, profileStatus, i18n]);
if (isPending || profileStatus.isPending) {
return <LoadingOverlay />; return <LoadingOverlay />;
} }

View File

@ -1,4 +1,4 @@
import { IIdentity_Response_DTO, ILogin_DTO, ILogin_Response_DTO } from "@shared/contexts"; import { IGetProfileResponse_DTO, ILogin_DTO, ILogin_Response_DTO } from "@shared/contexts";
import secureLocalStorage from "react-secure-storage"; import secureLocalStorage from "react-secure-storage";
import { IAuthActions } from "../hooks"; import { IAuthActions } from "../hooks";
import { createAxiosInstance } from "./axiosInstance"; import { createAxiosInstance } from "./axiosInstance";
@ -47,10 +47,10 @@ export const createAxiosAuthActions = (
}, },
check: () => { check: () => {
const profile = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO; const authUser = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
return Promise.resolve( return Promise.resolve(
profile?.token authUser?.token
? { ? {
authenticated: true, authenticated: true,
} }
@ -71,17 +71,17 @@ export const createAxiosAuthActions = (
*/ */
try { try {
const result = await httpClient.request<IIdentity_Response_DTO>({ const result = await httpClient.request<IGetProfileResponse_DTO>({
url: `${apiUrl}/profile`, url: `${apiUrl}/profile`,
method: "GET", method: "GET",
}); });
const { data } = result; const { data: profile } = result;
const profile = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO; const authUser = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
if (profile?.id === data?.id) { if (authUser?.id === profile?.id) {
secureLocalStorage.setItem("uecko.profile", data); secureLocalStorage.setItem("uecko.profile", profile);
return Promise.resolve(data); return Promise.resolve(profile);
} }
return Promise.resolve(null); return Promise.resolve(null);
} catch (error) { } catch (error) {

View File

@ -10,6 +10,7 @@ export interface IGetProfilePresenter {
export const GetProfilePresenter: IGetProfilePresenter = { export const GetProfilePresenter: IGetProfilePresenter = {
map: (profile: Profile, user: AuthUser, context: IProfileContext): IGetProfileResponse_DTO => { map: (profile: Profile, user: AuthUser, context: IProfileContext): IGetProfileResponse_DTO => {
return { return {
id: user.id.toString(),
name: user.name.toString(), name: user.name.toString(),
email: user.email.toString(), email: user.email.toString(),
lang_code: user.language.code, lang_code: user.language.code,

View File

@ -1,4 +1,5 @@
export interface IGetProfileResponse_DTO { export interface IGetProfileResponse_DTO {
id: string;
name: string; name: string;
email: string; email: string;
lang_code: string; lang_code: string;