This commit is contained in:
Raja Oktafrianto
2025-03-17 11:58:43 +07:00
parent ed7671a856
commit 19eb059354
27 changed files with 826 additions and 236 deletions

View File

@ -15,6 +15,20 @@ import {
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from '@/components/ui/command';
interface MunicipioProps {
id: number;
name: string;
}
const API_URL = apiConfig.service_master_data;
@ -22,9 +36,10 @@ const AddDialog = () => {
const parentRef = useRef<any | null>(null);
const { showAddDialog, handleAddDialog } = useManagePostoAdmsContext();
const { reload } = useDataGrid();
const { PostData } = useCallApi();
const { PostData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [municipios, setMunicipios] = useState<MunicipioProps[]>([]);
const [open, setOpen] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
@ -88,6 +103,34 @@ const AddDialog = () => {
}
}, [formattedTime]);
useEffect(() => {
if (showAddDialog === false) {
resetForm();
}
}, [showAddDialog]);
useEffect(() => {
const fetchMunicipios = async (sorting: any) => {
try {
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL}/municipios/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// console.log(response?.data);
setMunicipios(response?.data.list);
} catch (error) {
console.error('Error fetching municipios', error);
}
};
fetchMunicipios([{ id: 'name', desc: false }]);
}, []);
return (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
@ -122,18 +165,41 @@ const AddDialog = () => {
<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">
Municipio ID<span className="text-red-500">*</span>
Municipio Name<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
min={0}
value={formField.municipio_id === 0 ? '' : formField.municipio_id}
onChange={(e) => {
const value = parseInt(e.target.value, 10);
setFormField({ ...formField, municipio_id: isNaN(value) ? 0 : value });
}}
/>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{municipios.find((municipio) => municipio.id === formField.municipio_id)
?.name || 'Select Municipios'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search Municipios..." />
<CommandList>
<CommandEmpty>No Municipio found.</CommandEmpty>
<CommandGroup>
{municipios.map((municipio) => (
<CommandItem
key={municipio.id}
value={municipio.name}
onSelect={() => {
setFormField({
...formField,
municipio_id: municipio.id
});
setOpen(false);
}}
>
{municipio.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
</div>

View File

@ -15,15 +15,32 @@ import {
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from '@/components/ui/command';
interface MunicipioProps {
id: number;
name: string;
}
const API_URL = apiConfig.service_master_data;
const EditDialog = () => {
const parentRef = useRef<any | null>(null);
const { showEditDialog, handleEditDialog, selectedPostoAdms } = useManagePostoAdmsContext();
const { showEditDialog, handleEditDialog, selectedPostoAdms, postoAdms } =
useManagePostoAdmsContext();
const { reload } = useDataGrid();
const { PutData } = useCallApi();
const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [open, setOpen] = useState(false);
const [municipios, setMunicipios] = useState<MunicipioProps[]>([]);
const [alert, setAlert] = useState({
show: false,
@ -35,7 +52,6 @@ const EditDialog = () => {
updated_by: '',
updated_at: ''
};
const [formField, setFormField] = useState(initialState);
const created_time = new Date();
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
@ -87,6 +103,30 @@ const EditDialog = () => {
}
}, [formattedTime]);
useEffect(() => {
try {
const fetchMunicipios = async (sorting: any) => {
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL}/municipios/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// console.log(response?.data);
setMunicipios(response?.data.list);
};
fetchMunicipios([{ id: 'name', desc: false }]);
} catch (error) {
console.error('Error fetching municipios', error);
}
}, []);
// console.log(selectedPostoAdms);
return (
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
@ -121,22 +161,48 @@ const EditDialog = () => {
<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">
Municipio ID<span className="text-red-500">*</span>
Municipio Name<span className="text-red-500">*</span>
</label>
<Input
className="input"
type="number"
min={0}
value={formField.municipio_id === 0 ? '' : formField.municipio_id}
onChange={(e) => {
const value = parseInt(e.target.value, 10);
setFormField({ ...formField, municipio_id: isNaN(value) ? 0 : value });
}}
/>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{municipios.find((municipio) => municipio.id === formField.municipio_id)
?.name || 'Select Municipios'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search Municipios..." />
<CommandList>
<CommandEmpty>No Municipio found.</CommandEmpty>
<CommandGroup>
{municipios.map((municipio) => (
<CommandItem
key={municipio.id}
value={municipio.name}
onSelect={() => {
setFormField({
...formField,
municipio_id: municipio.id
});
setOpen(false);
}}
>
{municipio.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
</div>
<div className="flex justify-end">
<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>

View File

@ -16,7 +16,7 @@ const ListToolbar = () => {
<KeenIcon icon="magnifier" />
<input
type="text"
placeholder="Search Postu Administrativo"
placeholder="Search Postu"
value={(table.getColumn('posto_adms_name')?.getFilterValue() as string) ?? ''}
onChange={(event) =>
table.getColumn('posto_adms_name')?.setFilterValue(event.target.value)
@ -34,13 +34,13 @@ const ListToolbar = () => {
<KeenIcon icon="filter" />
</Button>
</DefaultTooltip>
<Button
{/* <Button
variant="outline"
className="h-7.5 text-[0.8rem]"
onClick={() => handleSearchDialog(true)}
>
Search Sucos
</Button>
</Button> */}
</div>
<div className="flex gap-3 items-center">
<Button

View File

@ -11,14 +11,16 @@ import { Alert, KeenIcon } from '@/components';
import { Button } from '@/components/ui/button';
import { apiConfig } from '@/config/api.config';
import axios from 'axios';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from '@/components/ui/select';
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList
} from '@/components/ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
interface SucosProps {
id: number;
@ -29,6 +31,7 @@ const API_URL = apiConfig.service_master_data;
const SearchDialog = () => {
const parentRef = useRef<any | null>(null);
const [open, setOpen] = useState(false);
const { showSearchDialog, handleSearchDialog, postoAdms } = useManagePostoAdmsContext();
const [alert, setAlert] = useState({
show: false,
@ -60,6 +63,7 @@ const SearchDialog = () => {
if (response.data.status) {
setSucos(response.data.data);
console.log(sucos);
setIsFound(true);
console.log('Found postoadms: ', response.data.data);
} else {
@ -79,25 +83,22 @@ const SearchDialog = () => {
setIsFound(false);
setSucos([]);
};
// console.log(municipios);
return (
<Dialog open={showSearchDialog} onOpenChange={(open) => handleSearchDialog(open)}>
<Dialog open={showSearchDialog} onOpenChange={handleSearchDialog}>
<DialogContent className="container-fixed max-w-[700px] flex flex-col p-5 overflow-hidden [&>button]:hidden">
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
<DialogHeader className="p-2 border-0">
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
<div className="flex items-center justify-between flex-wrap grow">
<div className="flex flex-col justify-center">
<h1 className="text-xl font-semibold leading-none text-gray-900">
Search Sucos
</h1>
<div className="flex items-center gap-2 text-sm font-normal text-gray-700"></div>
<h1 className="text-xl font-semibold leading-none text-gray-900">Search Sucos</h1>
</div>
<div
className="cursor-pointer hover:opacity-100 opacity-50"
onClick={() => {
handleSearchDialog(false);
resetForm();
handleReset();
}}
>
<KeenIcon icon="cross" className="text-1.5xl" />
@ -105,68 +106,68 @@ const SearchDialog = () => {
</div>
</DialogHeader>
<DialogBody className="scrollable-y px-0 pb-0" ref={parentRef}>
<div className="flex flex-col px-0">
{alert.show && (
<Alert variant="danger" className="mb-5">
{alert.message}
</Alert>
)}
<form action="" onSubmit={handleSubmit}>
<div className="card-body grid-cols-6 gap-5 p-0">
<div className="grid grid-cols-8 gap-2 w-full items-center">
<label className="form-label flex items-center gap-1 col-span-2">
PostoAdms Name<span className="text-red-500">*</span>
</label>
<form onSubmit={handleSubmit} className="flex flex-col px-5 gap-5">
{alert.show && <Alert variant="danger">{alert.message}</Alert>}
<Select
value={formField.id.toString()}
onValueChange={(target) => {
const selectedPostoadms = postoAdms.find((m) => m.posto_adms_id.toString() === target);
if (selectedPostoadms) {
setFormField({
...formField,
id: selectedPostoadms.posto_adms_id,
name: selectedPostoadms.posto_adms_name
});
}
}}
>
<SelectTrigger className="col-span-6">
<SelectValue placeholder="Select Municipios" />
</SelectTrigger>
<SelectContent>
{postoAdms.map((postoAdm) => (
<SelectItem key={postoAdm.posto_adms_id} value={postoAdm.posto_adms_id.toString()}>
{postoAdm.posto_adms_name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="grid grid-cols-8 gap-1 w-full items-center">
<label className="form-label flex items-center col-span-3">
Postu Administrativo Name<span className="text-red-500">*</span>
</label>
{isFound && sucos.length > 0 && (
<div className="mt-4 border-t pt-4">
<h2 className="text-md font-semibold">Sucos: </h2>
<br />
<div className="flex flex-col">
<span className="text-sm form-hint">
{sucos.map((suco) => suco.name).join(', ')}
</span>
</div>
</div>
)}
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className="input col-span-5 text-left">
{formField.name || 'Select PostoAdms'}
</button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput placeholder="Search PostoAdms..." />
<CommandList>
<CommandEmpty>No PostoAdms found.</CommandEmpty>
<CommandGroup>
{postoAdms.map((postoAdm) => (
<CommandItem
key={postoAdm.posto_adms_id}
value={postoAdm.posto_adms_name}
onSelect={() => {
setFormField({
id: postoAdm.posto_adms_id,
name: postoAdm.posto_adms_name
});
setOpen(false);
}}
>
{postoAdm.posto_adms_name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</div>
<div className="flex justify-end pt-2.5 gap-5 col-span-6">
<Button variant={'outline'} type="reset" onClick={handleReset}>
Reset
</Button>
<Button variant={'default'} type="submit">
Search
</Button>
{isFound && sucos.length > 0 && (
<div className="mt-4 border-t pt-4">
<h2 className="text-md font-semibold">Sucos: </h2>
<div className="flex flex-col">
<span className="text-sm form-hint">
{sucos.map((suco) => suco.name).join(', ')}
</span>
</div>
</div>
</form>
</div>
)}
<div className="flex justify-end gap-4">
<Button type="reset" variant="outline" onClick={handleReset}>
Reset
</Button>
<Button type="submit" variant="default">
Search
</Button>
</div>
</form>
</DialogBody>
</DialogContent>
</Dialog>

View File

@ -3,10 +3,10 @@ import { Button } from '@/components/ui/button';
import { Toaster } from '@/components/ui/sonner';
import { apiConfig } from '@/config/api.config';
import { ColumnDef } from '@tanstack/react-table';
import axios from 'axios';
import { createContext, useCallback, useMemo, useState } from 'react';
import { useNavigate, useParams } from 'react-router';
import ListToolbar from '../blocks/ListToolbar';
import { useCallApi } from '@/hooks';
interface PostoAdmsProps {
posto_adms_id: number;
@ -58,6 +58,7 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
const [showEditDialog, setShowEditDialog] = useState(false);
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const [selectedPostoAdms, setSelectedPostoAdms] = useState<string | null>(null);
const { GetData } = useCallApi();
const navigate = useNavigate();
const { municipioId } = useParams();
@ -93,8 +94,9 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
}
},
{
accessorFn: (row) => row.posto_adms_name,
id: 'posto_adms_name',
// accessorFn: (row) => row.posto_adms_name,
// id: 'posto_adms_name',
accessorKey: 'posto_adms_name',
header: ({ column }) => (
<DataGridColumnHeader title="Posto Administrativo Name" column={column} />
),
@ -125,13 +127,13 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
<>
<button
className="btn btn-sm btn-icon btn-clear btn-light"
onClick={() => handleEditDialog(true, row.id)}
onClick={() => handleEditDialog(true, row.posto_adms_id)}
>
<KeenIcon icon="notepad-edit" />
</button>
<button
className="btn btn-sm btn-icon btn-clear btn-light"
onClick={() => handleDeleteDialog(true, row.id)}
onClick={() => handleDeleteDialog(true, row.posto_adms_id)}
>
<KeenIcon icon="trash" />
</button>
@ -151,23 +153,21 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
try {
sorting: sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
const response = await axios.get(`${API_URL}/postoadms/list`, {
params: {
limit,
page: page + 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc ? 'DESC' : 'ASC'
}
const response = await GetData(`${API_URL}/postoadms/list`, {
limit,
page: page + 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc ? 'DESC' : 'ASC'
});
console.log(response.data);
// console.log(response.data);
// const sortedList = response.data.data.list.sort((a: PostoAdmsProps, b: PostoAdmsProps) => {
// if (a.name < b.name) return -1;
// if (a.name > b.name) return 1;
// return 0;
// });
setPostoAdms(response.data.data.list);
return { data: response.data.data.list, totalCount: response.data.data.total_count };
setPostoAdms(response?.data.list);
return { data: response?.data.list, totalCount: response?.data.total_count };
} catch (error) {
console.error('Error fetching Postu Administrativo', error);
}