mirror of
https://github.com/status-im/spiff-arena.git
synced 2025-02-19 13:14:25 +00:00
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import React, { createContext, useState } from 'react';
|
|
import { ErrorForDisplay } from '../interfaces';
|
|
|
|
type ErrorContextType = {
|
|
error: null | ErrorForDisplay;
|
|
addError: Function;
|
|
removeError: Function;
|
|
};
|
|
export const APIErrorContext = createContext<ErrorContextType>({
|
|
error: null,
|
|
// eslint-disable-next-line no-unused-vars
|
|
addError: () => {},
|
|
removeError: () => {},
|
|
});
|
|
|
|
// @ts-ignore
|
|
// eslint-disable-next-line react/prop-types
|
|
export default function APIErrorProvider({ children }) {
|
|
const [error, setError] = useState<ErrorForDisplay | null>(null);
|
|
const addError = (errorForDisplay: ErrorForDisplay | null) => {
|
|
setError(errorForDisplay);
|
|
};
|
|
const removeError = () => setError(null);
|
|
|
|
const contextValue = React.useMemo(
|
|
() => ({
|
|
error,
|
|
addError: (newError: ErrorForDisplay | null) => {
|
|
addError(newError);
|
|
},
|
|
removeError: () => {
|
|
removeError();
|
|
},
|
|
}),
|
|
[error]
|
|
);
|
|
|
|
return (
|
|
<APIErrorContext.Provider value={contextValue}>
|
|
{children}
|
|
</APIErrorContext.Provider>
|
|
);
|
|
}
|