import { apiConfig } from '@/config/api.config'; import { useRef, useState, useCallback, useEffect } from 'react'; import { Alert, useDataGrid } from '@/components'; import { useCallApi } from '@/hooks'; import { Dialog, DialogBody, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { toast } from 'sonner'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Button } from '@/components/ui/button'; import { useManageNotificationContext } from '../hooks/useManageNotificationContext'; import { doSaveLogActivity } from '@/actions/GlobalActions'; import { CustomerProps } from '@/pages/master/provider/blocks/AddDialog'; import { ChevronDown } from 'lucide-react'; const API_URL_CUSTOMER = apiConfig.service_customer; const API_URL_NOTIFICATION = apiConfig.service_notification; const AddDialog = () => { const parentRef = useRef(null); const { handleAddDialog, showAddDialog } = useManageNotificationContext(); const { reload } = useDataGrid(); const { GetData, PostData } = useCallApi(); const [alert, setAlert] = useState({ show: false, message: '' }); const initialState = { customers: [], type: '', via: '', subject: '', content: '' }; const [formField, setFormField] = useState(initialState); const [open, setOpen] = useState(false); const [customers, setCustomers] = useState([]); const resetForm = () => { setFormField(initialState); setAlert({ show: false, message: '' }); }; const handleChange = (e: React.ChangeEvent) => { setFormField({ ...formField, [e.target.name]: e.target.value }); }; const doCreateNotification = async (e: React.FormEvent) => { e.preventDefault(); const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField); console.log('API Response :', response); if (response?.status) { handleAddDialog(false); resetForm(); reload(); toast.success('Notification Send Successfully!'); const createActivity = { module: 'Manage Notification', description: `Send New Notification => ${formField.via}`, action: 'C' }; doSaveLogActivity(createActivity); } else { toast.error('Failed to create notification'); setAlert({ show: true, message: 'Failed to create notification. Please Try Again.' }); } }; // const getCustomerList = async (sorting: any) => { // try { // sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting; // const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, { // limit: 100, // page: 1, // with_deleted: false, // order_field: sorting[0].id, // order_direction: sorting[0].desc == false ? 'ASC' : 'DESC' // }); // setCustomers(response?.data.list); // } catch (error) { // console.error('Error fetching customer', error); // } // }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if ( formField.type.trim() === '' || formField.via.trim() === '' || formField.subject.trim() === '' || formField.content.trim() === '' ) { setAlert({ show: true, message: 'Please fill all required fields.' }); return; } doCreateNotification(e); // console.log(formField); setAlert({ show: false, message: '' }); }; // useEffect(() => { // getCustomerList([{ id: 'id', desc: false }]); // }, []); useEffect(() => { if (showAddDialog === false) { resetForm(); } }, [showAddDialog]); return ( Notification - Create
{alert.show && (

{alert.message}

)} {/*
*/} {/*
{ e.currentTarget.scrollTop += e.deltaY; }} > No Customer found. {customers.map((customer) => ( { setFormField({ ...formField, customers: customer.id }); setOpen(false); }} > {customer.username} ))}
*/}
{['info', 'promo'].map((type) => ( ))}