2021-05-31 16:46:41 +02:00
|
|
|
import { currentBackend } from '../backend';
|
2022-09-28 20:04:00 -06:00
|
|
|
import { addSnackbar, removeSnackbarById } from '../store/slices/snackbars';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
2021-05-31 14:23:16 +02:00
|
|
|
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,
|
2020-12-15 12:38:59 +02:00
|
|
|
} as const;
|
2020-06-03 12:44:03 +03:00
|
|
|
}
|
|
|
|
|
2020-06-15 10:59:28 -04: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 },
|
2020-12-15 12:38:59 +02:00
|
|
|
} as const;
|
2020-06-03 12:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function statusFailure(error: Error) {
|
|
|
|
return {
|
|
|
|
type: STATUS_FAILURE,
|
2020-06-15 10:59:28 -04:00
|
|
|
payload: { error },
|
2020-12-15 12:38:59 +02:00
|
|
|
} 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();
|
|
|
|
|
2020-06-15 10:59:28 -04:00
|
|
|
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
|
|
|
);
|
2020-06-15 10:59:28 -04: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 },
|
2020-06-15 10:59:28 -04:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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));
|
2020-06-15 10:59:28 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2020-12-15 12:38:59 +02:00
|
|
|
|
|
|
|
export type StatusAction = ReturnType<
|
|
|
|
typeof statusRequest | typeof statusSuccess | typeof statusFailure
|
|
|
|
>;
|