Skip to content

Commit

Permalink
Merge pull request #636 from universi-me/fix#635/profile/redirecionam…
Browse files Browse the repository at this point in the history
…ento-recarregar

FIX #635: Redirecionamento ao recarregar perfil
  • Loading branch information
NiiMiyo authored Oct 22, 2024
2 parents 5314623 + facee73 commit cb56020
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/contexts/Auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { UniversimeApi } from "@/services/UniversimeApi";
import { goTo } from "@/services/routes";
import type { Group } from "@/types/Group";
import type { Link } from "@/types/Link";
import { Nullable, Possibly } from "@/types/utils";
import ErrorPage from "@/components/ErrorPage";

export const AuthProvider = ({ children }: { children: ReactNode }) => {
const [profile, setProfile] = useState<ProfileClass | null>(null);
const [profile, setProfile] = useState<Possibly<ProfileClass>>();
const [profileLinks, setProfileLinks] = useState<Link[]>([]);
const [profileGroups, setProfileGroups] = useState<Group[]>([]);
const [organization, setOrganization] = useState<Group | null | undefined>();
const [organization, setOrganization] = useState<Possibly<Group>>();
const [finishedLogin, setFinishedLogin] = useState<boolean>(false);
const [isHealthy, setIsHealthy] = useState<boolean>();
const user = profile?.user ?? null;
Expand All @@ -25,8 +26,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
goTo("/manage-profile");
}

if (organization === undefined || isHealthy === undefined)
// Organization or status not fetched from the API yet
if ( organization === undefined || isHealthy === undefined || profile === undefined )
// Organization, status or user not fetched from the API yet
return null;

else if (organization === null)
Expand Down Expand Up @@ -94,16 +95,18 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {

async function updateLoggedUser() {
setFinishedLogin(false);
let profile: Nullable<ProfileClass> = null;
const organization = await updateOrganization();

if (organization) {
const profile = await getLoggedProfile();
setProfile(profile);
profile = await getLoggedProfile();

await Promise.all([
updateLinks(),
updateGroups(),
updateLinks(profile),
updateGroups(profile),
]);

setProfile(profile);
}

setFinishedLogin(true);
Expand All @@ -120,7 +123,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
return usedOrganization;
}

async function updateLinks() {
async function updateLinks( profile: Possibly<ProfileClass> ) {
if (!profile) {
setProfileLinks([]);
return [];
Expand All @@ -133,7 +136,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
return links;
}

async function updateGroups() {
async function updateGroups( profile: Possibly<ProfileClass> ) {
if (!profile) {
setProfileGroups([]);
return [];
Expand Down
6 changes: 0 additions & 6 deletions src/pages/Profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export function ProfilePage() {
const [profileContext, setProfileContext] = useState<ProfileContextType>(makeContext(loaderData));
useEffect(() => setProfileContext(makeContext(loaderData)), [ loaderData.profile?.id ]);

useEffect(() => {
if (auth.user === null) {
navigate('/login');
}
}, [auth.user]);

if (!profileContext)
return null;

Expand Down
1 change: 1 addition & 0 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type Nullable<T> = null | T;
export type NullableBoolean = Nullable<boolean>;
export type Optional<T> = T | undefined;
export type Possibly<T> = T | null | undefined;

0 comments on commit cb56020

Please sign in to comment.