Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #635: Redirecionamento ao recarregar perfil #636

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading