Skip to content

Commit

Permalink
Fix other type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdelion committed Aug 29, 2024
1 parent 86dbdf4 commit 287f3ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 9 additions & 0 deletions editor.planx.uk/src/components/Toast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ type DeleteToast = {
type: "DELETE_TOAST";
payload: { id: number };
};

export type ToastContextType = {
addToast: (type: ToastType, message: string) => void;
remove: (id: number) => void;
success: (message: string) => void;
warning: (message: string) => void;
info: (message: string) => void;
error: (message: string) => void;
} | null;
17 changes: 14 additions & 3 deletions editor.planx.uk/src/contexts/ToastContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import ToastContainer from "components/Toast/ToastContainer";
import { ToastState, ToastType } from "components/Toast/types";
import {
ToastContextType,
ToastState,
ToastType,
} from "components/Toast/types";
import React, { createContext, ReactNode, useReducer } from "react";
import { toastReducer } from "reducers/toastReducer";

export const ToastContext = createContext(null);
export const ToastContext = createContext<ToastContextType>(null);

const initialState: ToastState = {
toasts: [],
Expand Down Expand Up @@ -36,7 +40,14 @@ export const ToastContextProvider = ({
addToast("error", message);
};

const value = { success, warning, info, error, remove };
const value: ToastContextType = {
success,
warning,
info,
error,
remove,
addToast,
};

return (
<ToastContext.Provider value={value}>
Expand Down
2 changes: 2 additions & 0 deletions editor.planx.uk/src/reducers/toastReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const toastReducer = (state: ToastState, action: ToastAction) => {
};
}
default:
// @ts-ignore
// Typescript complains because action is of type 'never' here
throw new Error(`Unhandled action type: ${action.type}`);
}
};

0 comments on commit 287f3ba

Please sign in to comment.