fetch get data Municipios
This commit is contained in:
34
src/pages/master/municipios/MunicipiosContent.tsx
Normal file
34
src/pages/master/municipios/MunicipiosContent.tsx
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { ManageMunicipiosProvider } from './hooks/ManageMunicipiosContext';
|
||||||
|
import { useManageMunicipiosContext } from './hooks/useManageMunicipiosContext';
|
||||||
|
|
||||||
|
const MunicipiosContent = () => {
|
||||||
|
const {
|
||||||
|
municipios,
|
||||||
|
getMunicipiosLists,
|
||||||
|
getMunicipiosByName,
|
||||||
|
createMunicipios,
|
||||||
|
deleteMunicipios,
|
||||||
|
updateMunicipios,
|
||||||
|
restoreMunicipios
|
||||||
|
} = useManageMunicipiosContext();
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Municipios</h1>
|
||||||
|
<ul>
|
||||||
|
{/* {municipios.map((municipio) => (
|
||||||
|
<li key={municipio.id}>
|
||||||
|
{municipio.name}
|
||||||
|
<button onClick={() => updateMunicipios(municipio.id, { name: 'Updated Name' })}>
|
||||||
|
Update
|
||||||
|
</button>
|
||||||
|
<button onClick={() => deleteMunicipios(municipio.id)}>Delete</button>
|
||||||
|
<button onClick={() => restoreMunicipios(municipio.id)}>Restore</button>
|
||||||
|
</li>
|
||||||
|
))} */}
|
||||||
|
</ul>
|
||||||
|
<button onClick={() => createMunicipios({ name: 'New Municipio' })}>Create Municipio</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MunicipiosContent;
|
||||||
124
src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx
Normal file
124
src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
import { apiConfig } from '@/config/api.config';
|
||||||
|
import axios from 'axios';
|
||||||
|
import React, { createContext, useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
interface MunicipiosProps {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MunicipiosContext {
|
||||||
|
municipios: MunicipiosProps[];
|
||||||
|
getMunicipiosLists: (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: string,
|
||||||
|
order_direction: 'ASC' | 'DESC'
|
||||||
|
) => Promise<void>;
|
||||||
|
getMunicipiosByName: (name: string) => Promise<void>;
|
||||||
|
createMunicipios: (data: Partial<MunicipiosProps>) => Promise<void>;
|
||||||
|
updateMunicipios: (id: number, data: Partial<MunicipiosProps>) => Promise<void>;
|
||||||
|
deleteMunicipios: (id: number, hardDelete?: boolean) => Promise<void>;
|
||||||
|
restoreMunicipios: (id: number) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ManageMunicipiosContext = createContext<MunicipiosContext | undefined>(undefined);
|
||||||
|
const API_URL = apiConfig.service_master_data;
|
||||||
|
|
||||||
|
const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) => {
|
||||||
|
const [municipios, setMunicipios] = useState<MunicipiosProps[]>([]);
|
||||||
|
|
||||||
|
const getMunicipiosLists = async (
|
||||||
|
limit: number,
|
||||||
|
page: number,
|
||||||
|
with_deleted: boolean,
|
||||||
|
order_field: string,
|
||||||
|
order_direction: 'ASC' | 'DESC'
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/municipios/list`, {
|
||||||
|
params: {
|
||||||
|
limit: limit,
|
||||||
|
page: page,
|
||||||
|
with_deleted: with_deleted,
|
||||||
|
order_field: order_field,
|
||||||
|
order_direction: order_direction
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setMunicipios(response.data);
|
||||||
|
console.log(municipios);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching municipios', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getMunicipiosByName = async (name: string) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/municipios/postoadms/${name}`);
|
||||||
|
const data = response.data;
|
||||||
|
console.log(data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error fetching municipios by ${name}`, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const createMunicipios = async (data: Partial<MunicipiosProps>) => {
|
||||||
|
try {
|
||||||
|
await axios.post(`${API_URL}/municipios/create`, data);
|
||||||
|
getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating municipios', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateMunicipios = async (id: number, data: Partial<MunicipiosProps>) => {
|
||||||
|
try {
|
||||||
|
await axios.put(`${API_URL}/update/${id}`, data);
|
||||||
|
getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating municipios', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const deleteMunicipios = async (id: number, hardDelete?: boolean) => {
|
||||||
|
try {
|
||||||
|
await axios.delete(`${API_URL}/delete/${id}/${hardDelete}`);
|
||||||
|
getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error deleting municipios', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const restoreMunicipios = async (id: number) => {
|
||||||
|
try {
|
||||||
|
await axios.put(`${API_URL}/restore/${id}`);
|
||||||
|
getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error restoring municipios', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getMunicipiosLists(10, 1, false, 'name', 'ASC');
|
||||||
|
}, []);
|
||||||
|
console.log(municipios);
|
||||||
|
return (
|
||||||
|
<ManageMunicipiosContext.Provider
|
||||||
|
value={{
|
||||||
|
municipios,
|
||||||
|
getMunicipiosLists,
|
||||||
|
getMunicipiosByName,
|
||||||
|
createMunicipios,
|
||||||
|
updateMunicipios,
|
||||||
|
deleteMunicipios,
|
||||||
|
restoreMunicipios
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</ManageMunicipiosContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export { ManageMunicipiosProvider, ManageMunicipiosContext };
|
||||||
|
export type { MunicipiosProps };
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import { ManageMunicipiosContext } from './ManageMunicipiosContext';
|
||||||
|
|
||||||
|
const useManageMunicipiosContext = () => {
|
||||||
|
const context = useContext(ManageMunicipiosContext);
|
||||||
|
|
||||||
|
if (!context) throw new Error('useManageMunicipiosContext must be used within AuthProvider');
|
||||||
|
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { useManageMunicipiosContext };
|
||||||
Reference in New Issue
Block a user