fix(frontend): resolve hydration mismatch and unauthorized api calls
This commit is contained in:
parent
364826c5c8
commit
c5f6b1317e
2 changed files with 28 additions and 3 deletions
|
|
@ -24,6 +24,7 @@ import { useState, useEffect } from "react";
|
||||||
import { useNotify } from "@/contexts/notification-context";
|
import { useNotify } from "@/contexts/notification-context";
|
||||||
import { useTranslation } from "@/lib/i18n";
|
import { useTranslation } from "@/lib/i18n";
|
||||||
import { jobsApi } from "@/lib/api";
|
import { jobsApi } from "@/lib/api";
|
||||||
|
import { useAuth } from "@/contexts/AuthContext";
|
||||||
|
|
||||||
interface JobCardProps {
|
interface JobCardProps {
|
||||||
job: Job;
|
job: Job;
|
||||||
|
|
@ -33,12 +34,17 @@ interface JobCardProps {
|
||||||
|
|
||||||
export function JobCard({ job, isApplied, applicationStatus }: JobCardProps) {
|
export function JobCard({ job, isApplied, applicationStatus }: JobCardProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const { user } = useAuth();
|
||||||
const [isFavorited, setIsFavorited] = useState(false);
|
const [isFavorited, setIsFavorited] = useState(false);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const notify = useNotify();
|
const notify = useNotify();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const checkFavorite = async () => {
|
const checkFavorite = async () => {
|
||||||
|
if (!user) {
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const res = await jobsApi.checkFavorite(job.id);
|
const res = await jobsApi.checkFavorite(job.id);
|
||||||
setIsFavorited(res.isFavorite);
|
setIsFavorited(res.isFavorite);
|
||||||
|
|
@ -49,10 +55,14 @@ export function JobCard({ job, isApplied, applicationStatus }: JobCardProps) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
checkFavorite();
|
checkFavorite();
|
||||||
}, [job.id]);
|
}, [job.id, user]);
|
||||||
|
|
||||||
const handleFavorite = async () => {
|
const handleFavorite = async () => {
|
||||||
if (isLoading) return;
|
if (isLoading) return;
|
||||||
|
if (!user) {
|
||||||
|
notify.error("Erro", "Faça login para salvar vagas.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isFavorited) {
|
if (isFavorited) {
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,22 @@ function resolveKey(dict: Record<string, unknown>, key: string): string | undefi
|
||||||
}
|
}
|
||||||
|
|
||||||
export function I18nProvider({ children }: { children: ReactNode }) {
|
export function I18nProvider({ children }: { children: ReactNode }) {
|
||||||
const [locale, setLocaleState] = useState<Locale>(getInitialLocale);
|
const [locale, setLocaleState] = useState<Locale>('pt-BR');
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let initialLocale: Locale = 'pt-BR';
|
||||||
|
try {
|
||||||
|
const storedLocale = localStorage.getItem(localeStorageKey);
|
||||||
|
if (storedLocale && VALID_LOCALES.includes(storedLocale as Locale)) {
|
||||||
|
initialLocale = storedLocale as Locale;
|
||||||
|
} else if (navigator.language) {
|
||||||
|
initialLocale = normalizeLocale(navigator.language);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// localStorage might be blocked
|
||||||
|
}
|
||||||
|
setLocaleState(initialLocale);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const setLocale = (newLocale: Locale) => {
|
const setLocale = (newLocale: Locale) => {
|
||||||
setLocaleState(newLocale);
|
setLocaleState(newLocale);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue