fix double click in notification manager
This commit is contained in:
@ -36,6 +36,7 @@ const AddDialog = () => {
|
|||||||
const { reload } = useDataGrid();
|
const { reload } = useDataGrid();
|
||||||
const { GetData, PostData } = useCallApi();
|
const { GetData, PostData } = useCallApi();
|
||||||
const [alert, setAlert] = useState({ show: false, message: '' });
|
const [alert, setAlert] = useState({ show: false, message: '' });
|
||||||
|
const isSubmittingRef = useRef(false);
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
customers: [] as string[],
|
customers: [] as string[],
|
||||||
@ -48,9 +49,11 @@ const AddDialog = () => {
|
|||||||
|
|
||||||
const [formField, setFormField] = useState(initialState);
|
const [formField, setFormField] = useState(initialState);
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
const [customers, setCustomers] = useState<CustomerProps[]>([]);
|
const [customers, setCustomers] = useState<CustomerProps[]>([]);
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setFormField(initialState);
|
setFormField({ ...initialState });
|
||||||
|
setIsSubmitting(false);
|
||||||
setAlert({ show: false, message: '' });
|
setAlert({ show: false, message: '' });
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -62,36 +65,41 @@ const AddDialog = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const doCreateNotification = async (e: React.FormEvent<HTMLFormElement>) => {
|
const doCreateNotification = useCallback(
|
||||||
e.preventDefault();
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
if (isSubmittingRef.current) return;
|
||||||
|
isSubmittingRef.current = true;
|
||||||
|
setIsSubmitting(true);
|
||||||
|
|
||||||
// const payload = {
|
try {
|
||||||
// ...formField,
|
const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField);
|
||||||
// subject:
|
// console.log('send response :', response);
|
||||||
// formField.subject.trim() === '' && formField.via === 'sms'
|
|
||||||
// ? 'SMS Notification'
|
|
||||||
// : formField.subject
|
|
||||||
// };
|
|
||||||
|
|
||||||
const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField);
|
if (response?.status) {
|
||||||
// console.log('send response :', response);
|
handleAddDialog(false);
|
||||||
|
resetForm();
|
||||||
if (response?.status) {
|
reload();
|
||||||
handleAddDialog(false);
|
toast.success('Notification Send Successfully!');
|
||||||
resetForm();
|
const createActivity = {
|
||||||
reload();
|
module: 'Manage Notification',
|
||||||
toast.success('Notification Send Successfully!');
|
description: `Send New Notification => ${formField.via}`,
|
||||||
const createActivity = {
|
action: 'C'
|
||||||
module: 'Manage Notification',
|
};
|
||||||
description: `Send New Notification => ${formField.via}`,
|
doSaveLogActivity(createActivity);
|
||||||
action: 'C'
|
} else {
|
||||||
};
|
toast.error('Failed to create notification');
|
||||||
doSaveLogActivity(createActivity);
|
setAlert({ show: true, message: 'Failed to create notification. Please Try Again.' });
|
||||||
} else {
|
}
|
||||||
toast.error('Failed to create notification');
|
} catch (err) {
|
||||||
setAlert({ show: true, message: 'Failed to create notification. Please Try Again.' });
|
toast.error('Something went wrong');
|
||||||
}
|
console.log(err);
|
||||||
};
|
} finally {
|
||||||
|
isSubmittingRef.current = false;
|
||||||
|
setIsSubmitting(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[PostData, formField, handleAddDialog, reload]
|
||||||
|
);
|
||||||
|
|
||||||
const getCustomerList = async (sorting: any) => {
|
const getCustomerList = async (sorting: any) => {
|
||||||
try {
|
try {
|
||||||
@ -113,6 +121,8 @@ const AddDialog = () => {
|
|||||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (isSubmittingRef.current) return;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
formField.all_customer.trim() === '' ||
|
formField.all_customer.trim() === '' ||
|
||||||
formField.via.trim() === '' ||
|
formField.via.trim() === '' ||
|
||||||
@ -132,6 +142,7 @@ const AddDialog = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsSubmitting(true);
|
||||||
doCreateNotification(e);
|
doCreateNotification(e);
|
||||||
// console.log(formField);
|
// console.log(formField);
|
||||||
setAlert({ show: false, message: '' });
|
setAlert({ show: false, message: '' });
|
||||||
@ -155,213 +166,223 @@ const AddDialog = () => {
|
|||||||
<DialogDescription></DialogDescription>
|
<DialogDescription></DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<DialogBody className="scrollable">
|
<DialogBody className="scrollable">
|
||||||
<form onSubmit={handleSubmit} className="space-y-6">
|
<fieldset disabled={isSubmitting}>
|
||||||
{alert.show && (
|
<form onSubmit={handleSubmit} className="space-y-6">
|
||||||
<Alert variant="danger">
|
{alert.show && (
|
||||||
<h3>{alert.message}</h3>
|
<Alert variant="danger">
|
||||||
</Alert>
|
<h3>{alert.message}</h3>
|
||||||
)}
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Send To<span className="text-red-500">*</span>
|
Send To<span className="text-red-500">*</span>
|
||||||
</label>
|
|
||||||
<div className="flex gap-6 items-center">
|
|
||||||
<label className="flex items-center space-x-2">
|
|
||||||
<input
|
|
||||||
type="radio"
|
|
||||||
name="all_customer"
|
|
||||||
value="true"
|
|
||||||
checked={formField.all_customer === 'true'}
|
|
||||||
onChange={handleChange}
|
|
||||||
className="accent-blue-600"
|
|
||||||
/>
|
|
||||||
<span>All Customers</span>
|
|
||||||
</label>
|
</label>
|
||||||
<label className="flex items-center space-x-2">
|
<div className="flex gap-6 items-center">
|
||||||
<input
|
<label className="flex items-center space-x-2">
|
||||||
type="radio"
|
|
||||||
name="all_customer"
|
|
||||||
value="false"
|
|
||||||
checked={formField.all_customer === 'false'}
|
|
||||||
onChange={handleChange}
|
|
||||||
className="accent-blue-600"
|
|
||||||
/>
|
|
||||||
<span>Selected Customers</span>
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full">
|
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">Customer</label>
|
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
|
||||||
<PopoverTrigger asChild>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="input col-span-5 text-left flex justify-between"
|
|
||||||
style={{ color: 'inherit' }}
|
|
||||||
disabled={formField.all_customer !== 'false'}
|
|
||||||
>
|
|
||||||
<span>
|
|
||||||
{formField.customers.length === 0
|
|
||||||
? 'Select Customers'
|
|
||||||
: `${formField.customers.length} Customers Selected`}
|
|
||||||
</span>
|
|
||||||
<ChevronDown className="w-4 h-4 opacity-70" />
|
|
||||||
</button>
|
|
||||||
</PopoverTrigger>
|
|
||||||
<PopoverContent className="w-[400px] p-0">
|
|
||||||
<Command>
|
|
||||||
<CommandInput placeholder="Search Customer..." />
|
|
||||||
<CommandList
|
|
||||||
className="max-h-[300px] overflow-y-auto"
|
|
||||||
style={{ touchAction: 'pan-y' }}
|
|
||||||
onWheel={(e) => {
|
|
||||||
e.currentTarget.scrollTop += e.deltaY;
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<CommandEmpty>No Customer found.</CommandEmpty>
|
|
||||||
<CommandGroup>
|
|
||||||
{customers.map((customer) => {
|
|
||||||
const isSelected = formField.customers.includes(customer.id);
|
|
||||||
return (
|
|
||||||
<CommandItem
|
|
||||||
key={customer.id}
|
|
||||||
value={customer.username}
|
|
||||||
onSelect={() => {
|
|
||||||
const customerId = customer.id;
|
|
||||||
setFormField((prev) => {
|
|
||||||
const isSelected = prev.customers.includes(customerId);
|
|
||||||
return {
|
|
||||||
...prev,
|
|
||||||
customers: isSelected
|
|
||||||
? prev.customers.filter((id) => id !== customerId)
|
|
||||||
: [...prev.customers, customerId]
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
className={isSelected ? 'bg-accent font-semibold' : ''}
|
|
||||||
>
|
|
||||||
<span className="flex items-center gap-2">
|
|
||||||
{isSelected && <Check className="w-4 h-4 text-green-600" />}
|
|
||||||
{customer.username}
|
|
||||||
</span>
|
|
||||||
</CommandItem>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</CommandGroup>
|
|
||||||
</CommandList>
|
|
||||||
</Command>
|
|
||||||
</PopoverContent>
|
|
||||||
</Popover>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full">
|
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
|
||||||
Type<span className="text-red-500">*</span>
|
|
||||||
</label>
|
|
||||||
<div className="flex gap-6 items-center">
|
|
||||||
{['info', 'promo'].map((type) => (
|
|
||||||
<label key={type} className="flex items-center space-x-2">
|
|
||||||
<input
|
<input
|
||||||
type="radio"
|
type="radio"
|
||||||
name="type"
|
name="all_customer"
|
||||||
value={type}
|
value="true"
|
||||||
checked={formField.type === type}
|
checked={formField.all_customer === 'true'}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
className="accent-blue-600"
|
className="accent-blue-600"
|
||||||
/>
|
/>
|
||||||
<span>{type.charAt(0).toUpperCase() + type.slice(1)}</span>
|
<span>All Customers</span>
|
||||||
</label>
|
</label>
|
||||||
))}
|
<label className="flex items-center space-x-2">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="all_customer"
|
||||||
|
value="false"
|
||||||
|
checked={formField.all_customer === 'false'}
|
||||||
|
onChange={handleChange}
|
||||||
|
className="accent-blue-600"
|
||||||
|
/>
|
||||||
|
<span>Selected Customers</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">Customer</label>
|
||||||
Send Via<span className="text-red-500">*</span>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
</label>
|
<PopoverTrigger asChild>
|
||||||
<div className="flex gap-6 items-center">
|
<button
|
||||||
<label className="flex items-center space-x-2">
|
type="button"
|
||||||
<input
|
className="input col-span-5 text-left flex justify-between"
|
||||||
type="radio"
|
style={{ color: 'inherit' }}
|
||||||
name="via"
|
disabled={formField.all_customer !== 'false'}
|
||||||
value="fcm"
|
>
|
||||||
checked={formField.via === 'fcm'}
|
<span>
|
||||||
onChange={handleChange}
|
{formField.customers.length === 0
|
||||||
className="accent-blue-600"
|
? 'Select Customers'
|
||||||
/>
|
: `${formField.customers.length} Customers Selected`}
|
||||||
<span>FCM</span>
|
</span>
|
||||||
</label>
|
<ChevronDown className="w-4 h-4 opacity-70" />
|
||||||
<label className="flex items-center space-x-2">
|
</button>
|
||||||
<input
|
</PopoverTrigger>
|
||||||
type="radio"
|
<PopoverContent className="w-[400px] p-0">
|
||||||
name="via"
|
<Command>
|
||||||
value="sms"
|
<CommandInput placeholder="Search Customer..." />
|
||||||
checked={formField.via === 'sms'}
|
<CommandList
|
||||||
onChange={handleChange}
|
className="max-h-[300px] overflow-y-auto"
|
||||||
className="accent-blue-600"
|
style={{ touchAction: 'pan-y' }}
|
||||||
/>
|
onWheel={(e) => {
|
||||||
<span>SMS</span>
|
e.currentTarget.scrollTop += e.deltaY;
|
||||||
</label>
|
}}
|
||||||
<label className="flex items-center space-x-2">
|
>
|
||||||
<input
|
<CommandEmpty>No Customer found.</CommandEmpty>
|
||||||
type="radio"
|
<CommandGroup>
|
||||||
name="via"
|
{customers.map((customer) => {
|
||||||
value="email"
|
const isSelected = formField.customers.includes(customer.id);
|
||||||
checked={formField.via === 'email'}
|
return (
|
||||||
onChange={handleChange}
|
<CommandItem
|
||||||
className="accent-blue-600"
|
key={customer.id}
|
||||||
/>
|
value={customer.username}
|
||||||
<span>E-Mail</span>
|
onSelect={() => {
|
||||||
</label>
|
const customerId = customer.id;
|
||||||
|
setFormField((prev) => {
|
||||||
|
const isSelected = prev.customers.includes(customerId);
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
customers: isSelected
|
||||||
|
? prev.customers.filter((id) => id !== customerId)
|
||||||
|
: [...prev.customers, customerId]
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
className={isSelected ? 'bg-accent font-semibold' : ''}
|
||||||
|
>
|
||||||
|
<span className="flex items-center gap-2">
|
||||||
|
{isSelected && <Check className="w-4 h-4 text-green-600" />}
|
||||||
|
{customer.username}
|
||||||
|
</span>
|
||||||
|
</CommandItem>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</CommandGroup>
|
||||||
|
</CommandList>
|
||||||
|
</Command>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">Subject</label>
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
<Input
|
Type<span className="text-red-500">*</span>
|
||||||
className="input col-span-6"
|
</label>
|
||||||
name="subject"
|
<div className="flex gap-6 items-center">
|
||||||
placeholder="Enter Subject"
|
{['info', 'promo'].map((type) => (
|
||||||
value={formField.subject}
|
<label key={type} className="flex items-center space-x-2">
|
||||||
onChange={handleChange}
|
<input
|
||||||
disabled={formField.via !== 'email' && formField.via !== 'fcm'}
|
type="radio"
|
||||||
/>
|
name="type"
|
||||||
|
value={type}
|
||||||
|
checked={formField.type === type}
|
||||||
|
onChange={handleChange}
|
||||||
|
className="accent-blue-600"
|
||||||
|
/>
|
||||||
|
<span>{type.charAt(0).toUpperCase() + type.slice(1)}</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
<label className="form-label flex items-center gap-1 max-w-56">
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
Content<span className="text-red-500">*</span>
|
Send Via<span className="text-red-500">*</span>
|
||||||
</label>
|
</label>
|
||||||
<Textarea
|
<div className="flex gap-6 items-center">
|
||||||
className="input col-span-6"
|
<label className="flex items-center space-x-2">
|
||||||
name="content"
|
<input
|
||||||
placeholder="Enter Content Notification"
|
type="radio"
|
||||||
value={formField.content}
|
name="via"
|
||||||
onChange={handleChange}
|
value="fcm"
|
||||||
/>
|
checked={formField.via === 'fcm'}
|
||||||
|
onChange={handleChange}
|
||||||
|
className="accent-blue-600"
|
||||||
|
/>
|
||||||
|
<span>FCM</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center space-x-2">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="via"
|
||||||
|
value="sms"
|
||||||
|
checked={formField.via === 'sms'}
|
||||||
|
onChange={handleChange}
|
||||||
|
className="accent-blue-600"
|
||||||
|
/>
|
||||||
|
<span>SMS</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center space-x-2">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="via"
|
||||||
|
value="email"
|
||||||
|
checked={formField.via === 'email'}
|
||||||
|
onChange={handleChange}
|
||||||
|
className="accent-blue-600"
|
||||||
|
/>
|
||||||
|
<span>E-Mail</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="flex justify-end gap-4">
|
<div className="w-full">
|
||||||
<Button type="reset" variant="outline" onClick={() => setFormField(initialState)}>
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
Reset
|
<label className="form-label flex items-center gap-1 max-w-56">Subject</label>
|
||||||
</Button>
|
<Input
|
||||||
<Button type="submit">Create Notification</Button>
|
className="input col-span-6"
|
||||||
</div>
|
name="subject"
|
||||||
</form>
|
placeholder="Enter Subject"
|
||||||
|
value={formField.subject}
|
||||||
|
onChange={handleChange}
|
||||||
|
disabled={formField.via !== 'email' && formField.via !== 'fcm'}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="w-full">
|
||||||
|
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||||
|
<label className="form-label flex items-center gap-1 max-w-56">
|
||||||
|
Content<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Textarea
|
||||||
|
className="input col-span-6"
|
||||||
|
name="content"
|
||||||
|
placeholder="Enter Content Notification"
|
||||||
|
value={formField.content}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end gap-4">
|
||||||
|
<Button type="reset" variant="outline" onClick={() => setFormField(initialState)}>
|
||||||
|
Reset
|
||||||
|
</Button>
|
||||||
|
<div className="flex justify-end pt-2.5">
|
||||||
|
<Button
|
||||||
|
className={`btn btn-primary ${isSubmitting ? 'opacity-70 cursor-not-allowed' : ''}`}
|
||||||
|
type="submit"
|
||||||
|
disabled={isSubmitting}
|
||||||
|
>
|
||||||
|
{isSubmitting ? 'Creating...' : 'Create Notification'}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</fieldset>
|
||||||
</DialogBody>
|
</DialogBody>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|||||||
Reference in New Issue
Block a user