Path: blob/1.0-develop/resources/scripts/plugins/useFlash.ts
7458 views
import { Actions, useStoreActions } from 'easy-peasy';1import { FlashStore } from '@/state/flashes';2import { ApplicationStore } from '@/state';34interface KeyedFlashStore {5addError: (message: string, title?: string) => void;6clearFlashes: () => void;7clearAndAddHttpError: (error?: Error | string | null) => void;8}910const useFlash = (): Actions<FlashStore> => {11return useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);12};1314const useFlashKey = (key: string): KeyedFlashStore => {15const { addFlash, clearFlashes, clearAndAddHttpError } = useFlash();1617return {18addError: (message, title) => addFlash({ key, message, title, type: 'error' }),19clearFlashes: () => clearFlashes(key),20clearAndAddHttpError: (error) => clearAndAddHttpError({ key, error }),21};22};2324export { useFlashKey };25export default useFlash;262728