-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from JimTheCat/CU-8697352uj_Create-AlertProvid…
…er_Patryk-Kosiski feature: Create AlertProvider
- Loading branch information
Showing
5 changed files
with
217 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// src/providers/AlertProvider.tsx | ||
import React, {createContext, ReactNode, useContext, useMemo} from 'react'; | ||
import {Notifications, showNotification} from '@mantine/notifications'; | ||
import {setApiErrorHandler} from "../Services/api.ts"; | ||
|
||
// Context type | ||
interface AlertContextType { | ||
showError: (message: string) => void; | ||
} | ||
|
||
const AlertContext = createContext<AlertContextType | undefined>(undefined); | ||
|
||
// Provider for AlertContext | ||
interface AlertProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const AlertProvider: React.FC<AlertProviderProps> = ({children}) => { | ||
const showError = (message: string) => { | ||
showNotification({ | ||
title: 'Error', | ||
message, | ||
color: 'red', | ||
position: 'bottom-right', | ||
}); | ||
}; | ||
|
||
setApiErrorHandler(showError); | ||
|
||
const contextValue = useMemo(() => ({showError}), [showError]); | ||
|
||
return ( | ||
<AlertContext.Provider value={contextValue}> | ||
<Notifications/> | ||
{children} | ||
</AlertContext.Provider> | ||
); | ||
}; | ||
|
||
// Hook for using AlertContext | ||
export const useAlert = (): AlertContextType => { | ||
const context = useContext(AlertContext); | ||
if (!context) { | ||
throw new Error('useAlert must be used within an AlertProvider'); | ||
} | ||
return context; | ||
}; |
Oops, something went wrong.