error handlign

This commit is contained in:
gempir 2020-11-08 13:11:30 +01:00
parent e8cf7fb1ad
commit 2dd2cfa613
3 changed files with 12 additions and 5 deletions

View file

@ -42,7 +42,7 @@ export function Filters() {
return <FiltersWrapper>
<FiltersContainer onSubmit={handleSubmit} action="none">
<TextField name="channel" label="channel" variant="filled" autoComplete="off" defaultValue={state.currentChannel} autoFocus={state.currentChannel === null} />
<TextField name="username" label="username" variant="filled" autoComplete="off" defaultValue={state.currentUsername} autoFocus={state.currentChannel !== null && state.currentUsername === null} />
<TextField error={state.error} name="username" label="username" variant="filled" autoComplete="off" defaultValue={state.currentUsername} autoFocus={state.currentChannel !== null && state.currentUsername === null} />
<Button variant="contained" color="primary" size="large" type="submit">load</Button>
<Settings />
</FiltersContainer>

View file

@ -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<AvailableLogs>(`${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 [];

View file

@ -18,6 +18,7 @@ export interface State {
apiBaseUrl: string,
currentChannel: string | null,
currentUsername: string | null,
error: boolean,
}
export type Action = Record<string, unknown>;
@ -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) {