2022-11-22 17:22:26 +01:00

273 lines
7.8 KiB
TypeScript

import {
Action,
ActionCreatorWithoutPayload,
ActionCreatorWithPayload,
ThunkDispatch,
} from "@reduxjs/toolkit"
import { find } from "lodash"
import { toast } from "react-toastify"
import { AppState, AppThunk } from "."
export interface StateRequest {
readyStatus: "idle" | "request" | "success" | "failure"
error?: string
}
export function toastError(message: string, autoClose: number | false = 6000): void {
toast.error(message, {
position: "top-center",
...(autoClose ? { autoClose } : {}),
hideProgressBar: true,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
})
}
export function toastSuccess(message: string, autoClose: number | false = 5000): void {
toast.success(message, {
position: "top-center",
...(autoClose ? { autoClose } : {}),
hideProgressBar: true,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
})
}
export function elementFetch<Element, ServiceInput extends Array<any>>(
elementService: (...idArgs: ServiceInput) => Promise<{
data?: Element | undefined
error?: Error | undefined
}>,
getRequesting: ActionCreatorWithoutPayload<string>,
getSuccess: ActionCreatorWithPayload<Element, string>,
getFailure: ActionCreatorWithPayload<string, string>,
errorMessage?: (error: Error) => void,
successMessage?: (data: Element, dispatch: ThunkDispatch<AppState, any, Action>) => void
): (...idArgs: ServiceInput) => AppThunk {
return (...idArgs: ServiceInput): AppThunk =>
async (dispatch: ThunkDispatch<AppState, any, Action>) => {
dispatch(getRequesting())
const { error, data } = await elementService(...idArgs)
if (error) {
dispatch(getFailure(error.message))
errorMessage?.(error)
} else {
dispatch(getSuccess(data as Element))
successMessage?.(data as Element, dispatch)
}
}
}
export function elementAddFetch<Element, ServiceInput extends Array<any>>(
elementAddService: (...idArgs: ServiceInput) => Promise<{
data?: Element | undefined
error?: Error | undefined
}>,
getRequesting: ActionCreatorWithoutPayload<string>,
getSuccess: ActionCreatorWithPayload<Element, string>,
getFailure: ActionCreatorWithPayload<string, string>,
errorMessage?: (error: Error) => void,
successMessage?: () => void
): (...idArgs: ServiceInput) => AppThunk {
return (...idArgs: ServiceInput): AppThunk =>
async (dispatch) => {
dispatch(getRequesting())
const { error, data } = await elementAddService(...idArgs)
if (error) {
dispatch(getFailure(error.message))
errorMessage?.(error)
} else {
dispatch(getSuccess(data as Element))
successMessage?.()
}
}
}
export function elementListFetch<Element, ServiceInput extends Array<any>>(
elementListService: (...idArgs: ServiceInput) => Promise<{
data?: Element[] | undefined
error?: Error | undefined
}>,
getRequesting: ActionCreatorWithoutPayload<string>,
getSuccess: ActionCreatorWithPayload<Element[], string>,
getFailure: ActionCreatorWithPayload<string, string>,
errorMessage?: (error: Error) => void,
successMessage?: () => void
): (...idArgs: ServiceInput) => AppThunk {
return (...idArgs: ServiceInput): AppThunk =>
async (dispatch) => {
dispatch(getRequesting())
const { error, data } = await elementListService(...idArgs)
if (error) {
dispatch(getFailure(error.message))
errorMessage?.(error)
} else {
dispatch(getSuccess(data as Element[]))
successMessage?.()
}
}
}
export function elementValueFetch<Element>(
elementListService: () => Promise<{
data?: Element | undefined
error?: Error | undefined
}>,
getRequesting: ActionCreatorWithoutPayload<string>,
getSuccess: ActionCreatorWithPayload<Element, string>,
getFailure: ActionCreatorWithPayload<string, string>,
errorMessage?: (error: Error) => void,
successMessage?: () => void
): () => AppThunk {
return (): AppThunk => async (dispatch) => {
dispatch(getRequesting())
const { error, data } = await elementListService()
if (error) {
dispatch(getFailure(error.message))
errorMessage?.(error)
} else {
dispatch(getSuccess(data as Element))
successMessage?.()
}
}
}
export function gameTitleOrder(boxOrGame: { title: string }): string {
return boxOrGame.title
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.toLowerCase()
.replace(/^[^a-z0-9]+/, "")
.replace(/^(le |la |les |l'|the )/, "")
.replace(/[^a-z0-9]/g, "")
}
export function gameTitleCategory(boxOrGame: { title: string }, length = 3): string {
return gameTitleOrder(boxOrGame)
.substring(0, length)
.replace(/([0-9]).+/, "$1")
.toUpperCase()
}
export function gameTitleExactCategory(boxOrGame: { title: string }): string {
const cats = [
["1", "6"],
["7", "AB"],
["AC", "AK"],
["ALA", "ALO"],
["ALP", "AP"],
["AQ", "ARG"],
["ARH", "AT"],
["AU", "AV"],
["AW", "BAR"],
["BAS", "BER"],
["BES", "BLI"],
["BLJ", "BRE"],
["BRF", "CAC"],
["CAD", "CAP"],
["CAQ", "CEM"],
["CEN", "CHIN"],
["CHJ", "CK"],
["CL"],
["COA", "COL"],
["COM"],
["CON", "COP"],
["COQ", "CRA"],
["CRB", "DEB"],
["DEC", "DES"],
["DET", "DIC"],
["DID", "DOM"],
["DON", "DR"],
["DS", "ELG"],
["ELH", "EVE"],
["EVD", "FAM"],
["FAN", "FIC"],
["FID", "FOL"],
["FOM", "FUM"],
["FUN", "GEE"],
["GED", "GLA"],
["GLB", "GQ"],
["GR", "HAN"],
["HAO", "HIM"],
["HIN", "IL"],
["IM", "IS"],
["IT", "JE"],
["JD", "KAB"],
["KAC", "KEM"],
["KEN", "KILL"],
["KIM", "KOD"],
["KOE", "LAB"],
["LAC", "LIC"],
["LID", "LIP"],
["LIQ", "LOQ"],
["LOR", "LZ"],
["MAA", "MAN"],
["MAO", "MED"],
["MEE", "MIM"],
["MINE", "MOR"],
["MOS", "MYS"],
["MYT", "NE"],
["ND", "ODD"],
["ODE", "ONE"],
["ONF", "OU"],
["OV", "PAN"],
["PAO", "PEL"],
["PEM", "PIO"],
["PIP", "PLZ"],
["PO", "PRI"],
["PRJ", "PZ"],
["QI", "RAT"],
["RAU", "RI"],
["RJ", "ROO"],
["ROP", "SAL"],
["SAM", "SEI"],
["SEJ", "SIC"],
["SID", "SL"],
["SM", "SO"],
["SPA", "SPX"],
["SPY", "STE"],
["STD", "SUN"],
["SUO", "TAF"],
["TAG", "TD"],
["TE", "TIC"],
["TID", "TIL"],
["TIM"],
["TIN", "TO"],
["TP", "TRO"],
["TRP", "UNE"],
["UND", "VEM"],
["VEN", "WAT"],
["WAU", "WH"],
["WI", "YE"],
["YOGA", "ZOO"],
]
const gameCat = gameTitleCategory(boxOrGame, 4)
const foundCat = find(cats, (cat) =>
cat[1]
? gameCat.substring(0, cat[0].length) >= cat[0] &&
gameCat.substring(0, cat[1].length) <= `${cat[1]}`
: gameCat.substring(0, cat[0].length) === cat[0]
)
if (!foundCat) {
console.log("no game found for ", foundCat, boxOrGame.title)
}
return foundCat ? `${foundCat[0]}-${foundCat[1]}` : gameCat
}