add crud provider

This commit is contained in:
Wikzyy
2025-03-10 12:54:48 +07:00
parent 7c8d03f713
commit 8c930ae17a
7 changed files with 794 additions and 4 deletions

View File

@ -0,0 +1,202 @@
import { apiConfig } from '@/config/api.config';
import { useManageProviderContext } from '../hooks/useManageProviderContext';
import { Alert, useDataGrid } from '@/components';
import { useCallApi } from '@/hooks';
import { getAuth } from '@/auth';
import React, { useCallback, useState } from 'react';
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 EditDialog = () => {
const { showEditDialog, handleEditDialog, selectedProvider } = useManageProviderContext();
const { reload } = useDataGrid();
const { PutData } = useCallApi();
const parsedUser = getAuth()?.user;
const created_time = new Date();
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
const [alert, setAlert] = useState({
show: false,
message: ''
});
const initialState = {
name: '',
description: '',
type: '',
status: '',
transactionTypeId: '',
agentId: '',
created_by: '',
created_at: ''
};
const [formField, setFormField] = useState(initialState);
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
const doUpdateProvider = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const response = await PutData(`${API_URL}/provider/update/${selectedProvider}`, formField);
if (response?.status) {
resetForm();
handleEditDialog(false, null);
toast.success('Provider updated successfully.');
reload();
} else {
toast.error('Failed to update provider.');
setAlert({ show: true, message: 'Failed to update provider.' });
}
},
[selectedProvider, formField]
);
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (
formField.name === '' ||
formField.description === '' ||
formField.type === '' ||
formField.status === '' ||
formField.transactionTypeId === '' ||
formField.agentId === ''
) {
setAlert({ show: true, message: 'Please fill in all required fields.' });
return;
}
// doUpdateProvider(e);
console.log(formField);
setAlert({ show: false, message: '' });
};
return (
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
<DialogHeader>
<DialogTitle>Provider - Create</DialogTitle>
<DialogDescription></DialogDescription>
</DialogHeader>
<DialogBody>
<div className="flex flex-col">
{alert.show && (
<Alert variant="danger">
<h3>{alert.message}</h3>
</Alert>
)}
<form onSubmit={handleUpdate}>
<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">
Description<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.description}
onChange={(e) => setFormField({ ...formField, description: 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">
Type<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.type}
onChange={(e) => setFormField({ ...formField, type: 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>
<Input
className="input"
type="text"
value={formField.status}
onChange={(e) => setFormField({ ...formField, status: 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">
Transaction Type Id<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.transactionTypeId}
onChange={(e) =>
setFormField({ ...formField, transactionTypeId: 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">
Agent Id<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.agentId}
onChange={(e) => setFormField({ ...formField, agentId: e.target.value })}
/>
</div>
</div>
<div className="flex justify-end">
<Button variant="default">Save Changes</Button>
</div>
</div>
</form>
</div>
</DialogBody>
</DialogContent>
</Dialog>
);
};
export default EditDialog;