manage notification

This commit is contained in:
Raja Oktafrianto
2025-04-15 15:15:56 +07:00
parent 8a3d842819
commit a6e341c54a
4 changed files with 399 additions and 160 deletions

View File

@ -1,48 +1,50 @@
import { DataGridColumnHeader, DataGridProvider } from '@/components';
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
import { Toaster } from '@/components/ui/sonner';
import { apiConfig } from '@/config/api.config';
import { ColumnDef } from '@tanstack/react-table';
import React, { createContext, useCallback, useMemo, useState } from 'react';
import { ListToolBar } from '../blocks/ListToolbar';
interface ContextProps {
showEditDialog: boolean;
handleEditDialog: (show: boolean, selected_user: string | null) => void;
showAddDialog: boolean;
handleAddDialog: (show: boolean) => void;
selectedNotification: string | null;
notifications: NotificationProps[];
}
import { useCallApi } from '@/hooks';
import moment from 'moment';
interface SelectedNotification {
id: string;
name: string;
destination_module: string;
content: string;
subject: string;
type: string;
via: string;
created_at: string;
}
interface NotificationProps {
id: string;
name: string;
destination_module: string;
interface ContextProps {
showAddDialog: boolean;
handleAddDialog: (show: boolean) => void;
showEditDialog: boolean;
handleEditDialog: (show: boolean, selected_notification: string | null) => void;
showDeleteDialog: boolean;
handleDeleteDialog: (show: boolean, selected_notification: string | null) => void;
selectedNotification: string | null;
}
const initialProps: ContextProps = {
showEditDialog: false,
showAddDialog: false,
handleAddDialog: (show: boolean) => {},
showEditDialog: false,
handleEditDialog: () => {},
handleAddDialog: () => {},
selectedNotification: null,
notifications: []
showDeleteDialog: false,
handleDeleteDialog: () => {},
selectedNotification: null
};
const ManageNotifContext = createContext<ContextProps>(initialProps);
const API_URL = apiConfig.service_dashboard;
const API_URL_NOTIFICATION = apiConfig.service_notification;
const ManageNotifContextProvider = ({ children }: { children: React.ReactNode }) => {
const [showEditDialog, setShowEditDialog] = useState(false);
const [showAddDialog, setShowAddDialog] = useState(false);
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const [selectedNotification, setSelectedNotification] = useState<string | null>(null);
const [notifications, setNotifications] = useState<NotificationProps[]>([]);
const { GetData } = useCallApi();
const handleAddDialog = useCallback((show: boolean) => {
setShowAddDialog(show);
@ -53,59 +55,104 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
setShowEditDialog(show);
}, []);
const handleDeleteDialog = useCallback((show: boolean, selected_notification: string | null) => {
setSelectedNotification(show ? selected_notification : null);
setShowDeleteDialog(show);
}, []);
const columns = useMemo<ColumnDef<any>[]>(
() => [
{
accessorFn: (row) => row.id,
id: 'id',
header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
accessorFn: (row) => row.content,
id: 'content',
header: ({ column }) => <DataGridColumnHeader title="Content" column={column} />,
enableSorting: true,
enableHiding: false,
meta: {
headerClassName: 'w-[100px]'
headerClassName: 'w-[300px]'
}
},
{
accessorFn: (row) => row.name,
id: 'name',
header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />,
accessorFn: (row) => row.subject,
id: 'subject',
header: ({ column }) => <DataGridColumnHeader title="Subject" column={column} />,
enableSorting: true,
enableHiding: false
},
{
accessorFn: (row) => row.destination_module,
id: 'destination_module',
header: ({ column }) => <DataGridColumnHeader title="Destination Module" column={column} />,
accessorFn: (row) => row.type,
id: 'type',
header: ({ column }) => <DataGridColumnHeader title="Type" column={column} />,
enableSorting: true,
enableHiding: false
},
{
id: 'actions',
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
meta: {
headerClassName: 'w-[100px]',
cellClassName: 'text-center'
},
cell: (data: any) => {
const row = data.row.original;
return (
<div className="flex justify-center gap-2">
<button
type="button"
className="flex items-center justify-center gap-2 text-sm font-medium leading-6 text-primary"
onClick={() => handleEditDialog(true, row.id)}
>
<span>Edit</span>
</button>
</div>
);
}
accessorFn: (row) => row.via,
id: 'via',
header: ({ column }) => <DataGridColumnHeader title="Via" column={column} />,
enableSorting: true,
enableHiding: false
},
{
accessorFn: (row) => row.created_at,
id: 'created_at',
header: ({ column }) => <DataGridColumnHeader title="Date Create" column={column} />,
enableSorting: true,
enableHiding: false,
cell: ({ row }) => moment(row.original.created_at).format('YYYY-MM-DD HH:mm:ss')
}
// {
// id: 'actions',
// header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
// meta: {
// headerClassName: 'w-[100px]',
// cellClassName: 'text-center'
// },
// cell: (data: any) => {
// const row = data.row.original;
// return (
// <>
// <button
// className="btn btn-sm btn-icon btn-clear btn-light"
// onClick={() => handleEditDialog(true, row.id)}
// >
// <KeenIcon icon="notepad-edit" />
// </button>
// <button
// className="btn btn-sm btn-icon btn-clear btn-light"
// onClick={() => handleDeleteDialog(true, row.id)}
// >
// <KeenIcon icon="trash" />
// </button>
// </>
// );
// }
// }
],
[handleEditDialog, handleAddDialog]
[handleEditDialog, handleDeleteDialog]
);
const getNotificationList = async (page: number, limit: number, sorting: any, filter: any) => {
try {
sorting = sorting.length == 0 ? [{ id: 'content', desc: false }] : sorting;
filter =
filter.length == 0 ? {} : { content: { like: `%${filter[0].value?.toLowerCase()}%` } };
const response = await GetData(`${API_URL_NOTIFICATION}/list`, {
limit: limit,
page: page + 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
filter: JSON.stringify(filter)
});
console.log('API Response notif: ', response);
return { data: response?.data.list, totalCount: response?.data.total_count };
} catch (error) {
console.error('Error fetching notification', error);
}
};
return (
<div>
<ManageNotifContext.Provider
@ -114,8 +161,9 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
showAddDialog,
handleEditDialog,
showEditDialog,
selectedNotification,
notifications
handleDeleteDialog,
showDeleteDialog,
selectedNotification
}}
>
<Toaster expand visibleToasts={9} duration={3000} />
@ -125,8 +173,11 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
pagination={{ size: 10 }}
toolbar={<ListToolBar />}
layout={{ card: true }}
sorting={[{ id: 'username', desc: false }]}
sorting={[{ id: 'content', desc: false }]}
serverSide={true}
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
getNotificationList(pageIndex, pageSize, sorting, columnFilters)
}
>
{children}
</DataGridProvider>