From 1e9d347b4d70d6d788ac9fdbfd61b34bf59fdc3d Mon Sep 17 00:00:00 2001 From: Wikzyy Date: Wed, 26 Feb 2025 17:07:12 +0700 Subject: [PATCH] fetch get data Municipios --- .../master/municipios/MunicipiosContent.tsx | 34 +++++ .../hooks/ManageMunicipiosContext.tsx | 124 ++++++++++++++++++ .../hooks/useManageMunicipiosContext.tsx | 12 ++ 3 files changed, 170 insertions(+) create mode 100644 src/pages/master/municipios/MunicipiosContent.tsx create mode 100644 src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx create mode 100644 src/pages/master/municipios/hooks/useManageMunicipiosContext.tsx diff --git a/src/pages/master/municipios/MunicipiosContent.tsx b/src/pages/master/municipios/MunicipiosContent.tsx new file mode 100644 index 0000000..273072f --- /dev/null +++ b/src/pages/master/municipios/MunicipiosContent.tsx @@ -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 ( +
+

Municipios

+ + +
+ ); +}; + +export default MunicipiosContent; diff --git a/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx b/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx new file mode 100644 index 0000000..925cf8f --- /dev/null +++ b/src/pages/master/municipios/hooks/ManageMunicipiosContext.tsx @@ -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; + getMunicipiosByName: (name: string) => Promise; + createMunicipios: (data: Partial) => Promise; + updateMunicipios: (id: number, data: Partial) => Promise; + deleteMunicipios: (id: number, hardDelete?: boolean) => Promise; + restoreMunicipios: (id: number) => Promise; +} + +const ManageMunicipiosContext = createContext(undefined); +const API_URL = apiConfig.service_master_data; + +const ManageMunicipiosProvider = ({ children }: { children: React.ReactNode }) => { + const [municipios, setMunicipios] = useState([]); + + 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) => { + 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) => { + 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 ( + + {children} + + ); +}; + +export { ManageMunicipiosProvider, ManageMunicipiosContext }; +export type { MunicipiosProps }; diff --git a/src/pages/master/municipios/hooks/useManageMunicipiosContext.tsx b/src/pages/master/municipios/hooks/useManageMunicipiosContext.tsx new file mode 100644 index 0000000..6dba2e7 --- /dev/null +++ b/src/pages/master/municipios/hooks/useManageMunicipiosContext.tsx @@ -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 };