Skip to content

Commit

Permalink
fix: removed calls that we shouldn't do when we are not logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
TeddyRoncin committed Nov 8, 2024
1 parent 49b5c9c commit 7fdbd05
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 39 deletions.
12 changes: 9 additions & 3 deletions src/api/annals/fetchAnnals.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { useEffect, useState } from 'react';
import { API, useAPI } from '@/api/api';
import { Annal } from '@/api/annals/annal.interface';
import { useLoggedIn } from '@/module/session';

export default function useAnnals(
code: string,
): [Annal[] | null, (index: number, exam: Annal) => void, (exam: Annal) => void] {
const [annals, setAnnals] = useState<Annal[] | null>(null);
): [Annal[] | null | undefined, (index: number, exam: Annal) => void, (exam: Annal) => void] {
const [annals, setAnnals] = useState<Annal[] | null | undefined>(undefined);
const api = useAPI();
const logged = useLoggedIn();

useEffect(() => {
api.get<Annal[]>(`/ue/annals?ueCode=${code}`, { timeoutMillis: 15 * 1000 }).on('success', setAnnals);
if (logged && !annals) {
api.get<Annal[]>(`/ue/annals?ueCode=${code}`, { timeoutMillis: 15 * 1000 }).on('success', setAnnals);
} else if (!logged) {
setAnnals(null);
}
}, []);

return [
Expand Down
24 changes: 17 additions & 7 deletions src/api/annals/fetchMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { useEffect, useState } from 'react';
import { useAPI } from '@/api/api';
import { AnnalMetadata, AnnalType } from '@/api/annals/annal.interface';
import { useLoggedIn } from '@/module/session';

export default function useAnnalMetadata(code: string): [types: AnnalType[] | null, semesters: string[] | null] {
const [availableTypes, setAvailableTypes] = useState<AnnalType[] | null>(null);
const [availableSemesters, setAvailableSemesters] = useState<string[] | null>(null);
export default function useAnnalMetadata(
code: string,
): [types: AnnalType[] | null | undefined, semesters: string[] | null | undefined] {
const [availableTypes, setAvailableTypes] = useState<AnnalType[] | null | undefined>(undefined);
const [availableSemesters, setAvailableSemesters] = useState<string[] | null | undefined>(undefined);
const api = useAPI();
const logged = useLoggedIn();

useEffect(() => {
api.get<AnnalMetadata>(`/ue/annals/metadata?ueCode=${code}`).on('success', (metadata) => {
setAvailableTypes(metadata.types);
setAvailableSemesters(metadata.semesters);
});
if (logged && (!availableTypes || !availableSemesters)) {
api.get<AnnalMetadata>(`/ue/annals/metadata?ueCode=${code}`).on('success', (metadata) => {
setAvailableTypes(metadata.types);
setAvailableSemesters(metadata.semesters);
});
} else if (!logged) {
setAvailableTypes(null);
setAvailableSemesters(null);
}
}, []);
return [availableTypes, availableSemesters];
}
4 changes: 3 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ export interface API {
function applyDefaultHandler<T>(handler: ResponseHandler<T>, setNotFound: () => void) {
return handler
.on('success', (body) => body)
.on('failure', () => toast.error('Could not connect to the API'))
.on('failure', () => {
toast.error('Could not connect to the API');
})
.on('error', () => {
toast.error('Request resulted in an error');
})
Expand Down
14 changes: 10 additions & 4 deletions src/api/ueRate/getUERate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { UERate } from '@/api/ueRate/ueRateCriterion.interface';
import { useAPI } from '@/api/api';
import { useEffect, useState } from 'react';
import { useLoggedIn } from '@/module/session';

export default function useUERate(ueCode: string): [UERate[] | null, (newVal: UERate[]) => void] {
const [rates, setRates] = useState<UERate[] | null>(null);
export default function useUERate(ueCode: string): [UERate[] | null | undefined, (newVal: UERate[]) => void] {
const [rates, setRates] = useState<UERate[] | null | undefined>(undefined);
const logged = useLoggedIn();
const api = useAPI();
useEffect(() => {
api.get<UERate[]>(`ue/${ueCode}/rate`).on('success', setRates);
}, []);
if (logged && !rates) {
api.get<UERate[]>(`ue/${ueCode}/rate`).on('success', setRates);
} else if (!logged) {
setRates(null);
}
}, [logged]);
return [rates, setRates];
}
12 changes: 9 additions & 3 deletions src/api/users/getTodaysBirthdays.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { useEffect, useState } from 'react';
import { useAPI } from '@/api/api';
import { User } from '@/api/users/user.interface';
import { useLoggedIn } from '@/module/session';

export default function useTodaysBirthdays(): User[] | null {
const [users, setUsers] = useState<User[] | null>(null);
export default function useTodaysBirthdays(): User[] | null | undefined {
const [users, setUsers] = useState<User[] | null | undefined>(undefined);
const api = useAPI();
const logged = useLoggedIn();
useEffect(() => {
api.get<User[]>('/users/birthdays/today').on('success', setUsers);
if (logged && !users) {
api.get<User[]>('/users/birthdays/today').on('success', setUsers);
} else if (!logged) {
setUsers(null);
}
}, []);
return users;
}
37 changes: 21 additions & 16 deletions src/app/ues/[code]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import styles from './style.module.scss';
import { useParams } from 'next/navigation';
import useUe from '@/api/ue/fetchUe';
import { useAppSelector } from '@/lib/hooks';
import Comments from '@/app/ues/[code]/Comments';
import { useAppTranslation } from '@/lib/i18n';
import { useUERateCriteria } from '@/module/constantData';
Expand All @@ -22,12 +21,13 @@ import useAnnals from '@/api/annals/fetchAnnals';
import useAnnalMetadata from '@/api/annals/fetchMetadata';
import ExamList from '@/components/ues/ExamList';
import ExamSender from '@/components/ues/ExamSender';
import { useLoggedIn } from '@/module/session';

export default function UEDetailsPage() {
usePageSettings({});
const params = useParams<{ code: string }>();
const { t } = useAppTranslation();
const logged = useAppSelector((state) => state.session.logged);
const logged = useLoggedIn();
const [ue, refreshUE] = useUe(params.code);
const criteria = useUERateCriteria();
const [myRates, setMyRates] = useGetRate(params.code);
Expand All @@ -37,7 +37,7 @@ export default function UEDetailsPage() {
const [isAnnalUploaderOpen, setAnnalUploaderOpen] = useState(false);
const api = useAPI();

if (!ue || !criteria || (!myRates && logged)) {
if (!ue || !criteria || myRates === undefined) {
return false;
}

Expand Down Expand Up @@ -118,12 +118,14 @@ export default function UEDetailsPage() {
: ue.info.requirements.toString()}
</div>
</div>
<ExamList
annals={annals}
setAnnalUploaderOpen={setAnnalUploaderOpen}
annalSemesters={annalSemesters}
annalTypes={annalTypes}
/>
{annals && annalSemesters && annalTypes && (
<ExamList
annals={annals}
setAnnalUploaderOpen={setAnnalUploaderOpen}
annalSemesters={annalSemesters}
annalTypes={annalTypes}
/>
)}
<div className={styles.thoughts}>
<h2>Avis des étudiants</h2>
<div className={styles.rates}>
Expand Down Expand Up @@ -168,13 +170,16 @@ export default function UEDetailsPage() {
</div>
</>
) : (
<ExamSender
ueCode={params.code}
addAnnal={addAnnal}
setAnnalUploaderOpen={setAnnalUploaderOpen}
annalSemesters={annalSemesters}
annalTypes={annalTypes}
/>
annalSemesters &&
annalTypes && (
<ExamSender
ueCode={params.code}
addAnnal={addAnnal}
setAnnalUploaderOpen={setAnnalUploaderOpen}
annalSemesters={annalSemesters}
annalTypes={annalTypes}
/>
)
)}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/homeWidgets/TodaysBirthdaysWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function TodaysBirthdaysWidget() {
title={t('homepage:todaysBirthdays.title')}
subtitle={t('homepage:todaysBirthdays.subtitle')}
className={styles.widget}>
{users !== null
{users
? users.length === 0
? t('homepage:todaysBirthdays.noBirthdays')
: users.map((user) => (
Expand Down
4 changes: 2 additions & 2 deletions src/components/ues/ExamList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default function ExamList({
}: {
annals: Annal[] | null;
setAnnalUploaderOpen: (opened: boolean) => void;
annalTypes: AnnalType[] | null;
annalSemesters: string[] | null;
annalTypes: AnnalType[];
annalSemesters: string[];
}) {
const type = useAppSelector((state) => state.user?.type);
const userId = useAppSelector((state) => state.user?.id);
Expand Down
4 changes: 2 additions & 2 deletions src/components/ues/ExamSender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default function ExamSender({
setAnnalUploaderOpen,
}: {
ueCode: string;
annalTypes: AnnalType[] | null;
annalSemesters: string[] | null;
annalTypes: AnnalType[];
annalSemesters: string[];
addAnnal: (annal: Annal) => void;
setAnnalUploaderOpen: (open: boolean) => void;
}) {
Expand Down
5 changes: 5 additions & 0 deletions src/module/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setUser } from '@/module/user';
import { API, setAuthorizationToken } from '@/api/api';
import { fetchProfile } from '@/api/profile/fetchProfile';
import { CookieNames, getCookie, setCookie } from '@/module/cookies';
import { useAppSelector } from '@/lib/hooks';

interface SessionSlice {
logged: boolean;
Expand Down Expand Up @@ -86,4 +87,8 @@ export function setToken(token: string | null, api?: API): AppThunk {
};
}

export function useLoggedIn() {
return useAppSelector((state) => state.session.logged);
}

export default sessionSlice.reducer;

0 comments on commit 7fdbd05

Please sign in to comment.