diff --git a/src/components/ActiveCarpools/ActiveCarpools.tsx b/src/components/ActiveCarpools/ActiveCarpools.tsx index e4f26ae..db30079 100644 --- a/src/components/ActiveCarpools/ActiveCarpools.tsx +++ b/src/components/ActiveCarpools/ActiveCarpools.tsx @@ -17,7 +17,7 @@ export default function ActiveCarpools() { const [activeCarpools, setActiveCarpools] = useState([]); useEffect(() => { - getActiveCarpools().then(setActiveCarpools); + getActiveCarpools().then(setActiveCarpools).catch(console.error); // TODO error handling }, []); return ( @@ -30,9 +30,11 @@ export default function ActiveCarpools() { >

Carpools

- {activeCarpools.length > 0 ? activeCarpools.map((carpool) => ( - - )) : "No Carpools"} + {activeCarpools.length > 0 + ? activeCarpools.map((carpool) => ( + + )) + : 'No Carpools'} ); diff --git a/src/components/ActiveEvents/Events.tsx b/src/components/ActiveEvents/Events.tsx index c279f89..35069bc 100644 --- a/src/components/ActiveEvents/Events.tsx +++ b/src/components/ActiveEvents/Events.tsx @@ -11,7 +11,7 @@ export default function ActiveEvents() { useEffect(() => { if (!hasEvents) { - getActiveEvents().then(setEvents); + getActiveEvents().then(setEvents).catch(console.error); // TODO error handling } }, [hasEvents, setEvents]); diff --git a/src/components/Authentication/AuthenticationWrapper.tsx b/src/components/Authentication/AuthenticationWrapper.tsx index a550550..44d886c 100644 --- a/src/components/Authentication/AuthenticationWrapper.tsx +++ b/src/components/Authentication/AuthenticationWrapper.tsx @@ -15,15 +15,19 @@ export default function AuthenticationWrapper({ const sessionToken = localStorage.getItem('session_token'); if (sessionToken) { - const user = await getMe(); - if (!('status' in user && user.status === 'error')) { - setUser(user); - return; + try { + setUser(await getMe()); + } catch (e) { + console.error(e); + setUser(null); } + } else { + setUser(null); } - setUser(null); } - refresh_().finally(() => setLoaded(true)); + refresh_() + .catch(console.error) // TODO error handling + .finally(() => setLoaded(true)); }, []); useEffect(refresh, [refresh]); diff --git a/src/components/Authentication/Authenticator.tsx b/src/components/Authentication/Authenticator.tsx index 677b9c4..ddebc1a 100644 --- a/src/components/Authentication/Authenticator.tsx +++ b/src/components/Authentication/Authenticator.tsx @@ -33,6 +33,7 @@ function useToken( .then(({ token }) => { setToken(token ?? null); }) + .catch(console.error) // TODO error handling .finally(() => setPending(false)); } }, [code, provider]); diff --git a/src/components/Carpool/Carpool.tsx b/src/components/Carpool/Carpool.tsx index ff9ee22..2cc279c 100644 --- a/src/components/Carpool/Carpool.tsx +++ b/src/components/Carpool/Carpool.tsx @@ -58,20 +58,22 @@ export default function Carpool({ id }: { id: number }) { ); useEffect(() => { - getCarpool(id).then((carpool) => { - const invitationsMap: Record = {}; - carpool.invitations.forEach((invite) => { - invitationsMap[invite.user.id] = invite; - }); - setCarpool({ - id: carpool.id, - name: carpool.name, - event: carpool.event, - members: carpool.members, - creatorId: carpool.creatorId, - invitations: invitationsMap, - }); - }); + getCarpool(id) + .then((carpool) => { + const invitationsMap: Record = {}; + carpool.invitations.forEach((invite) => { + invitationsMap[invite.user.id] = invite; + }); + setCarpool({ + id: carpool.id, + name: carpool.name, + event: carpool.event, + members: carpool.members, + creatorId: carpool.creatorId, + invitations: invitationsMap, + }); + }) + .catch(console.error); // TODO error handling }, [id, setCarpool]); const acceptRequest = useCallback( diff --git a/src/components/Carpool/CarpoolTopButtonsNonMembersOnly.tsx b/src/components/Carpool/CarpoolTopButtonsNonMembersOnly.tsx index d828e33..71a8f77 100644 --- a/src/components/Carpool/CarpoolTopButtonsNonMembersOnly.tsx +++ b/src/components/Carpool/CarpoolTopButtonsNonMembersOnly.tsx @@ -27,7 +27,7 @@ export default function CarpoolTopButtonsNonMembersOnly() { const me = useMe() || defaultMe; const sendRequest = useCallback(() => { - sendCarpoolRequest(carpool.id); + sendCarpoolRequest(carpool.id); // TODO carpool request error handling }, [carpool.id, sendCarpoolRequest]); const cancelRequest = useCallback(() => { @@ -35,15 +35,19 @@ export default function CarpoolTopButtonsNonMembersOnly() { }, [carpool.id, cancelCarpoolRequest]); const acceptInvitation = useCallback(() => { - acceptCarpoolInvite(carpool.id).then(() => { - members.push(me); - }); + acceptCarpoolInvite(carpool.id) + .then(() => { + members.push(me); + }) + .catch(console.error); // TODO error handling }, [acceptCarpoolInvite, carpool.id, members, me]); const denyInvitation = useCallback(() => { - denyCarpoolInvite(carpool.id).then(() => { - members.push(me); - }); + denyCarpoolInvite(carpool.id) + .then(() => { + members.push(me); + }) + .catch(console.error); // TODO error handling }, [carpool.id, denyCarpoolInvite, me, members]); const invitationState = useInvitationState(carpool.id); diff --git a/src/components/Carpool/InvitationList.tsx b/src/components/Carpool/InvitationList.tsx index 103d278..03a197c 100644 --- a/src/components/Carpool/InvitationList.tsx +++ b/src/components/Carpool/InvitationList.tsx @@ -46,7 +46,9 @@ export default function InvitationList() { useImmutable(null); useEffect(() => { - getPotentialInvitees(carpool.id).then(setAvailableSignups); + getPotentialInvitees(carpool.id) + .then(setAvailableSignups) + .catch(console.error); // TODO error handling }, [carpool.id, setAvailableSignups]); const invitedUserIDs = useMemo( diff --git a/src/components/Carpool/useSignups.tsx b/src/components/Carpool/useSignups.tsx index 925e7ef..a75d223 100644 --- a/src/components/Carpool/useSignups.tsx +++ b/src/components/Carpool/useSignups.tsx @@ -8,9 +8,7 @@ export default function useSignups(eventId: number, userIds: number[]) { const [signups, setSignups] = useState([]); useEffect(() => { - getEventSignupsBulk(eventId, userIds).then((signups) => { - setSignups(signups); - }); + getEventSignupsBulk(eventId, userIds).then(setSignups).catch(console.error); // TODO error handling }, [eventId, userIds]); return signups; diff --git a/src/components/Event/Event.tsx b/src/components/Event/Event.tsx index 8d9cfd2..1f8311b 100644 --- a/src/components/Event/Event.tsx +++ b/src/components/Event/Event.tsx @@ -21,6 +21,7 @@ export default function Event({ setLoading(true); getEvent(id) .then((e) => e && setEvent(e)) + .catch(console.error) // TODO error handling .finally(() => setLoading(false)); }, [id, setEvent]); diff --git a/src/components/EventCreator/EventCreator.tsx b/src/components/EventCreator/EventCreator.tsx index 5a0be7e..48f7a87 100644 --- a/src/components/EventCreator/EventCreator.tsx +++ b/src/components/EventCreator/EventCreator.tsx @@ -70,6 +70,7 @@ export default function EventCreator({ group }: { group: IGroup }) { .then(({ id }) => { setCreatedEventId(id); }) + .catch(console.error) // TODO error handling .finally(() => setCreating(false)); } }, [ diff --git a/src/components/Group/Group.tsx b/src/components/Group/Group.tsx index 4ece291..7496ab9 100644 --- a/src/components/Group/Group.tsx +++ b/src/components/Group/Group.tsx @@ -30,14 +30,8 @@ export default function Group({ id }: { id: number }) { useEffect(() => { setLoading(true); getGroup(id) - .then((group) => { - // @ts-ignore - if ('status' in group && group.status === 'error') { - setGroup(null); - } else { - setGroup(group); - } - }) + .then(setGroup) + .catch(console.error) // TODO error handling .finally(() => setLoading(false)); }, [id, setGroup]); diff --git a/src/components/Group/GroupInviteCode.tsx b/src/components/Group/GroupInviteCode.tsx index 250f9f9..f425bc0 100644 --- a/src/components/Group/GroupInviteCode.tsx +++ b/src/components/Group/GroupInviteCode.tsx @@ -19,15 +19,19 @@ export default function GroupInviteCode() { const [shown, setShown] = useState(false); const generateJoinCode = useCallback(() => { - generateCode(group.id).then((code) => { - group.joinCode = code; - }); + generateCode(group.id) + .then((code) => { + group.joinCode = code; + }) + .catch(console.error); // TODO error handling }, [group]); const resetJoinCode = useCallback(() => { - resetCode(group.id).then((code) => { - group.joinCode = null; - }); + resetCode(group.id) + .then((code) => { + group.joinCode = null; + }) + .catch(console.error); // TODO error handling }, [group]); if (group.joinCode) { diff --git a/src/components/Group/GroupMembersLink.tsx b/src/components/Group/GroupMembersLink.tsx index bf5ed4d..eca4dec 100644 --- a/src/components/Group/GroupMembersLink.tsx +++ b/src/components/Group/GroupMembersLink.tsx @@ -22,11 +22,13 @@ export default function GroupMembersLink() { const addAdmin = useCallback( (adminId: number, adminName: string) => { - addGroupAdmin(group.id, adminId).then(({ status }) => { - if (status === 'success') { - group.admins.push({ id: adminId, name: adminName }); - } - }); + addGroupAdmin(group.id, adminId) + .then(({ status }) => { + if (status === 'success') { + group.admins.push({ id: adminId, name: adminName }); + } + }) + .catch(console.error); // TODO error handling }, [group.admins, group.id] ); @@ -35,11 +37,13 @@ export default function GroupMembersLink() { const removeAdmin = useCallback( (adminId: number) => { - removeGroupAdmin(group.id, adminId).then((res) => { - if (res.status === 'success') { - group.admins = group.admins.filter((admin) => admin.id !== adminId); - } - }); + removeGroupAdmin(group.id, adminId) + .then((res) => { + if (res.status === 'success') { + group.admins = group.admins.filter((admin) => admin.id !== adminId); + } + }) + .catch(console.error); // TODO error handling }, [group] ); diff --git a/src/components/Group/GroupSettings.tsx b/src/components/Group/GroupSettings.tsx index e230333..081a8ab 100644 --- a/src/components/Group/GroupSettings.tsx +++ b/src/components/Group/GroupSettings.tsx @@ -16,9 +16,7 @@ export default function GroupSettings({ group }: { group: IGroup }) { .then(({ status }) => { setDeletionSuccessful(status === 'success'); }) - .catch(() => { - setDeletionSuccessful(false); - }); + .catch(() => setDeletionSuccessful(false)); }, [group.id]); const confirmDeletion = useCallback(() => { diff --git a/src/components/GroupCreator/GroupCreator.tsx b/src/components/GroupCreator/GroupCreator.tsx index b75026e..47f7eea 100644 --- a/src/components/GroupCreator/GroupCreator.tsx +++ b/src/components/GroupCreator/GroupCreator.tsx @@ -19,6 +19,7 @@ export default function GroupCreator() { setCreationSuccessful(true); setCreatedGroupId(id); }) + .catch(console.error) // TODO error handling .finally(() => { setCreating(false); }); diff --git a/src/components/GroupJoinerLink.tsx b/src/components/GroupJoinerLink.tsx index 9553037..3eb0a64 100644 --- a/src/components/GroupJoinerLink.tsx +++ b/src/components/GroupJoinerLink.tsx @@ -19,11 +19,13 @@ export function GroupJoiner() { useEffect(() => { if (code) { const initialCode = code; - resolveCode(code).then((group) => { - if (code === initialCode) { - setGroup(group); - } - }); + resolveCode(code) + .then((group) => { + if (code === initialCode) { + setGroup(group); + } + }) + .catch(console.error); } }, [code]); @@ -38,6 +40,7 @@ export function GroupJoiner() { window.location.href = '/groups/' + id; } }) + .catch(console.error) .finally(() => setJoining(false)); } }, [code, group?.id]); diff --git a/src/components/GroupSharedLinkResolver.tsx b/src/components/GroupSharedLinkResolver.tsx index d60c4ba..47f996f 100644 --- a/src/components/GroupSharedLinkResolver.tsx +++ b/src/components/GroupSharedLinkResolver.tsx @@ -15,6 +15,7 @@ export default function GroupSharedLinkResolver() { setResolving(true); resolveCode(code) .then(setGroup) + .catch(console.error) .finally(() => setResolving(false)); }, [code]); @@ -29,6 +30,7 @@ export default function GroupSharedLinkResolver() { window.location.href = '/groups/' + id; } }) + .catch(console.error) .finally(() => setJoining(false)); } }, [code, group?.id]); diff --git a/src/components/Groups/Groups.tsx b/src/components/Groups/Groups.tsx index 4325511..8a6e21a 100644 --- a/src/components/Groups/Groups.tsx +++ b/src/components/Groups/Groups.tsx @@ -9,7 +9,7 @@ export default function Groups() { const [groups, setGroups] = useState([]); useEffect(() => { - getGroups().then(setGroups); + getGroups().then(setGroups).catch(console.error); // TODO error handling }, []); return ( diff --git a/src/components/UI/UIPlacesAutocomplete.tsx b/src/components/UI/UIPlacesAutocomplete.tsx index 4be8dde..6f9d62a 100644 --- a/src/components/UI/UIPlacesAutocomplete.tsx +++ b/src/components/UI/UIPlacesAutocomplete.tsx @@ -60,13 +60,15 @@ export default function UIPlacesAutocomplete({ useEffect(() => { if (placeId) { - getPlaceDetails(placeId).then((result) => { - if (result.formattedAddress.startsWith(result.name)) { - setLocation(result.formattedAddress); - } else { - setLocation(`${result.name}, ${result.formattedAddress}`); - } - }); + getPlaceDetails(placeId) + .then((result) => { + if (result.formattedAddress.startsWith(result.name)) { + setLocation(result.formattedAddress); + } else { + setLocation(`${result.name}, ${result.formattedAddress}`); + } + }) + .catch(console.error); // TODO error handling } }, [placeId]); diff --git a/src/components/api.ts b/src/components/api.ts index 4d5d481..80a59ee 100644 --- a/src/components/api.ts +++ b/src/components/api.ts @@ -23,6 +23,9 @@ async function post(path: string, data: any) { 'Content-Type': 'application/json', }, }); + if (res.status !== 200) { + throw new Error(`${path}: ${await res.text()}`); + } return await res.json(); } @@ -35,6 +38,9 @@ async function delete$(path: string, body?: any) { }, body: body ? JSON.stringify(body) : undefined, }); + if (res.status !== 200) { + throw new Error(`${path}: ${await res.text()}`); + } return await res.json(); } @@ -45,12 +51,10 @@ async function get(path: string) { }, }); const result = await res.json(); + if (res.status !== 200) { + throw new Error(`${path}: ${await res.text()}`); + } return result; - // if (res.ok) { - // return result; - // } else { - // throw new Error(result.message); - // } } export async function getEventSignupsBulk( diff --git a/src/components/hooks.ts b/src/components/hooks.ts index d4ba820..47768a9 100644 --- a/src/components/hooks.ts +++ b/src/components/hooks.ts @@ -10,7 +10,9 @@ export function useNotifications() { const [notifications, setNotifications] = useState([]); const refresh = useCallback(() => { - getReceivedInvitationsAndRequests().then(setNotifications); + getReceivedInvitationsAndRequests() + .then(setNotifications) + .catch(console.error); // TODO error handling }, []); useEffect(refresh, [refresh]); diff --git a/src/components/usePlace.ts b/src/components/usePlace.ts index dfa5f28..2b857e7 100644 --- a/src/components/usePlace.ts +++ b/src/components/usePlace.ts @@ -9,7 +9,7 @@ export default function usePlace(placeId: string | null) { if (placeId == null) { setPlaceDetails(null); } else { - getPlaceDetails(placeId).then(setPlaceDetails); + getPlaceDetails(placeId).then(setPlaceDetails).catch(console.error); // TODO error handling } }, [placeId]); diff --git a/src/state/Notifications/NotificationsProvider.tsx b/src/state/Notifications/NotificationsProvider.tsx index 7afe2b0..a8eb526 100644 --- a/src/state/Notifications/NotificationsProvider.tsx +++ b/src/state/Notifications/NotificationsProvider.tsx @@ -33,20 +33,23 @@ export default function NotificationsProvider({ >({}); useEffect(() => { - api.getSentRequestsAndInvites().then((invitations) => { - const invited = {} as Record; - const requested = {} as Record; - for (let invitation of invitations) { - if (invitation.isRequest) { - invited[invitation.carpool.id] = true; - } else { - requested[invitation.carpool.id] = true; + api + .getSentRequestsAndInvites() + .then((invitations) => { + const invited = {} as Record; + const requested = {} as Record; + for (let invitation of invitations) { + if (invitation.isRequest) { + invited[invitation.carpool.id] = true; + } else { + requested[invitation.carpool.id] = true; + } } - } - setInvitedCarpoolIds(invited); - setRequestedCarpoolIds(requested); - }); + setInvitedCarpoolIds(invited); + setRequestedCarpoolIds(requested); + }) + .catch(console.error); // TODO error handling }, [setInvitedCarpoolIds, setRequestedCarpoolIds]); const sendCarpoolRequest = useCallback(