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 { useWarnAboutChange } from "@/lib/hooks";
import { Form } from "@/ui";
import { Button, Form } from "@/ui";
import { ICreateQuote_Request_DTO } from "@shared/contexts";
import { useEffect } from "react";
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'>
<BackHistoryButton
size='sm'
variant={"outline"}
label={t("quotes.create.buttons.discard")}
url='/quotes'
/>
<Button size='sm' variant={"outline"} url='/quotes'>
{t("quotes.create.buttons.discard")}
</Button>
<SubmitButton size='sm' label={t("common.continue")}></SubmitButton>
</div>

View File

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

View File

@ -1,5 +1,6 @@
import { useIsLoggedIn } from "@/lib/hooks";
import React from "react";
import { useGetProfile, useIsLoggedIn } from "@/lib/hooks";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Navigate } from "react-router-dom";
import { LoadingOverlay } from "../LoadingOverlay";
@ -9,8 +10,17 @@ type ProctectRouteProps = {
export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
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 />;
}

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

View File

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

View File

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