Notifications
In this step, we will explore integrating Ant Design's notification system with Refine to deliver notifications to users.
Refine triggers notification in various scenarios, such as when a record is created, updated, or deleted, when there is an error from your data provider or your auth provider. It's important to provide feedback to the user when interacting with the application.
All these notifications are enabled just by providing a notificationProvider
prop to the <Refine />
component. A notification provider is responsible from displaying and dismissing notifications as well as handling undoable mutation notifications.
Ant Design provides a notification system that can be used to handle these notifications by using the useNotificationProvider
export of the @refinedev/antd
package.
Adding Notification Provider
To add the notification provider to our application, we need to import the useNotificationProvider
and pass it to the notificationProvider
prop of the <Refine />
component.
Update your src/App.tsx
file by adding the following lines:
import { Refine, Authenticated } from "@refinedev/core";
import routerProvider, { NavigateToResource } from "@refinedev/react-router";
import {
ThemedLayoutV2,
ThemedTitleV2,
useNotificationProvider,
} from "@refinedev/antd";
import { BrowserRouter, Routes, Route, Outlet } from "react-router";
import { ConfigProvider, App as AntdApp } from "antd";
import { dataProvider } from "./providers/data-provider";
import { authProvider } from "./providers/auth-provider";
import { ShowProduct } from "./pages/products/show";
import { EditProduct } from "./pages/products/edit";
import { ListProducts } from "./pages/products/list";
import { CreateProduct } from "./pages/products/create";
import { Login } from "./pages/login";
import "antd/dist/reset.css";
export default function App(): JSX.Element {
return (
<BrowserRouter>
<ConfigProvider>
<AntdApp>
<Refine
dataProvider={dataProvider}
authProvider={authProvider}
routerProvider={routerProvider}
notificationProvider={useNotificationProvider}
resources={[
{
name: "protected-products",
list: "/products",
show: "/products/:id",
edit: "/products/:id/edit",
create: "/products/create",
meta: { label: "Products" },
},
]}
>
<Routes>{/* ... */}</Routes>
</Refine>
</AntdApp>
</ConfigProvider>
</BrowserRouter>
);
}
Now we're set with the notification provider. You can now create, update, and delete records to see the notifications in action.
In the next step, we'll learn about the <AuthPage />
components and how to use them to create login pages.
import { useMany, getDefaultFilter } from "@refinedev/core"; import { useTable, EditButton, ShowButton, getDefaultSortOrder, FilterDropdown, useSelect, List, } from "@refinedev/antd"; import { Table, Space, Input, Select } from "antd"; export const ListProducts = () => { const { tableProps, filters, sorters } = useTable({ sorters: { initial: [{ field: "id", order: "asc" }] }, filters: { initial: [{ field: "category.id", operator: "eq", value: 2 }], }, syncWithLocation: true, }); const { data: categories, isLoading } = useMany({ resource: "categories", ids: tableProps?.dataSource?.map((product) => product.category?.id) ?? [], }); const { selectProps } = useSelect({ resource: "categories", defaultValue: getDefaultFilter("category.id", filters, "eq"), }); return ( <List> <Table {...tableProps} rowKey="id"> <Table.Column dataIndex="id" title="ID" sorter defaultSortOrder={getDefaultSortOrder("id", sorters)} /> <Table.Column dataIndex="name" title="Name" sorter defaultSortOrder={getDefaultSortOrder("name", sorters)} filterDropdown={(props) => ( <FilterDropdown {...props}> <Input /> </FilterDropdown> )} /> <Table.Column dataIndex={["category", "id"]} title="Category" render={(value) => { if (isLoading) { return "Loading..."; } return categories?.data?.find((category) => category.id == value) ?.title; }} filterDropdown={(props) => ( <FilterDropdown {...props} mapValue={(selectedKey) => Number(selectedKey)} > <Select style={{ minWidth: 200 }} {...selectProps} /> </FilterDropdown> )} defaultFilteredValue={getDefaultFilter("category.id", filters, "eq")} /> <Table.Column dataIndex="material" title="Material" /> <Table.Column dataIndex="price" title="Price" /> <Table.Column title="Actions" render={(_, record) => ( <Space> <ShowButton hideText size="small" recordItemId={record.id} /> <EditButton hideText size="small" recordItemId={record.id} /> </Space> )} /> </Table> </List> ); };