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