Convert Redux custom middleware to TypeScript

This commit is contained in:
Alex Gleason
2022-04-24 14:40:14 -05:00
parent 7038d6a844
commit 57e5d81e33
3 changed files with 45 additions and 25 deletions

View File

@ -1,20 +0,0 @@
import { showAlertForError } from '../actions/alerts';
const isFailType = type => type.endsWith('_FAIL');
const isRememberFailType = type => type.endsWith('_REMEMBER_FAIL');
const hasResponse = error => Boolean(error && error.response);
const shouldShowError = ({ type, skipAlert, error }) => {
return !skipAlert && hasResponse(error) && isFailType(type) && !isRememberFailType(type);
};
export default function errorsMiddleware() {
return ({ dispatch }) => next => action => {
if (shouldShowError(action)) {
dispatch(showAlertForError(action.error));
}
return next(action);
};
}

View File

@ -0,0 +1,29 @@
import { showAlertForError } from '../actions/alerts';
import type { AnyAction } from 'redux';
import type { ThunkMiddleware } from 'redux-thunk';
/** Whether the action is considered a failure. */
const isFailType = (type: string): boolean => type.endsWith('_FAIL');
/** Whether the action is a failure to fetch from browser storage. */
const isRememberFailType = (type: string): boolean => type.endsWith('_REMEMBER_FAIL');
/** Whether the error contains an Axios response. */
const hasResponse = (error: any): boolean => Boolean(error && error.response);
/** Whether the error should be shown to the user. */
const shouldShowError = ({ type, skipAlert, error }: AnyAction): boolean => {
return !skipAlert && hasResponse(error) && isFailType(type) && !isRememberFailType(type);
};
/** Middleware to display Redux errors to the user. */
export default function errorsMiddleware(): ThunkMiddleware {
return ({ dispatch }) => next => action => {
if (shouldShowError(action)) {
dispatch(showAlertForError(action.error));
}
return next(action);
};
}

View File

@ -1,6 +1,15 @@
'use strict';
const createAudio = sources => {
import type { ThunkMiddleware } from 'redux-thunk';
/** Soapbox audio clip. */
type Sound = {
src: string,
type: string,
}
/** Produce HTML5 audio from sound data. */
const createAudio = (sources: Sound[]): HTMLAudioElement => {
const audio = new Audio();
sources.forEach(({ type, src }) => {
const source = document.createElement('source');
@ -11,7 +20,8 @@ const createAudio = sources => {
return audio;
};
const play = audio => {
/** Play HTML5 sound. */
const play = (audio: HTMLAudioElement): void => {
if (!audio.paused) {
audio.pause();
if (typeof audio.fastSeek === 'function') {
@ -24,8 +34,9 @@ const play = audio => {
audio.play();
};
export default function soundsMiddleware() {
const soundCache = {
/** Middleware to play sounds in response to certain Redux actions. */
export default function soundsMiddleware(): ThunkMiddleware {
const soundCache: Record<string, HTMLAudioElement> = {
boop: createAudio([
{
src: require('../../sounds/boop.ogg'),
@ -49,7 +60,7 @@ export default function soundsMiddleware() {
};
return () => next => action => {
if (action.meta && action.meta.sound && soundCache[action.meta.sound]) {
if (action.meta?.sound && soundCache[action.meta.sound]) {
play(soundCache[action.meta.sound]);
}