Files
revenue-fe/src/pages/menu/manage-menu/blocks/AddDIalog.tsx
2025-03-18 02:44:19 +07:00

224 lines
8.3 KiB
TypeScript

import { apiConfig } from '@/config/api.config';
import React, { useCallback, useRef, useState } from 'react';
import { useManageMenusContext } from '../hooks/useManageMenusContext';
import { Alert, useDataGrid } from '@/components';
import { useCallApi } from '@/hooks';
import { toast } from 'sonner';
import {
Dialog,
DialogBody,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog';
import { FormControl,NativeSelect } from "@mui/material";
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
const API_URL = apiConfig.service_dashboard;
const AddDialog = () => {
const parentRef = useRef<any | null>(null);
const { showAddDialog, handleAddDialog, parents } = useManageMenusContext();
const { reload } = useDataGrid();
const { PostData } = useCallApi();
const [alert, setAlert] = useState({
show: false,
message: ''
});
const initialState = {
module: '',
name: '',
link: '',
id_parent: '',
order_number: 0,
icon: '',
status: ''
};
const [formField, setFormField] = useState(initialState);
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (
formField.module === '' ||
formField.name === '' ||
formField.link === '' ||
formField.order_number === 0 ||
formField.status === ''
) {
setAlert({ show: true, message: 'Please fill in all required fields.' });
return;
}
const response = await PostData(`${API_URL}/menus/create`, formField);
if (response?.status) {
handleAddDialog(false);
resetForm();
toast.success('Success Create Menu');
reload();
} else {
toast.error('Failed Create Menu');
setAlert({ show: true, message: 'Failed Create Menu' });
}
console.log(formField);
setAlert({ show: false, message: '' });
};
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
return (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
<DialogHeader>
<DialogTitle>Menu - Create</DialogTitle>
<DialogDescription></DialogDescription>
</DialogHeader>
<DialogBody ref={parentRef}>
<div className="flex flex-col">
{alert.show && (
<Alert variant="danger">
<h3>{alert.message}</h3>
</Alert>
)}
<form onSubmit={handleSubmit}>
<div className="card-body grid gap-5">
<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">
Module<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.module}
onChange={(e) => setFormField({ ...formField, module: e.target.value })}
/>
</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">
Name<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.name}
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
/>
</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">
Link<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.link}
onChange={(e) => setFormField({ ...formField, link: e.target.value })}
/>
</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">Parent</label>
<FormControl fullWidth margin="dense">
<NativeSelect
defaultValue={30}
value={formField.id_parent}
onChange={(e: any) => setFormField({ ...formField, id_parent: e.target.value })}
inputProps={{
name: 'id_parent',
id: 'uncontrolled-native',
}}
>
{
parents ? parents.map((el: any) => (
<option key={el.id} value={el.id}>{el.name}</option>
)) : ''
}
</NativeSelect>
</FormControl>
</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">
Order Number<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
min={0}
value={formField.order_number === 0 ? '' : formField.order_number}
onChange={(e) => {
const value = parseInt(e.target.value, 10);
setFormField({ ...formField, order_number: isNaN(value) ? 0 : value });
}}
/>
</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">Icon</label>
<Input
className="input"
type="text"
value={formField.icon}
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
/>
</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">
Status<span className="text-red-500">*</span>
</label>
<FormControl fullWidth margin="dense">
<NativeSelect
defaultValue={30}
value={formField.status}
onChange={(e: any) => setFormField({ ...formField, status: e.target.value })}
inputProps={{
name: 'status',
id: 'uncontrolled-native',
}}
>
<option value={"Y"}>Active</option>
<option value={"N"}>Inactive</option>
</NativeSelect>
</FormControl>
</div>
</div>
<div className="flex justify-end gap-5">
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button variant="default">Save Changes</Button>
</div>
</div>
</form>
</div>
</DialogBody>
</DialogContent>
</Dialog>
);
};
export default AddDialog;