update search filter

- add debounce
- set default value page to 1 each time search
This commit is contained in:
Wikzyy
2025-04-16 22:26:21 +07:00
parent 68e5c15a00
commit 40ebdcc37f
15 changed files with 211 additions and 598 deletions

View File

@ -39,6 +39,7 @@ const EditDialog = () => {
const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [open, setOpen] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [postoadms, setPostoadms] = useState<PostoAdmsProps[]>([]);
const [alert, setAlert] = useState({
@ -111,6 +112,7 @@ const EditDialog = () => {
}, []);
const doFetchData = useCallback(async (id: string) => {
setIsLoading(true);
const response = await GetData(`${API_URL}/sucos/getdata/${id}`, { id });
// console.log('Data Sucos:', response?.data);
@ -126,6 +128,7 @@ const EditDialog = () => {
name: ''
}));
}
setIsLoading(false);
}, []);
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
@ -182,72 +185,87 @@ const EditDialog = () => {
</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">
Sucos Name<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="text"
value={formField.name}
onChange={(e) => setFormField({ ...formField, name: e.target.value })}
/>
{isLoading ? (
<div className="flex flex-col items-center justify-center p-8">
<div className="animate-pulse flex space-x-4 w-full">
<div className="flex-1 space-y-4 py-1">
<div className="h-4 bg-gray-200 rounded w-3/4"></div>
<div className="space-y-2">
<div className="h-4 bg-gray-200 rounded"></div>
<div className="h-4 bg-gray-200 rounded w-5/6"></div>
</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">
Postu Administrativo Name
<span className="text-red-500">*</span>
</label>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{postoadms.find((posto) => posto.PostoAdms_id === formField.postoId)
?.PostoAdms_name || 'Select Postu Administrativo'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search Postu Administrativo..." />
<CommandList>
<CommandEmpty>No Postu Administrativo Found.</CommandEmpty>
<CommandGroup>
{postoadms.map((posto) => (
<CommandItem
key={posto.PostoAdms_id}
value={posto.PostoAdms_name}
onSelect={() => {
setFormField({
...formField,
postoId: posto.PostoAdms_id
});
setOpen(false);
}}
>
{posto.PostoAdms_name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
</div>
<div className="flex justify-end gap-5">
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button className="btn btn-primary">Save Changes</Button>
</div>
<p className="mt-4 text-gray-500">Loading Sucos Details...</p>
</div>
</form>
) : (
<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">
Sucos 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">
Postu Administrativo Name
<span className="text-red-500">*</span>
</label>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{postoadms.find((posto) => posto.PostoAdms_id === formField.postoId)
?.PostoAdms_name || 'Select Postu Administrativo'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search Postu Administrativo..." />
<CommandList>
<CommandEmpty>No Postu Administrativo Found.</CommandEmpty>
<CommandGroup>
{postoadms.map((posto) => (
<CommandItem
key={posto.PostoAdms_id}
value={posto.PostoAdms_name}
onSelect={() => {
setFormField({
...formField,
postoId: posto.PostoAdms_id
});
setOpen(false);
}}
>
{posto.PostoAdms_name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
</div>
<div className="flex justify-end gap-5">
<Button type="button" variant="outline" onClick={resetForm}>
Reset
</Button>
<Button className="btn btn-primary">Save Changes</Button>
</div>
</div>
</form>
)}
</div>
</DialogBody>
</DialogContent>