From 73511c72ca355428fae5033b581870464768a627 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Fri, 7 Jun 2024 18:17:39 -0300 Subject: [PATCH] Added stop signal to not let the request happen twice --- src/frontend/src/controllers/API/index.ts | 17 ++- .../hooks/use-get-profile-pictures.ts | 9 +- .../profilePictureChooserComponent/index.tsx | 45 ++----- .../components/ProfilePictureForm/index.tsx | 121 ++++++++++++------ .../SettingsPage/pages/GeneralPage/index.tsx | 4 + 5 files changed, 120 insertions(+), 76 deletions(-) diff --git a/src/frontend/src/controllers/API/index.ts b/src/frontend/src/controllers/API/index.ts index daed4adfb..8874f51a7 100644 --- a/src/frontend/src/controllers/API/index.ts +++ b/src/frontend/src/controllers/API/index.ts @@ -365,8 +365,21 @@ export async function uploadFile( return await api.post(`${BASE_URL_API}files/upload/${id}`, formData); } -export async function getProfilePictures(): Promise { - return api.get(`${BASE_URL_API}files/list/profile_pictures`); +export async function getProfilePictures( + abortSignal, +): Promise { + try { + const res = await api.get(`${BASE_URL_API}images/list/profile_pictures`, { + signal: abortSignal, + }); + + if (res.status === 200) { + return res.data; + } + } catch (error) { + throw error; + } + return null; } export async function postCustomComponent( diff --git a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/hooks/use-get-profile-pictures.ts b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/hooks/use-get-profile-pictures.ts index da87594c0..de3c7c744 100644 --- a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/hooks/use-get-profile-pictures.ts +++ b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/hooks/use-get-profile-pictures.ts @@ -3,11 +3,12 @@ import { PROFILE_PICTURES_GET_ERROR_ALERT } from "../../../../../../../../../con import { getProfilePictures } from "../../../../../../../../../controllers/API"; const useGetProfilePictures = (setErrorData) => { - const handleGetProfilePictures = async () => { + const handleGetProfilePictures = async (abortSignal) => { try { - const profilePictures = await getProfilePictures(); - return profilePictures.files; + const profilePictures = await getProfilePictures(abortSignal); + return profilePictures!.files; } catch (error) { + console.log(error); setErrorData({ title: PROFILE_PICTURES_GET_ERROR_ALERT, list: [(error as any)?.response?.data?.detail], @@ -16,7 +17,7 @@ const useGetProfilePictures = (setErrorData) => { } }; - return handleGetProfilePictures; + return { handleGetProfilePictures }; }; export default useGetProfilePictures; diff --git a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/index.tsx b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/index.tsx index e1f0ee209..a5a6b9c56 100644 --- a/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/index.tsx +++ b/src/frontend/src/pages/SettingsPage/pages/GeneralPage/components/ProfilePictureForm/components/profilePictureChooserComponent/index.tsx @@ -11,48 +11,31 @@ import HorizontalScrollFadeComponent from "../../../../../../../../components/ho import LoadingComponent from "../../../../../../../../components/loadingComponent"; import Loading from "../../../../../../../../components/ui/loading"; -export default function ProfilePictureChooserComponent({ value, onChange }) { - const setErrorData = useAlertStore((state) => state.setErrorData); - const getProfilePictures = useGetProfilePictures({ setErrorData }); - - const [profilePictures, setProfilePictures] = useState([]); - const [loading, setLoading] = useState(true); - - useEffect(() => { - getProfilePictures() - .then((data) => { - if (data) { - data.forEach((profile_picture) => { - const [folder, path] = profile_picture.split("/"); - setProfilePictures((prev) => { - if (prev[folder]) { - prev[folder].push(path); - } else { - prev[folder] = [path]; - } - return prev; - }); - setLoading(false); - }); - } - }) - .catch(() => { - setLoading(false); - }); - }); +type ProfilePictureChooserComponentProps = { + profilePictures: { [key: string]: string[] }; + loading: boolean; + value: string; + onChange: (value: string) => void; +}; +export default function ProfilePictureChooserComponent({ + profilePictures, + loading, + value, + onChange, +}: ProfilePictureChooserComponentProps) { return (
{loading ? ( ) : ( - profilePictures.map((folder, idx) => ( + Object.keys(profilePictures).map((folder, idx) => (