156 lines
4.9 KiB
TypeScript
156 lines
4.9 KiB
TypeScript
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { useManageSucosContext } from '../hooks/useManageSucosContext';
|
|
import { Alert, useDataGrid } from '@/components';
|
|
import { useCallApi } from '@/hooks';
|
|
import { getAuth } from '@/auth';
|
|
import { apiConfig } from '@/config/api.config';
|
|
import { toast } from 'sonner';
|
|
import {
|
|
Dialog,
|
|
DialogBody,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogHeader,
|
|
DialogTitle
|
|
} from '@/components/ui/dialog';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
const API_URL = apiConfig.service_master_data;
|
|
|
|
const AddDialog = () => {
|
|
const parentRef = useRef<any | null>(null);
|
|
const { showAddDialog, handleAddDialog } = useManageSucosContext();
|
|
const { reload } = useDataGrid();
|
|
const { PostData } = useCallApi();
|
|
const parsedUser = getAuth()?.user;
|
|
const [alert, setAlert] = useState({
|
|
show: false,
|
|
message: ''
|
|
});
|
|
const initialState = {
|
|
name: '',
|
|
posto_adm_id: 0,
|
|
created_by: '',
|
|
created_at: ''
|
|
};
|
|
|
|
const [formField, setFormField] = useState(initialState);
|
|
const created_time = new Date();
|
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
|
|
|
const resetForm = () => {
|
|
setFormField(initialState);
|
|
setAlert({ show: false, message: '' });
|
|
};
|
|
|
|
const doCreateSucos = useCallback(async (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
const response = await PostData(`${API_URL}/sucos/create`, formField);
|
|
|
|
if (response?.status) {
|
|
resetForm();
|
|
handleAddDialog(false);
|
|
reload();
|
|
toast.success('Success Create Sucos');
|
|
} else {
|
|
toast.error('Failed Create Sucos');
|
|
setAlert({ show: true, message: response?.message });
|
|
}
|
|
}, []);
|
|
|
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault();
|
|
|
|
if (formField.name.trim() === '' || formField.posto_adm_id === 0) {
|
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
|
return;
|
|
}
|
|
|
|
doCreateSucos(e);
|
|
console.log(formField);
|
|
setAlert({ show: false, message: '' });
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (showAddDialog) {
|
|
setFormField({
|
|
...formField,
|
|
created_by: parsedUser.username,
|
|
created_at: formattedTime
|
|
});
|
|
}
|
|
}, [formattedTime]);
|
|
|
|
useEffect(() => {
|
|
if (showAddDialog === false) {
|
|
resetForm();
|
|
}
|
|
}, [showAddDialog]);
|
|
|
|
return (
|
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
|
<DialogHeader>
|
|
<DialogTitle>Sucos - 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">
|
|
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">
|
|
Posto Adm ID<span className="text-red-500">*</span>
|
|
</label>
|
|
<Input
|
|
className="input"
|
|
type="number"
|
|
min={0}
|
|
value={formField.posto_adm_id === 0 ? '' : formField.posto_adm_id}
|
|
onChange={(e) => {
|
|
const value = parseInt(e.target.value, 10);
|
|
setFormField({ ...formField, posto_adm_id: isNaN(value) ? 0 : value });
|
|
}}
|
|
/>
|
|
</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;
|