99 lines
2.8 KiB
TypeScript
Raw Normal View History

import { currentBackend } from '../backend';
2022-09-28 20:04:00 -06:00
import { addSnackbar, removeSnackbarById } from '../store/slices/snackbars';
import type { AnyAction } from 'redux';
2022-09-28 20:04:00 -06:00
import type { ThunkDispatch } from 'redux-thunk';
2022-10-20 11:57:30 -04:00
import type { RootState } from '../store';
2020-06-03 12:44:03 +03:00
export const STATUS_REQUEST = 'STATUS_REQUEST';
export const STATUS_SUCCESS = 'STATUS_SUCCESS';
export const STATUS_FAILURE = 'STATUS_FAILURE';
export function statusRequest() {
return {
type: STATUS_REQUEST,
} as const;
2020-06-03 12:44:03 +03:00
}
export function statusSuccess(status: {
auth: { status: boolean };
api: { status: boolean; statusPage: string };
}) {
2020-06-03 12:44:03 +03:00
return {
type: STATUS_SUCCESS,
payload: { status },
} as const;
2020-06-03 12:44:03 +03:00
}
export function statusFailure(error: Error) {
return {
type: STATUS_FAILURE,
payload: { error },
} as const;
2020-06-03 12:44:03 +03:00
}
export function checkBackendStatus() {
2022-10-20 11:57:30 -04:00
return async (dispatch: ThunkDispatch<RootState, {}, AnyAction>, getState: () => RootState) => {
2020-06-03 12:44:03 +03:00
try {
const state = getState();
2022-10-20 11:57:30 -04:00
const config = state.config.config;
if (state.status.isFetching || !config) {
2020-06-03 12:44:03 +03:00
return;
}
dispatch(statusRequest());
2022-10-20 11:57:30 -04:00
const backend = currentBackend(config);
2020-06-03 12:44:03 +03:00
const status = await backend.status();
const backendDownKey = 'ui.toast.onBackendDown';
2022-09-28 20:04:00 -06:00
const previousBackendDownNotifs = state.snackbar.messages.filter(
2022-10-20 11:57:30 -04:00
n => typeof n.message !== 'string' && n.message.key === backendDownKey,
2022-09-28 20:04:00 -06:00
);
if (status.api.status === false) {
if (previousBackendDownNotifs.length === 0) {
dispatch(
2022-09-28 20:04:00 -06:00
addSnackbar({
type: 'error',
message: { key: 'ui.toast.onBackendDown', details: status.api.statusPage },
}),
);
}
return dispatch(statusSuccess(status));
} else if (status.api.status === true && previousBackendDownNotifs.length > 0) {
// If backend is up, clear all the danger messages
previousBackendDownNotifs.forEach(notif => {
2022-09-28 20:04:00 -06:00
dispatch(removeSnackbarById(notif.id));
});
}
const authError = status.auth.status === false;
2020-06-03 12:44:03 +03:00
if (authError) {
const key = 'ui.toast.onLoggedOut';
2022-10-20 11:57:30 -04:00
const existingNotification = state.snackbar.messages.find(
n => typeof n.message !== 'string' && n.message.key === key,
);
2020-06-03 12:44:03 +03:00
if (!existingNotification) {
dispatch(
2022-09-28 20:04:00 -06:00
addSnackbar({
type: 'error',
message: { key: 'ui.toast.onLoggedOut' },
2020-06-03 12:44:03 +03:00
}),
);
}
}
dispatch(statusSuccess(status));
2022-10-20 11:57:30 -04:00
} catch (error: unknown) {
console.error(error);
if (error instanceof Error) {
dispatch(statusFailure(error));
}
2020-06-03 12:44:03 +03:00
}
};
}
export type StatusAction = ReturnType<
typeof statusRequest | typeof statusSuccess | typeof statusFailure
>;