Skip to content

Commit

Permalink
Merge pull request #79 from JimTheCat/CU-8697352uj_Create-AlertProvid…
Browse files Browse the repository at this point in the history
…er_Patryk-Kosiski

feature: Create AlertProvider
  • Loading branch information
JimTheCat authored Dec 15, 2024
2 parents 54c5288 + 14f1bad commit 3f32d66
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 61 deletions.
179 changes: 131 additions & 48 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"test": "jest"
},
"dependencies": {
"@mantine/core": "^7.14.1",
"@mantine/dates": "^7.14.1",
"@mantine/form": "^7.14.1",
"@mantine/hooks": "^7.14.1",
"@mantine/tiptap": "^7.14.1",
"@mantine/core": "^7.15.1",
"@mantine/dates": "^7.15.1",
"@mantine/form": "^7.15.1",
"@mantine/hooks": "^7.15.1",
"@mantine/notifications": "^7.15.1",
"@mantine/tiptap": "^7.15.1",
"@tabler/icons-react": "^3.23.0",
"@tiptap/extension-highlight": "^2.10.0",
"@tiptap/extension-link": "^2.10.0",
Expand Down
47 changes: 47 additions & 0 deletions frontend/src/Providers/AlertProvider.tsx
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;
};
Loading

0 comments on commit 3f32d66

Please sign in to comment.