From 2dd2cfa6138b095328964cf02a725c1c24c669e8 Mon Sep 17 00:00:00 2001 From: gempir Date: Sun, 8 Nov 2020 13:11:30 +0100 Subject: [PATCH] error handlign --- web/src/components/Filters.tsx | 2 +- web/src/hooks/useAvailableLogs.ts | 11 ++++++++--- web/src/store.tsx | 4 +++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/web/src/components/Filters.tsx b/web/src/components/Filters.tsx index ffd1c06..2329466 100644 --- a/web/src/components/Filters.tsx +++ b/web/src/components/Filters.tsx @@ -42,7 +42,7 @@ export function Filters() { return - + diff --git a/web/src/hooks/useAvailableLogs.ts b/web/src/hooks/useAvailableLogs.ts index 8099eb1..31ee347 100644 --- a/web/src/hooks/useAvailableLogs.ts +++ b/web/src/hooks/useAvailableLogs.ts @@ -6,13 +6,13 @@ import { store } from "../store"; export type AvailableLogs = Array<{ month: string, year: string }>; export function useAvailableLogs(channel: string | null, username: string | null): AvailableLogs { - const { state } = useContext(store); + const { state, setState } = useContext(store); const { data } = useQuery(`${channel}:${username}`, () => { if (channel && username) { const channelIsId = isUserId(channel); const usernameIsId = isUserId(username); - + const queryUrl = new URL(`${state.apiBaseUrl}/list`); queryUrl.searchParams.append(`channel${channelIsId ? "id" : ""}`, channel); queryUrl.searchParams.append(`user${usernameIsId ? "id" : ""}`, username); @@ -24,7 +24,12 @@ export function useAvailableLogs(channel: string | null, username: string | null throw Error(response.statusText); }).then(response => response.json()) - .then((data: { availableLogs: AvailableLogs }) => data.availableLogs); + .then((data: { availableLogs: AvailableLogs }) => data.availableLogs) + .catch(() => { + setState({ ...state, error: true }); + + return []; + }); } return []; diff --git a/web/src/store.tsx b/web/src/store.tsx index 0bf0e42..1c45a27 100644 --- a/web/src/store.tsx +++ b/web/src/store.tsx @@ -18,6 +18,7 @@ export interface State { apiBaseUrl: string, currentChannel: string | null, currentUsername: string | null, + error: boolean, } export type Action = Record; @@ -39,6 +40,7 @@ const defaultContext = { }, currentChannel: url.searchParams.get("channel"), currentUsername: url.searchParams.get("username"), + error: false, } as State, setState: (state: State) => { }, setCurrents: (currentChannel: string | null = null, currentUsername: string | null = null) => { }, @@ -59,7 +61,7 @@ const StateProvider = ({ children }: { children: JSX.Element }): JSX.Element => } const setCurrents = (currentChannel: string | null = null, currentUsername: string | null = null) => { - setState({ ...state, currentChannel, currentUsername }); + setState({ ...state, currentChannel, currentUsername, error: false }); const url = new URL(window.location.href); if (currentChannel) {