Merge branch 'master' of https://git.shiblysolution.id/TPAY/dashboard
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,6 +10,7 @@ lerna-debug.log*
|
|||||||
node_modules
|
node_modules
|
||||||
dist
|
dist
|
||||||
dist-ssr
|
dist-ssr
|
||||||
|
yarn.lock
|
||||||
*.local
|
*.local
|
||||||
|
|
||||||
# Editor directories and files
|
# Editor directories and files
|
||||||
|
|||||||
@ -1,10 +1,22 @@
|
|||||||
|
import { Container, DataGridInner } from '@/components';
|
||||||
|
import { ManageAldeiasContextProvider } from './hooks/ManageAldeiasContext';
|
||||||
|
import AddDialog from './blocks/AddDialog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
|
import DeleteDialog from './blocks/DeleteDialog';
|
||||||
|
|
||||||
const AldeiasMaster = () => {
|
const AldeiasMaster = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<ManageAldeiasContextProvider>
|
||||||
<div className="container mx-auto p-5">
|
<Container>
|
||||||
<h1 className="text-xl font-medium leading-none text-gray-900">Aldeias Master Data</h1>
|
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">Aldeias</h1>
|
||||||
</div>
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
</div>
|
<DataGridInner />
|
||||||
|
</div>
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog />
|
||||||
|
<DeleteDialog />
|
||||||
|
</Container>
|
||||||
|
</ManageAldeiasContextProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
152
src/pages/master/aldeias/blocks/AddDialog.tsx
Normal file
152
src/pages/master/aldeias/blocks/AddDialog.tsx
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import React, { useCallback, useEffect, useRef, 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 AddDialog = () => {
|
||||||
|
const parentRef = useRef<any | null>(null);
|
||||||
|
const { showAddDialog, handleAddDialog } = useManageAldeiasContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PostData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
sucos: 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 doCreateAldeias = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PostData(`${API_URL}/aldeias/create`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
resetForm();
|
||||||
|
handleAddDialog(false);
|
||||||
|
toast.success('Success Create Aldeias');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Create Aldeias');
|
||||||
|
setAlert({ show: true, message: 'Failed to create Aldeias. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.sucos === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// doCreateAldeias(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showAddDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
created_by: parsedUser.username,
|
||||||
|
created_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||||
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Aldeias - 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">
|
||||||
|
Sucos ID<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
value={formField.sucos === 0 ? '' : formField.sucos}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value, 10);
|
||||||
|
setFormField({ ...formField, sucos: 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;
|
||||||
74
src/pages/master/aldeias/blocks/DeleteDialog.tsx
Normal file
74
src/pages/master/aldeias/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ChangeEvent, useCallback, useState } from 'react';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@/components/ui/dialog';
|
||||||
|
import { EnforceSwitch } from '@/components/switch';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
const DeleteDialog = () => {
|
||||||
|
const { showDeleteDialog, handleDeleteDialog, selectedAldeias } = useManageAldeiasContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [enforce, setEnforce] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
const doDeleteAldeias = useCallback(async () => {
|
||||||
|
const response = await DeleteData(`${API_URL}/aldeias/delete/${selectedAldeias}/${enforce}`, {
|
||||||
|
id: selectedAldeias
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Aldeias');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Failed Delete Aldeias');
|
||||||
|
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||||
|
}
|
||||||
|
}, [selectedAldeias, enforce]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
|
<DialogHeader className="p-0 border-0 block">
|
||||||
|
<Alert variant="warning">
|
||||||
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
|
<span className="text-sm">you will delete this data!</span>
|
||||||
|
<div className="mt-2 flex items-center gap-x-2">
|
||||||
|
<label className="form-label max-w-56">Hard Delete</label>
|
||||||
|
<EnforceSwitch
|
||||||
|
enforce={enforce}
|
||||||
|
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setEnforce(e.target.checked);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Alert>
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
|
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button variant={'destructive'} onClick={() => doDeleteAldeias()}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
148
src/pages/master/aldeias/blocks/EditDialog.tsx
Normal file
148
src/pages/master/aldeias/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import React, { useCallback, useEffect, 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, selectedAldeias } = useManageAldeiasContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PutData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
sucos: 0,
|
||||||
|
updated_by: '',
|
||||||
|
updated_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 doUpdateAldeias = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PutData(`${API_URL}/aldeias/update/${selectedAldeias}`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
resetForm();
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
toast.success('Success Update Aldeias');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Update Aldeias');
|
||||||
|
setAlert({ show: true, message: 'Error Update Aldeias' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[selectedAldeias, formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.sucos === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// doUpdateAldeias(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showEditDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
updated_by: parsedUser?.username,
|
||||||
|
updated_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
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>Aldeias - Update</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">
|
||||||
|
Sucos ID<span className="text-red-500">*</span>
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="number"
|
||||||
|
min={0}
|
||||||
|
value={formField.sucos === 0 ? '' : formField.sucos}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = parseInt(e.target.value, 10);
|
||||||
|
setFormField({ ...formField, sucos: isNaN(value) ? 0 : value });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button className="btn btn-primary">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditDialog;
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
import { useManageAldeiasContext } from '../hooks/useManageAldeiasContext';
|
||||||
|
|
||||||
|
const ListToolbar = () => {
|
||||||
|
const { table, reload } = useDataGrid();
|
||||||
|
const { handleAddDialog, handleSearchDialog } = useManageAldeiasContext();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card-header flex-wrap gap-2 border-b-0 px-5">
|
||||||
|
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
||||||
|
<div className="flex justify-between w-full items-center">
|
||||||
|
<div className="flex w-[50%] gap-3 items-center">
|
||||||
|
<label className="input input-sm w-1/3">
|
||||||
|
<KeenIcon icon="magnifier" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search Aldeias"
|
||||||
|
value={(table.getColumn('name')?.getFilterValue() as string) ?? ''}
|
||||||
|
onChange={(event) => table.getColumn('name')?.setFilterValue(event.target.value)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<DefaultTooltip title={'Filter'} placement={'top'}>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 disabled:bg-gray-400"
|
||||||
|
// disabled={isLoading}
|
||||||
|
// onClick={handleFilterData}
|
||||||
|
>
|
||||||
|
{/* {loadingButton === 'filter' ? <ContentLoader /> : <KeenIcon icon="filter" />} */}
|
||||||
|
<KeenIcon icon="filter" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleSearchDialog(true)}
|
||||||
|
>
|
||||||
|
Search Sucos
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 items-center">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleAddDialog(true)}
|
||||||
|
>
|
||||||
|
Add Data
|
||||||
|
</Button>
|
||||||
|
<DefaultTooltip title={'Refresh'} placement={'top'}>
|
||||||
|
<Button variant="outline" className="h-7.5" onClick={() => reload()}>
|
||||||
|
<KeenIcon icon="arrows-circle" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListToolbar;
|
||||||
|
|||||||
196
src/pages/master/aldeias/hooks/ManageAldeiasContext.tsx
Normal file
196
src/pages/master/aldeias/hooks/ManageAldeiasContext.tsx
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
|
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||||
|
import { Toaster } from 'sonner';
|
||||||
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
|
||||||
|
interface AldeiasProps {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContextProps {
|
||||||
|
aldeias: AldeiasProps[];
|
||||||
|
showSearchDialog: boolean;
|
||||||
|
handleSearchDialog: (show: boolean) => void;
|
||||||
|
showEditDialog: boolean;
|
||||||
|
handleEditDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
showAddDialog: boolean;
|
||||||
|
handleAddDialog: (show: boolean) => void;
|
||||||
|
showDeleteDialog: boolean;
|
||||||
|
handleDeleteDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
selectedAldeias: string | null;
|
||||||
|
getAldeiasLists: (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: any,
|
||||||
|
order_direction: any
|
||||||
|
) => Promise<{ data: AldeiasProps[]; totalCount: number } | undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialProps: ContextProps = {
|
||||||
|
aldeias: [],
|
||||||
|
showSearchDialog: false,
|
||||||
|
handleSearchDialog: () => {},
|
||||||
|
showEditDialog: false,
|
||||||
|
handleEditDialog: () => {},
|
||||||
|
showAddDialog: false,
|
||||||
|
handleAddDialog: () => {},
|
||||||
|
showDeleteDialog: false,
|
||||||
|
handleDeleteDialog: () => {},
|
||||||
|
selectedAldeias: null,
|
||||||
|
getAldeiasLists: async () => undefined
|
||||||
|
};
|
||||||
|
|
||||||
|
const ManageAldeiasContext = createContext<ContextProps>(initialProps);
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const ManageAldeiasContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
const [aldeias, setAldeias] = useState<AldeiasProps[]>([]);
|
||||||
|
const [showSearchDialog, setShowSearchDialog] = useState(false);
|
||||||
|
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||||
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||||
|
const [selectedAldeias, setSelectedAldeias] = useState<string | null>(null);
|
||||||
|
const { GetData } = useCallApi();
|
||||||
|
|
||||||
|
const handleAddDialog = useCallback((show: boolean) => {
|
||||||
|
setShowAddDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleEditDialog = useCallback((show: boolean, selected_aldeias: string | null) => {
|
||||||
|
setSelectedAldeias(show ? selected_aldeias : null);
|
||||||
|
setShowEditDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDeleteDialog = useCallback((show: boolean, selected_aldeias: string | null) => {
|
||||||
|
setSelectedAldeias(show ? selected_aldeias : null);
|
||||||
|
setShowDeleteDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleSearchDialog = useCallback((show: boolean) => {
|
||||||
|
setShowSearchDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const columns = useMemo<ColumnDef<any>[]>(
|
||||||
|
() => [
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.id,
|
||||||
|
id: 'id',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.name,
|
||||||
|
id: 'name',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Name" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[250px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.sucos.name,
|
||||||
|
id: 'sucos',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Sucos" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[250px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'actions',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
cell: (data) => {
|
||||||
|
const row = data.row.original;
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleEditDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="notepad-edit" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleDeleteDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="trash" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]',
|
||||||
|
cellClassName: 'text-center'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const getAldeiasLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
||||||
|
try {
|
||||||
|
const response = await GetData(`${API_URL}/aldeias/list`, {
|
||||||
|
limit,
|
||||||
|
page: page + 1,
|
||||||
|
with_deleted: true,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
|
||||||
|
filter: JSON.stringify(filter)
|
||||||
|
});
|
||||||
|
console.log(response?.data);
|
||||||
|
setAldeias(response?.data.list);
|
||||||
|
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching Aldeias', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ManageAldeiasContext.Provider
|
||||||
|
value={{
|
||||||
|
aldeias,
|
||||||
|
showSearchDialog,
|
||||||
|
handleSearchDialog,
|
||||||
|
showEditDialog,
|
||||||
|
handleEditDialog,
|
||||||
|
showAddDialog,
|
||||||
|
handleAddDialog,
|
||||||
|
showDeleteDialog,
|
||||||
|
handleDeleteDialog,
|
||||||
|
selectedAldeias,
|
||||||
|
getAldeiasLists
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Toaster expand visibleToasts={9} duration={3000} />
|
||||||
|
<DataGridProvider
|
||||||
|
columns={columns}
|
||||||
|
pagination={{ size: 25 }}
|
||||||
|
toolbar={<ListToolbar />}
|
||||||
|
layout={{ card: true }}
|
||||||
|
sorting={[{ id: 'id', desc: false }]}
|
||||||
|
serverSide={true}
|
||||||
|
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||||
|
getAldeiasLists(pageIndex, pageSize, sorting, columnFilters)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</DataGridProvider>
|
||||||
|
</ManageAldeiasContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { ManageAldeiasContext, ManageAldeiasContextProvider };
|
||||||
|
export type { AldeiasProps };
|
||||||
12
src/pages/master/aldeias/hooks/useManageAldeiasContext.tsx
Normal file
12
src/pages/master/aldeias/hooks/useManageAldeiasContext.tsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { ManageAldeiasContext } from './ManageAldeiasContext';
|
||||||
|
|
||||||
|
const useManageAldeiasContext = () => {
|
||||||
|
const context = useContext(ManageAldeiasContext);
|
||||||
|
|
||||||
|
if (!context) throw new Error('useManageAldeiasContext must be used within AuthProvider');
|
||||||
|
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useManageAldeiasContext };
|
||||||
@ -37,6 +37,9 @@ const AddDialog = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const [formField, setFormField] = useState(initialState);
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
const created_time = new Date();
|
||||||
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setFormField(initialState);
|
setFormField(initialState);
|
||||||
};
|
};
|
||||||
@ -49,8 +52,8 @@ const AddDialog = () => {
|
|||||||
if (response?.status) {
|
if (response?.status) {
|
||||||
handleAddDialog(false);
|
handleAddDialog(false);
|
||||||
resetForm();
|
resetForm();
|
||||||
toast.success('Municipio created successfully!');
|
|
||||||
reload();
|
reload();
|
||||||
|
toast.success('Municipio created successfully!');
|
||||||
// const createActivity = {
|
// const createActivity = {
|
||||||
// module: 'Manage Municipio',
|
// module: 'Manage Municipio',
|
||||||
// description: `Create Municipio => ${selectedMunicipios}`,
|
// description: `Create Municipio => ${selectedMunicipios}`,
|
||||||
@ -80,27 +83,26 @@ const AddDialog = () => {
|
|||||||
// created_at: formattedTime
|
// created_at: formattedTime
|
||||||
// });
|
// });
|
||||||
|
|
||||||
// doCreateMunicipio(e);
|
doCreateMunicipio(e);
|
||||||
console.log(parsedUser.email);
|
console.log(parsedUser.email);
|
||||||
console.log(formField);
|
console.log(formField);
|
||||||
setAlert({ show: false, message: '' });
|
setAlert({ show: false, message: '' });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleReset = () => {
|
const handleReset = () => {
|
||||||
setFormField(initialState);
|
resetForm();
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const created_time = new Date();
|
|
||||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
|
||||||
if (showAddDialog) {
|
if (showAddDialog) {
|
||||||
setFormField({
|
setFormField({
|
||||||
name: formField.name,
|
name: formField.name,
|
||||||
created_by: parsedUser.email,
|
created_by: parsedUser?.username,
|
||||||
created_at: formattedTime
|
created_at: formattedTime
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [showAddDialog]);
|
}, [formattedTime]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||||
|
|||||||
@ -39,6 +39,8 @@ const EditDialog = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const [formField, setFormField] = useState(initialState);
|
const [formField, setFormField] = useState(initialState);
|
||||||
|
const created_time = new Date();
|
||||||
|
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
setFormField(initialState);
|
setFormField(initialState);
|
||||||
@ -96,8 +98,6 @@ const EditDialog = () => {
|
|||||||
// }, []);
|
// }, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const created_time = new Date();
|
|
||||||
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
|
|
||||||
if (selectedMunicipios) {
|
if (selectedMunicipios) {
|
||||||
setFormField({
|
setFormField({
|
||||||
name: formField.name,
|
name: formField.name,
|
||||||
@ -105,7 +105,7 @@ const EditDialog = () => {
|
|||||||
updated_at: formattedTime
|
updated_at: formattedTime
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [selectedMunicipios]);
|
}, [formattedTime]);
|
||||||
|
|
||||||
// console.log(selectedMunicipios);
|
// console.log(selectedMunicipios);
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
import AddDialog from './blocks/AddDialog';
|
||||||
|
import DeleteDialog from './blocks/DeleteDialog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
import SearchDialog from './blocks/SearchDialog';
|
import SearchDialog from './blocks/SearchDialog';
|
||||||
import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext';
|
import { ManagePostoAdmsContextProvider } from './hooks/ManagePostoAdmsContext';
|
||||||
import { Container, DataGridInner } from '@/components';
|
import { Container, DataGridInner } from '@/components';
|
||||||
@ -6,10 +9,15 @@ const PostoAdmsMaster = () => {
|
|||||||
return (
|
return (
|
||||||
<ManagePostoAdmsContextProvider>
|
<ManagePostoAdmsContextProvider>
|
||||||
<Container>
|
<Container>
|
||||||
<h1>Postu Administrativo</h1>
|
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">
|
||||||
|
Postu Administrativo
|
||||||
|
</h1>
|
||||||
<div className="grid gap-5 lg:gap-7.5">
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
<DataGridInner />
|
<DataGridInner />
|
||||||
</div>
|
</div>
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog />
|
||||||
|
<DeleteDialog />
|
||||||
<SearchDialog />
|
<SearchDialog />
|
||||||
</Container>
|
</Container>
|
||||||
</ManagePostoAdmsContextProvider>
|
</ManagePostoAdmsContextProvider>
|
||||||
|
|||||||
155
src/pages/master/postoadms/blocks/AddDialog.tsx
Normal file
155
src/pages/master/postoadms/blocks/AddDialog.tsx
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
import { Alert, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth, useAuthContext } 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 } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PostData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
municipio_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 doCreatePostoAdm = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PostData(`${API_URL}/postoadms/create`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
handleAddDialog(false);
|
||||||
|
resetForm();
|
||||||
|
reload();
|
||||||
|
toast.success('Posto Adm created successfully!');
|
||||||
|
} else {
|
||||||
|
toast.error('Failed to create Posto Adm. Please try again.');
|
||||||
|
setAlert({ show: true, message: 'Failed to create Posto Adm. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.municipio_id === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doCreatePostoAdm(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showAddDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
created_by: parsedUser?.username,
|
||||||
|
created_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
|
||||||
|
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Postu Administrativo - 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">
|
||||||
|
Municipio ID<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 });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</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;
|
||||||
77
src/pages/master/postoadms/blocks/DeleteDialog.tsx
Normal file
77
src/pages/master/postoadms/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ChangeEvent, useCallback, useState } from 'react';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@/components/ui/dialog';
|
||||||
|
import { EnforceSwitch } from '@/components/switch';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const DeleteDialog = () => {
|
||||||
|
const { showDeleteDialog, handleDeleteDialog, selectedPostoAdms } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [enforce, setEnforce] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const doDeletePostoAdm = useCallback(async () => {
|
||||||
|
const response = await DeleteData(
|
||||||
|
`${API_URL}/postoadms/delete/${selectedPostoAdms}/${enforce}`,
|
||||||
|
{
|
||||||
|
id: selectedPostoAdms
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Posto Adm');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||||
|
}
|
||||||
|
}, [selectedPostoAdms, enforce]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
|
<DialogHeader className="p-0 border-0 block">
|
||||||
|
<Alert variant="warning">
|
||||||
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
|
<span className="text-sm">you will delete this data!</span>
|
||||||
|
<div className="mt-2 flex items-center gap-x-2">
|
||||||
|
<label className="form-label max-w-56">Hard Delete</label>
|
||||||
|
<EnforceSwitch
|
||||||
|
enforce={enforce}
|
||||||
|
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setEnforce(e.target.checked);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Alert>
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
|
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button variant={'destructive'} onClick={() => doDeletePostoAdm()}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
151
src/pages/master/postoadms/blocks/EditDialog.tsx
Normal file
151
src/pages/master/postoadms/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
import { useManagePostoAdmsContext } from '../hooks/useManagePostoAdmsContext';
|
||||||
|
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 EditDialog = () => {
|
||||||
|
const parentRef = useRef<any | null>(null);
|
||||||
|
const { showEditDialog, handleEditDialog, selectedPostoAdms } = useManagePostoAdmsContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PutData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
municipio_id: 0,
|
||||||
|
updated_by: '',
|
||||||
|
updated_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 doUpdatePostoAdm = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PutData(`${API_URL}/postoadms/update/${selectedPostoAdms}`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
resetForm();
|
||||||
|
toast.success('Success Update Posto Adm');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Error Update Posto Adm');
|
||||||
|
setAlert({ show: true, message: 'Failed to update posto adm. Please try again.' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[selectedPostoAdms, formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.municipio_id === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doUpdatePostoAdm(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedPostoAdms) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
updated_by: parsedUser?.username,
|
||||||
|
updated_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
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>Posto Adm - Update</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">
|
||||||
|
Municipio ID<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 });
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button className="btn btn-primary">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditDialog;
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import { DataGridColumnHeader, DataGridProvider } from '@/components';
|
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Toaster } from '@/components/ui/sonner';
|
import { Toaster } from '@/components/ui/sonner';
|
||||||
import { apiConfig } from '@/config/api.config';
|
import { apiConfig } from '@/config/api.config';
|
||||||
@ -106,18 +106,29 @@ const ManagePostoAdmsContextProvider = ({ children }: { children: React.ReactNod
|
|||||||
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
enableSorting: false,
|
enableSorting: false,
|
||||||
enableHiding: false,
|
enableHiding: false,
|
||||||
meta: {
|
cell: (data) => {
|
||||||
headerClassName: 'w-[100px], text-center',
|
const row = data.row.original;
|
||||||
cellClassName: 'text-center'
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleEditDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="notepad-edit" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleDeleteDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="trash" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
},
|
},
|
||||||
cell: (info) => (
|
meta: {
|
||||||
<Button
|
headerClassName: 'w-[100px]',
|
||||||
variant={'outline'}
|
cellClassName: 'text-center'
|
||||||
onClick={() => navigate(`/master-data/municipios/postoadms/${info.row.original.id}`)}
|
}
|
||||||
>
|
|
||||||
Details
|
|
||||||
</Button>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
[handleEditDialog, handleDeleteDialog]
|
[handleEditDialog, handleDeleteDialog]
|
||||||
|
|||||||
@ -1,10 +1,20 @@
|
|||||||
|
import { Container, DataGridInner } from '@/components';
|
||||||
|
import { ManageSucosContextProvider } from './hooks/ManageSucosContext';
|
||||||
|
import AddDialog from './blocks/AddDialog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
|
|
||||||
const SucosMaster = () => {
|
const SucosMaster = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<ManageSucosContextProvider>
|
||||||
<div className="container mx-auto p-5">
|
<Container>
|
||||||
<h1 className="text-xl font-medium leading-none text-gray-900">Sucos Master Data</h1>
|
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">Sucos</h1>
|
||||||
</div>
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
</div>
|
<DataGridInner />
|
||||||
|
</div>
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog />
|
||||||
|
</Container>
|
||||||
|
</ManageSucosContextProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
149
src/pages/master/sucos/blocks/AddDialog.tsx
Normal file
149
src/pages/master/sucos/blocks/AddDialog.tsx
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
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 === '' || 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]);
|
||||||
|
|
||||||
|
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;
|
||||||
74
src/pages/master/sucos/blocks/DeleteDialog.tsx
Normal file
74
src/pages/master/sucos/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useManageSucosContext } from '../hooks/useManageSucosContext';
|
||||||
|
import { ChangeEvent, useCallback, useState } from 'react';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import { Dialog, DialogContent, DialogFooter, DialogHeader } from '@/components/ui/dialog';
|
||||||
|
import { EnforceSwitch } from '@/components/switch';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const DeleteDialog = () => {
|
||||||
|
const { showDeleteDialog, handleDeleteDialog, selectedSucos } = useManageSucosContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [enforce, setEnforce] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const doDeleteSucos = useCallback(async () => {
|
||||||
|
const response = await DeleteData(`${API_URL}/sucos/delete/${selectedSucos}/${enforce}`, {
|
||||||
|
id: selectedSucos
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Sucos');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||||
|
}
|
||||||
|
}, [selectedSucos, enforce]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
|
<DialogHeader className="p-0 border-0 block">
|
||||||
|
<Alert variant="warning">
|
||||||
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
|
<span className="text-sm">you will delete this data!</span>
|
||||||
|
<div className="mt-2 flex items-center gap-x-2">
|
||||||
|
<label className="form-label max-w-56">Hard Delete</label>
|
||||||
|
<EnforceSwitch
|
||||||
|
enforce={enforce}
|
||||||
|
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setEnforce(e.target.checked);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Alert>
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
|
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button variant={'destructive'} onClick={() => doDeleteSucos()}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
153
src/pages/master/sucos/blocks/EditDialog.tsx
Normal file
153
src/pages/master/sucos/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useManageSucosContext } from '../hooks/useManageSucosContext';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { getAuth } from '@/auth';
|
||||||
|
import React, { useCallback, useEffect, useState } from 'react';
|
||||||
|
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 EditDialog = () => {
|
||||||
|
const { showEditDialog, handleEditDialog, selectedSucos } = useManageSucosContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PutData } = useCallApi();
|
||||||
|
const parsedUser = getAuth()?.user;
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
const initialState = {
|
||||||
|
name: '',
|
||||||
|
posto_adm_id: 0,
|
||||||
|
updated_by: '',
|
||||||
|
updated_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 doUpdateSucos = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PutData(`${API_URL}/sucos/update/${selectedSucos}`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
resetForm();
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
toast.success('Success Update Sucos');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Failed Update Sucos');
|
||||||
|
setAlert({ show: true, message: 'Failed Update Sucos. Please try again' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[selectedSucos, formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
if (formField.name === '' || formField.posto_adm_id === 0) {
|
||||||
|
setAlert({ show: true, message: 'Please fill in all required fields.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// doUpdateSucos(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (showEditDialog) {
|
||||||
|
setFormField({
|
||||||
|
...formField,
|
||||||
|
updated_by: parsedUser.username,
|
||||||
|
updated_at: formattedTime
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [formattedTime]);
|
||||||
|
|
||||||
|
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>Sucos - Update</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">
|
||||||
|
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">
|
||||||
|
<Button className="btn btn-primary">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditDialog;
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
import { useManageSucosContext } from '../hooks/useManageSucosContext';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const ListToolbar = () => {
|
||||||
|
const { table, reload } = useDataGrid();
|
||||||
|
const { handleAddDialog, handleSearchDialog } = useManageSucosContext();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card-header flex-wrap gap-2 border-b-0 px-5">
|
||||||
|
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
||||||
|
<div className="flex justify-between w-full items-center">
|
||||||
|
<div className="flex w-[50%] gap-3 items-center">
|
||||||
|
<label className="input input-sm w-1/3">
|
||||||
|
<KeenIcon icon="magnifier" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search Sucos"
|
||||||
|
value={(table.getColumn('sucos_name')?.getFilterValue() as string) ?? ''}
|
||||||
|
onChange={(event) =>
|
||||||
|
table.getColumn('sucos_name')?.setFilterValue(event.target.value)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<DefaultTooltip title={'Filter'} placement={'top'}>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 disabled:bg-gray-400"
|
||||||
|
// disabled={isLoading}
|
||||||
|
// onClick={handleFilterData}
|
||||||
|
>
|
||||||
|
{/* {loadingButton === 'filter' ? <ContentLoader /> : <KeenIcon icon="filter" />} */}
|
||||||
|
<KeenIcon icon="filter" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleSearchDialog(true)}
|
||||||
|
>
|
||||||
|
Search Sucos
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 items-center">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleAddDialog(true)}
|
||||||
|
>
|
||||||
|
Add Data
|
||||||
|
</Button>
|
||||||
|
<DefaultTooltip title={'Refresh'} placement={'top'}>
|
||||||
|
<Button variant="outline" className="h-7.5" onClick={() => reload()}>
|
||||||
|
<KeenIcon icon="arrows-circle" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListToolbar;
|
||||||
|
|||||||
200
src/pages/master/sucos/hooks/ManageSucosContext.tsx
Normal file
200
src/pages/master/sucos/hooks/ManageSucosContext.tsx
Normal file
@ -0,0 +1,200 @@
|
|||||||
|
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||||
|
import { Toaster } from '@/components/ui/sonner';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
|
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||||
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
|
||||||
|
interface SucosProps {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContextProps {
|
||||||
|
sucos: SucosProps[];
|
||||||
|
showSearchDialog: boolean;
|
||||||
|
handleSearchDialog: (show: boolean) => void;
|
||||||
|
showEditDialog: boolean;
|
||||||
|
handleEditDialog: (show: boolean, selected_sucos: string | null) => void;
|
||||||
|
showAddDialog: boolean;
|
||||||
|
handleAddDialog: (show: boolean) => void;
|
||||||
|
showDeleteDialog: boolean;
|
||||||
|
handleDeleteDialog: (show: boolean, selected_sucos: string | null) => void;
|
||||||
|
selectedSucos: string | null;
|
||||||
|
getSucosLists: (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: any,
|
||||||
|
order_direction: any
|
||||||
|
) => Promise<{ data: SucosProps[]; totalCount: number } | undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialProps: ContextProps = {
|
||||||
|
sucos: [],
|
||||||
|
showSearchDialog: false,
|
||||||
|
handleSearchDialog: () => {},
|
||||||
|
showEditDialog: false,
|
||||||
|
handleEditDialog: () => {},
|
||||||
|
showAddDialog: false,
|
||||||
|
handleAddDialog: () => {},
|
||||||
|
showDeleteDialog: false,
|
||||||
|
handleDeleteDialog: () => {},
|
||||||
|
selectedSucos: null,
|
||||||
|
getSucosLists: async () => undefined
|
||||||
|
};
|
||||||
|
|
||||||
|
const ManageSucosContext = createContext<ContextProps>(initialProps);
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const ManageSucosContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
const [sucos, setSucos] = useState<SucosProps[]>([]);
|
||||||
|
const [showSearchDialog, setShowSearchDialog] = useState(false);
|
||||||
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
|
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||||
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||||
|
const [selectedSucos, setSelectedSucos] = useState<string | null>(null);
|
||||||
|
const { GetData } = useCallApi();
|
||||||
|
|
||||||
|
const handleSearchDialog = useCallback((show: boolean) => {
|
||||||
|
setShowSearchDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleAddDialog = useCallback((show: boolean) => {
|
||||||
|
setShowAddDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleEditDialog = useCallback((show: boolean, selected_sucos: string | null) => {
|
||||||
|
setSelectedSucos(show ? selected_sucos : null);
|
||||||
|
setShowEditDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDeleteDialog = useCallback((show: boolean, selected_sucos: string | null) => {
|
||||||
|
setSelectedSucos(show ? selected_sucos : null);
|
||||||
|
setShowDeleteDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const columns = useMemo<ColumnDef<any>[]>(
|
||||||
|
() => [
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.sucos_id,
|
||||||
|
id: 'id',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="ID" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.sucos_name,
|
||||||
|
id: 'sucos_name',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Sucos Name" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[250px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.posto_name,
|
||||||
|
id: 'posto_name',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Posto Name" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[250px]'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'actions',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
cell: (data) => {
|
||||||
|
const row = data.row.original;
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleEditDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="notepad-edit" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleDeleteDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="trash" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]',
|
||||||
|
cellClassName: 'text-center'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[handleEditDialog, handleDeleteDialog]
|
||||||
|
);
|
||||||
|
|
||||||
|
const getSucosLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
||||||
|
try {
|
||||||
|
sorting: sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||||
|
filter = filter.length == 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
||||||
|
const response = await GetData(`${API_URL}/sucos/list`, {
|
||||||
|
limit,
|
||||||
|
page: page + 1,
|
||||||
|
with_deleted: true,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
|
||||||
|
filter: JSON.stringify(filter)
|
||||||
|
});
|
||||||
|
console.log(response?.data);
|
||||||
|
setSucos(response?.data.list);
|
||||||
|
// console.log(sucos);
|
||||||
|
return { data: response?.data.list, totalCount: response?.data.total_count };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching Sucos', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ManageSucosContext.Provider
|
||||||
|
value={{
|
||||||
|
sucos,
|
||||||
|
showSearchDialog,
|
||||||
|
handleSearchDialog,
|
||||||
|
showEditDialog,
|
||||||
|
handleEditDialog,
|
||||||
|
showAddDialog,
|
||||||
|
handleAddDialog,
|
||||||
|
showDeleteDialog,
|
||||||
|
handleDeleteDialog,
|
||||||
|
selectedSucos,
|
||||||
|
getSucosLists
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Toaster expand visibleToasts={9} duration={3000} />
|
||||||
|
|
||||||
|
<DataGridProvider
|
||||||
|
columns={columns}
|
||||||
|
pagination={{ size: 25 }}
|
||||||
|
toolbar={<ListToolbar />}
|
||||||
|
layout={{ card: true }}
|
||||||
|
sorting={[{ id: 'id', desc: false }]}
|
||||||
|
serverSide={true}
|
||||||
|
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||||
|
getSucosLists(pageIndex, pageSize, sorting, columnFilters)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</DataGridProvider>
|
||||||
|
</ManageSucosContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { ManageSucosContextProvider, ManageSucosContext };
|
||||||
|
export type { SucosProps };
|
||||||
12
src/pages/master/sucos/hooks/useManageSucosContext.tsx
Normal file
12
src/pages/master/sucos/hooks/useManageSucosContext.tsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { ManageSucosContext } from './ManageSucosContext';
|
||||||
|
|
||||||
|
const useManageSucosContext = () => {
|
||||||
|
const context = useContext(ManageSucosContext);
|
||||||
|
|
||||||
|
if (!context) throw new Error('useManageSucosContext must be used within AuthProvider');
|
||||||
|
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useManageSucosContext };
|
||||||
@ -1,10 +1,22 @@
|
|||||||
|
import { Container, DataGridInner } from '@/components';
|
||||||
|
import { ManageMenusContextProvider } from './hooks/ManageMenusContext';
|
||||||
|
import AddDialog from './blocks/AddDIalog';
|
||||||
|
import EditDialog from './blocks/EditDialog';
|
||||||
|
import DeleteDialog from './blocks/DeleteDialog';
|
||||||
|
|
||||||
const ManageMenu = () => {
|
const ManageMenu = () => {
|
||||||
return (
|
return (
|
||||||
<div>
|
<ManageMenusContextProvider>
|
||||||
<div className="container mx-auto p-5">
|
<Container>
|
||||||
<h1 className="text-xl font-medium leading-none text-gray-900">Manage Menu</h1>
|
<h1 className="text-xl font-medium leading-none text-gray-900 mb-5">Manage Menus</h1>
|
||||||
</div>
|
<div className="grid gap-5 lg:gap-7.5">
|
||||||
</div>
|
<DataGridInner />
|
||||||
|
</div>
|
||||||
|
<AddDialog />
|
||||||
|
<EditDialog />
|
||||||
|
<DeleteDialog />
|
||||||
|
</Container>
|
||||||
|
</ManageMenusContextProvider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
212
src/pages/menu/manage-menu/blocks/AddDIalog.tsx
Normal file
212
src/pages/menu/manage-menu/blocks/AddDIalog.tsx
Normal file
@ -0,0 +1,212 @@
|
|||||||
|
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 { 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 } = 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 doCreateMenu = useCallback(
|
||||||
|
async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
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' });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[formField]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSubmit = (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;
|
||||||
|
}
|
||||||
|
|
||||||
|
doCreateMenu(e);
|
||||||
|
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">ID Parent</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="text"
|
||||||
|
value={formField.id_parent}
|
||||||
|
onChange={(e) => setFormField({ ...formField, id_parent: 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">
|
||||||
|
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>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="text"
|
||||||
|
value={formField.status}
|
||||||
|
onChange={(e) => setFormField({ ...formField, status: e.target.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;
|
||||||
76
src/pages/menu/manage-menu/blocks/DeleteDialog.tsx
Normal file
76
src/pages/menu/manage-menu/blocks/DeleteDialog.tsx
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useManageMenusContext } from '../hooks/useManageMenusContext';
|
||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ChangeEvent, useCallback, useState } from 'react';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||||
|
import { EnforceSwitch } from '@/components/switch';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const API_URL = apiConfig.service_dashboard;
|
||||||
|
const DeleteDialog = () => {
|
||||||
|
const { showDeleteDialog, handleDeleteDialog, selectedMenu } = useManageMenusContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { DeleteData } = useCallApi();
|
||||||
|
const [enforce, setEnforce] = useState(false);
|
||||||
|
const [alert, setAlert] = useState({
|
||||||
|
show: false,
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
const doDeleteMenu = useCallback(async () => {
|
||||||
|
const response = await DeleteData(`${API_URL}/menus/delete/${selectedMenu}/${enforce}`, {
|
||||||
|
id: selectedMenu
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
setAlert((prev) => ({ ...prev, show: false, message: '' }));
|
||||||
|
handleDeleteDialog(false, null);
|
||||||
|
toast.success('Success Delete Menu');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Failed Delete Menu');
|
||||||
|
setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
|
||||||
|
}
|
||||||
|
}, [selectedMenu, enforce]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={showDeleteDialog} onOpenChange={(open) => handleDeleteDialog(open, null)}>
|
||||||
|
<DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
|
||||||
|
<DialogHeader className="p-0 border-0 block">
|
||||||
|
<DialogTitle></DialogTitle>
|
||||||
|
<DialogDescription></DialogDescription>
|
||||||
|
<Alert variant="warning">
|
||||||
|
<h3 className="text-lg">Are you sure?</h3>
|
||||||
|
<span className="text-sm">you will delete this data!</span>
|
||||||
|
<div className="mt-2 flex items-center gap-x-2">
|
||||||
|
<label className="form-label max-w-56">Hard Delete</label>
|
||||||
|
<EnforceSwitch
|
||||||
|
enforce={enforce}
|
||||||
|
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setEnforce(e.target.checked);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Alert>
|
||||||
|
{alert.show && (
|
||||||
|
<Alert variant="danger">
|
||||||
|
<h3>{alert.message}</h3>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter className="flex justify-end items-center gap-4 mt-3">
|
||||||
|
<Button variant={'outline'} onClick={() => handleDeleteDialog(false, null)}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button variant={'destructive'} onClick={() => doDeleteMenu()}>
|
||||||
|
Delete
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DeleteDialog;
|
||||||
205
src/pages/menu/manage-menu/blocks/EditDialog.tsx
Normal file
205
src/pages/menu/manage-menu/blocks/EditDialog.tsx
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
import { Alert, useDataGrid } from '@/components';
|
||||||
|
import { useManageMenusContext } from '../hooks/useManageMenusContext';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
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_dashboard;
|
||||||
|
const EditDialog = () => {
|
||||||
|
const { showEditDialog, handleEditDialog, selectedMenu } = useManageMenusContext();
|
||||||
|
const { reload } = useDataGrid();
|
||||||
|
const { PutData } = 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 doUpdateMenu = useCallback(async (e: React.FormEvent<HTMLFormElement>) => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const response = await PutData(`${API_URL}/menus/update/${selectedMenu}`, formField);
|
||||||
|
|
||||||
|
if (response?.status) {
|
||||||
|
handleEditDialog(false, null);
|
||||||
|
resetForm();
|
||||||
|
toast.success('Success Update Menu');
|
||||||
|
reload();
|
||||||
|
} else {
|
||||||
|
toast.error('Failed Update Menu');
|
||||||
|
setAlert({ show: true, message: 'Failed Update Menu' });
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleUpdate = (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;
|
||||||
|
}
|
||||||
|
|
||||||
|
doUpdateMenu(e);
|
||||||
|
console.log(formField);
|
||||||
|
setAlert({ show: false, message: '' });
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setFormField(initialState);
|
||||||
|
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>Menu - Update</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">
|
||||||
|
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">ID Parent</label>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="text"
|
||||||
|
value={formField.id_parent}
|
||||||
|
onChange={(e) => setFormField({ ...formField, id_parent: 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">
|
||||||
|
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>
|
||||||
|
<Input
|
||||||
|
className="input"
|
||||||
|
type="text"
|
||||||
|
value={formField.status}
|
||||||
|
onChange={(e) => setFormField({ ...formField, status: e.target.value })}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button className="btn btn-primary">Save Changes</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</DialogBody>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditDialog;
|
||||||
55
src/pages/menu/manage-menu/blocks/ListToolbar.tsx
Normal file
55
src/pages/menu/manage-menu/blocks/ListToolbar.tsx
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import { DefaultTooltip, KeenIcon, useDataGrid } from '@/components';
|
||||||
|
import { useManageMenusContext } from '../hooks/useManageMenusContext';
|
||||||
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
||||||
|
const ListToolbar = () => {
|
||||||
|
const { table, reload } = useDataGrid();
|
||||||
|
const { handleAddDialog } = useManageMenusContext();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="card-header flex-wrap gap-2 border-b-0 px-5">
|
||||||
|
<div className="flex flex-wrap gap-2 lg:gap-5 w-full">
|
||||||
|
<div className="flex justify-between w-full items-center">
|
||||||
|
<div className="flex w-[50%] gap-3 items-center">
|
||||||
|
<label className="input input-sm w-1/3">
|
||||||
|
<KeenIcon icon="magnifier" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search Menu"
|
||||||
|
value={(table.getColumn('subMenu')?.getFilterValue() as string) ?? ''}
|
||||||
|
onChange={(event) => table.getColumn('subMenu')?.setFilterValue(event.target.value)}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<DefaultTooltip title={'Filter'} placement={'top'}>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 disabled:bg-gray-400"
|
||||||
|
// disabled={isLoading}
|
||||||
|
// onClick={handleFilterData}
|
||||||
|
>
|
||||||
|
{/* {loadingButton === 'filter' ? <ContentLoader /> : <KeenIcon icon="filter" />} */}
|
||||||
|
<KeenIcon icon="filter" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 items-center">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
className="h-7.5 text-[0.8rem]"
|
||||||
|
onClick={() => handleAddDialog(true)}
|
||||||
|
>
|
||||||
|
Add Data
|
||||||
|
</Button>
|
||||||
|
<DefaultTooltip title={'Refresh'} placement={'top'}>
|
||||||
|
<Button variant="outline" className="h-7.5" onClick={() => reload()}>
|
||||||
|
<KeenIcon icon="arrows-circle" />
|
||||||
|
</Button>
|
||||||
|
</DefaultTooltip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListToolbar;
|
||||||
250
src/pages/menu/manage-menu/hooks/ManageMenusContext.tsx
Normal file
250
src/pages/menu/manage-menu/hooks/ManageMenusContext.tsx
Normal file
@ -0,0 +1,250 @@
|
|||||||
|
// interface SelectedMenu {
|
||||||
|
// id: string;
|
||||||
|
// module: string;
|
||||||
|
// name: string;
|
||||||
|
// id_parent: string;
|
||||||
|
// order_number: number;
|
||||||
|
// icon: string;
|
||||||
|
// application: string;
|
||||||
|
// status: string;
|
||||||
|
// }
|
||||||
|
|
||||||
|
import { DataGridColumnHeader, DataGridProvider, KeenIcon } from '@/components';
|
||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import { useCallApi } from '@/hooks';
|
||||||
|
import { ColumnDef } from '@tanstack/react-table';
|
||||||
|
import React, { createContext, useCallback, useMemo, useState } from 'react';
|
||||||
|
import { Toaster } from 'sonner';
|
||||||
|
import ListToolbar from '../blocks/ListToolbar';
|
||||||
|
|
||||||
|
interface MenuProps {
|
||||||
|
id: string;
|
||||||
|
module: string;
|
||||||
|
name: string;
|
||||||
|
id_parent: string;
|
||||||
|
order_number: number;
|
||||||
|
icon: string;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContextProps {
|
||||||
|
menus: MenuProps[];
|
||||||
|
showEditDialog: boolean;
|
||||||
|
handleEditDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
showAddDialog: boolean;
|
||||||
|
handleAddDialog: (show: boolean) => void;
|
||||||
|
showDeleteDialog: boolean;
|
||||||
|
handleDeleteDialog: (show: boolean, selected_postoAdms: string | null) => void;
|
||||||
|
selectedMenu: string | null;
|
||||||
|
getMenusLists: (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: any,
|
||||||
|
order_direction: any
|
||||||
|
) => Promise<{ data: MenuProps[]; totalCount: number } | undefined>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialProps: ContextProps = {
|
||||||
|
menus: [],
|
||||||
|
showAddDialog: false,
|
||||||
|
handleAddDialog: (show: boolean) => {},
|
||||||
|
showEditDialog: false,
|
||||||
|
handleEditDialog: (show: boolean, selected_menu: string | null) => {},
|
||||||
|
showDeleteDialog: false,
|
||||||
|
handleDeleteDialog: (show: boolean, selected_menu: string | null) => {},
|
||||||
|
selectedMenu: null,
|
||||||
|
getMenusLists: async () => ({ data: [], totalCount: 0 })
|
||||||
|
};
|
||||||
|
|
||||||
|
const ManageMenusContext = createContext<ContextProps>(initialProps);
|
||||||
|
const API_URL = apiConfig.service_dashboard;
|
||||||
|
|
||||||
|
const ManageMenusContextProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
const [menus, setMenus] = useState<MenuProps[]>([]);
|
||||||
|
const [showAddDialog, setShowAddDialog] = useState(false);
|
||||||
|
const [showEditDialog, setShowEditDialog] = useState(false);
|
||||||
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||||
|
const [selectedMenu, setSelectedMenu] = useState<string | null>(null);
|
||||||
|
const { GetData } = useCallApi();
|
||||||
|
|
||||||
|
const handleAddDialog = useCallback((show: boolean) => {
|
||||||
|
setShowAddDialog(show);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleEditDialog = useCallback((show: boolean, selected_menu: string | null) => {
|
||||||
|
setShowEditDialog(show);
|
||||||
|
setSelectedMenu(selected_menu);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleDeleteDialog = useCallback((show: boolean, selected_menu: string | null) => {
|
||||||
|
setShowDeleteDialog(show);
|
||||||
|
setSelectedMenu(selected_menu);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const columns = useMemo<ColumnDef<any>[]>(
|
||||||
|
() => [
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.module,
|
||||||
|
id: 'module',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Module" column={column} />,
|
||||||
|
enableSorting: true,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[200px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.parentName,
|
||||||
|
id: 'menu',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Menu" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[200px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.name,
|
||||||
|
id: 'subMenu',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Sub Menu" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[200px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.link,
|
||||||
|
id: 'link',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="URL" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[250px]' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accessorFn: (row) => row.status,
|
||||||
|
id: 'status',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Status" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
meta: { headerClassName: 'w-[100px]', cellClassName: 'text-center' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'actions',
|
||||||
|
header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
|
||||||
|
enableSorting: false,
|
||||||
|
enableHiding: false,
|
||||||
|
cell: (data) => {
|
||||||
|
const row = data.row.original;
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleEditDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="notepad-edit" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="btn btn-sm btn-icon btn-clear btn-light"
|
||||||
|
onClick={() => handleDeleteDialog(true, row.id)}
|
||||||
|
>
|
||||||
|
<KeenIcon icon="trash" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
meta: {
|
||||||
|
headerClassName: 'w-[100px]',
|
||||||
|
cellClassName: 'text-center'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[handleEditDialog, handleDeleteDialog]
|
||||||
|
);
|
||||||
|
|
||||||
|
const flattenChildren = (parent: any, parentIdx: number, depth = 0, parentName = '') => {
|
||||||
|
if (!parent.children || parent.children.length === 0) {
|
||||||
|
return []; // Jika tidak ada children, kembalikan array kosong
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent.children.flatMap((child: any, childIdx: number) => {
|
||||||
|
// Jika child masih punya children, lakukan rekursi lebih dalam
|
||||||
|
if (child.children && child.children.length > 0) {
|
||||||
|
return flattenChildren(child, parentIdx * 100 + childIdx, depth + 1, child.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jika ini adalah child terakhir (leaf node), masukkan ke array hasil
|
||||||
|
return {
|
||||||
|
id: parentIdx * 100 + childIdx + 1,
|
||||||
|
module: parent.module,
|
||||||
|
parentName: parentName || parent.name,
|
||||||
|
name: child.name,
|
||||||
|
link: child.link,
|
||||||
|
id_parent: parent.id_parent,
|
||||||
|
status: parent.status
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getMenusLists = async (page: number, limit: number, sorting: any, filter: any) => {
|
||||||
|
try {
|
||||||
|
sorting = sorting.length === 0 ? [{ id: 'name', desc: false }] : sorting;
|
||||||
|
filter = filter.length === 0 ? {} : { any: filter[0].value?.toLowerCase() };
|
||||||
|
|
||||||
|
const response = await GetData(`${API_URL}/menus/list`, {
|
||||||
|
limit,
|
||||||
|
page: page + 1,
|
||||||
|
with_deleted: false,
|
||||||
|
order_field: sorting[0].id,
|
||||||
|
order_direction: sorting[0].desc ? 'DESC' : 'ASC'
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(response?.data);
|
||||||
|
if (!response?.data.list) return { data: [], totalCount: 0 };
|
||||||
|
|
||||||
|
// Gunakan rekursi untuk mencari children paling dalam
|
||||||
|
const transformedData = response.data.list.flatMap((row: any, parentIdx: number) =>
|
||||||
|
flattenChildren(row, parentIdx)
|
||||||
|
);
|
||||||
|
|
||||||
|
const total_count = transformedData.length;
|
||||||
|
|
||||||
|
setMenus(transformedData);
|
||||||
|
console.log(menus);
|
||||||
|
return { data: transformedData, totalCount: total_count };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching Menus', error);
|
||||||
|
return { data: [], totalCount: 0 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ManageMenusContext.Provider
|
||||||
|
value={{
|
||||||
|
menus,
|
||||||
|
showAddDialog,
|
||||||
|
handleAddDialog,
|
||||||
|
showEditDialog,
|
||||||
|
handleEditDialog,
|
||||||
|
showDeleteDialog,
|
||||||
|
handleDeleteDialog,
|
||||||
|
selectedMenu,
|
||||||
|
getMenusLists
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Toaster expand visibleToasts={9} duration={3000} />
|
||||||
|
|
||||||
|
<DataGridProvider
|
||||||
|
columns={columns}
|
||||||
|
pagination={{ size: 25 }}
|
||||||
|
toolbar={<ListToolbar />}
|
||||||
|
layout={{ card: true }}
|
||||||
|
sorting={[{ id: 'id', desc: false }]}
|
||||||
|
serverSide={true}
|
||||||
|
onFetchData={({ pageIndex, pageSize, sorting, columnFilters }) =>
|
||||||
|
getMenusLists(pageIndex, pageSize, sorting, columnFilters)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</DataGridProvider>
|
||||||
|
</ManageMenusContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { ManageMenusContextProvider, ManageMenusContext };
|
||||||
|
export type { MenuProps };
|
||||||
12
src/pages/menu/manage-menu/hooks/useManageMenusContext.tsx
Normal file
12
src/pages/menu/manage-menu/hooks/useManageMenusContext.tsx
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { ManageMenusContext } from './ManageMenusContext';
|
||||||
|
|
||||||
|
const useManageMenusContext = () => {
|
||||||
|
const context = useContext(ManageMenusContext);
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('useManageMenusContext must be used within a ManageMenusContextProvider');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useManageMenusContext };
|
||||||
Reference in New Issue
Block a user