type actionhelpers

This commit is contained in:
Seve Badajoz
2021-08-12 16:35:27 -07:00
parent 848b156828
commit eb2edb35d0
+29 -25
View File
@@ -1,15 +1,15 @@
import sortBy from "lodash.sortby";
import { Dispatch } from "redux";
/* XXX: cough, cough, ... */
import { postNetworkErrorToast } from "../components/framework/toasters";
import type { RootState } from "../reducers";
/*
dispatch an action error to the user. Currently we use
async toasts.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
let networkErrorToastKey: any = null;
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export const dispatchNetworkErrorMessageToUser = (message: any) => {
let networkErrorToastKey: string | null = null;
export const dispatchNetworkErrorMessageToUser = (message: string): void => {
if (!networkErrorToastKey) {
networkErrorToastKey = postNetworkErrorToast(message);
} else {
@@ -20,12 +20,12 @@ export const dispatchNetworkErrorMessageToUser = (message: any) => {
/*
Catch unexpected errors and make sure we don't lose them!
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function catchErrorsWrap(fn: any, dispatchToUser = false) {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
return (dispatch: any, getState: any) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
fn(dispatch, getState).catch((error: any) => {
export function catchErrorsWrap(
fn: (dispatch: Dispatch, getState: () => RootState) => Promise<void>,
dispatchToUser = false
) {
return (dispatch: Dispatch, getState: () => RootState): void => {
fn(dispatch, getState).catch((error: Error) => {
console.error(error);
if (dispatchToUser) {
dispatchNetworkErrorMessageToUser(error.message);
@@ -39,8 +39,10 @@ export function catchErrorsWrap(fn: any, dispatchToUser = false) {
* Wrapper to perform async fetch with some modest error handling
* and decoding. Arguments are identical to standard fetch.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export const doFetch = async (url: any, init = {}) => {
export const doFetch = async (
url: string,
init?: RequestInit
): Promise<Response> => {
try {
// add defaults to the fetch init param.
init = {
@@ -48,13 +50,13 @@ export const doFetch = async (url: any, init = {}) => {
credentials: "include",
...init,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const acceptType = (init as any).headers?.get("Accept");
const acceptType = (init.headers as Headers)?.get("Accept");
const res = await fetch(url, init);
if (
res.ok &&
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
(!acceptType || res.headers.get("Content-Type").includes(acceptType))
(res.ok &&
(!acceptType ||
res.headers?.get("Content-Type")?.includes(acceptType))) ??
false
) {
return res;
}
@@ -74,8 +76,10 @@ export const doFetch = async (url: any, init = {}) => {
/*
Wrapper to perform an async fetch and JSON decode response.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export const doJsonRequest = async (url: any, init = {}) => {
export const doJsonRequest = async (
url: string,
init?: RequestInit
): Promise<unknown> => {
const res = await doFetch(url, {
...init,
headers: new Headers({ Accept: "application/json" }),
@@ -86,8 +90,10 @@ export const doJsonRequest = async (url: any, init = {}) => {
/*
Wrapper to perform an async fetch for binary data.
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export const doBinaryRequest = async (url: any, init = {}) => {
export const doBinaryRequest = async (
url: string,
init?: RequestInit
): Promise<ArrayBuffer> => {
const res = await doFetch(url, {
...init,
headers: new Headers({ Accept: "application/octet-stream" }),
@@ -110,13 +116,11 @@ Parameters:
So [1, 2, 3, 4, 10, 11, 14] -> [ [1, 4], [10, 11], 14]
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export const rangeEncodeIndices = (
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
indices: any,
indices: Array<number>,
minRangeLength = 3,
sorted = false
) => {
): Array<number | Array<number>> => {
if (indices.length === 0) {
return indices;
}