Skip to content

Commit

Permalink
Merge pull request #63 from Grupo-Syntax-Squad/44-recepção-das-mediçõ…
Browse files Browse the repository at this point in the history
…es-e-exibição-nos-gráficos

Performando estações, parâmetros e alertas
  • Loading branch information
iagocpv authored Nov 12, 2024
2 parents 82dd5fb + c7f52c8 commit ced9abb
Show file tree
Hide file tree
Showing 67 changed files with 5,305 additions and 3,666 deletions.
5,029 changes: 3,603 additions & 1,426 deletions tupan/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions tupan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
},
"dependencies": {
"@material-tailwind/react": "^2.1.10",
"@types/js-cookie": "^3.0.6",
"axios": "^1.7.7",
"flowbite": "^2.5.1",
"highcharts": "^11.4.8",
"js-cookie": "^3.0.5",
"leaflet": "^1.9.4",
"next": "^14.2.15",
"react": "^18",
"react-dom": "^18",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-leaflet": "^4.2.1",
"react-toastify": "^10.0.5",
"tupan": "file:"
Expand Down
Binary file added tupan/public/assets/favicon.ico
Binary file not shown.
Binary file modified tupan/public/assets/image 126.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions tupan/src/app/_api/get/alertas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ export const obterAlertas = async (token: string): Promise<Alerta[]> => {
}
};

export const obterAlertaPorId = async (id: number): Promise<Alerta> => {
const token = useToken();
export const obterAlertaPorId = async (id: number, token: string): Promise<Alerta> => {

if (!token) {
throw new Error("Token não encontrado. Por favor, faça login.");
}

try {
const token = useToken()
const response = await fetch(`${api_route}alertas/${id}`, {
method: "GET",
headers: {
Expand All @@ -51,7 +49,7 @@ export const obterAlertaPorId = async (id: number): Promise<Alerta> => {
});

if (!response.ok) {
throw new Error(`Erro ao obter categoria: ${response.statusText}`);
throw new Error(`Erro ao obter alerta: ${response.statusText}`);
}

const data = await response.json();
Expand Down
37 changes: 37 additions & 0 deletions tupan/src/app/_api/get/cep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface Endereco {
cep: string;
logradouro: string;
complemento: string;
bairro: string;
localidade: string;
uf: string;
ibge: string;
gia: string;
ddd: string;
siafi: string;
}

export async function fetchEndereco(cep: string): Promise<Endereco | null> {
const url = `https://viacep.com.br/ws/${cep}/json/`;

try {
const response = await fetch(url);

if (!response.ok) {
throw new Error(`Erro ao buscar o CEP: ${response.status}`);
}

const data: Endereco = await response.json();

if ('erro' in data) {
console.log('CEP não encontrado');
return null;
}

return data;
} catch (error) {
console.error("Erro na requisição:", error);
return null;
}
}

8 changes: 3 additions & 5 deletions tupan/src/app/_api/get/estacoes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { api_route } from "..";
import { useToken } from "@/hooks/token";

interface Estacao {
id: number;
nome: string;
endereco: {};
topico: string;
ativo: boolean;
criado: string;
Expand All @@ -12,8 +12,7 @@ interface Estacao {
}


export const obterEstacoes = async (): Promise<Estacao[]> => {
const token = useToken();
export const obterEstacoes = async (token: string): Promise<Estacao[]> => {

if (!token) {
throw new Error("Token não encontrado. Por favor, faça login.");
Expand All @@ -40,8 +39,7 @@ export const obterEstacoes = async (): Promise<Estacao[]> => {
}
};

export const obterEstacaoPorId = async (id: number): Promise<Estacao> => {
const token = useToken();
export const obterEstacaoPorId = async (id: number, token: string): Promise<Estacao> => {

if (!token) {
throw new Error("Token não encontrado. Por favor, faça login.");
Expand Down
5 changes: 1 addition & 4 deletions tupan/src/app/_api/get/parametros.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useToken } from "@/hooks/token";
import { api_route } from "..";

interface Parametro {
Expand Down Expand Up @@ -34,15 +33,13 @@ export const obterParametros = async (token: string): Promise<Parametro[]> => {
}
};

export const obterParametroPorId = async (id: number): Promise<Parametro> => {
const token = useToken();
export const obterParametroPorId = async (id: number, token: string): Promise<Parametro> => {

if (!token) {
throw new Error("Token não encontrado. Por favor, faça login.");
}

try {
const token = useToken()
const response = await fetch(`${api_route}parametros/${id}`, {
method: "GET",
headers: {
Expand Down
4 changes: 1 addition & 3 deletions tupan/src/app/_api/post/alertas.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { api_route } from "..";
import { useToken } from "@/hooks/token";

interface Alerta {
nome: string;
condicao: string;
ativo: boolean;
}

export const criarAlerta = async (alerta: Alerta): Promise<any> => {
const token = useToken()
export const criarAlerta = async (alerta: Alerta, token: any): Promise<any> => {
try {
if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
Expand Down
35 changes: 16 additions & 19 deletions tupan/src/app/_api/post/enderecos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { api_route } from "..";
import { useToken } from "@/hooks/token";
import { api_route } from '..';

interface Endereco {
id: number;
logradouro: string;
bairro: string;
cidade: string;
Expand All @@ -14,30 +12,29 @@ interface Endereco {
longitude: string;
}

export const criarEndereco = async (endereco: Endereco): Promise<Endereco> => {
const token = useToken()
if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
}
export const criarEndereco = async (endereco: Endereco, token: any): Promise<any> => {

try {
const response = await fetch(`${api_route}enderecos`, {
method: "POST",
if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
}

const responseEndereco = await fetch(`${api_route}enderecos`, {
method: 'POST',
headers: {
"Authorization": `Token ${token}`,
"Content-Type": "application/json",
Authorization: `Token ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(endereco),
});

const responseData = await response.json();

if (!response.ok) {
throw new Error(`Erro ao criar o endereço: ${response.statusText}`);
if (!responseEndereco.ok) {
throw new Error(`Erro ao criar o endereço: ${responseEndereco.statusText}`);
}

return responseData;
return await responseEndereco.json();
} catch (error) {
console.error("Erro na requisição do endereço:", error);
console.error(error);
throw error;
}
};
};
59 changes: 17 additions & 42 deletions tupan/src/app/_api/post/estacoes.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,35 @@
import { api_route } from "..";
import { criarEndereco } from "@/app/_api/post/enderecos";
import { useToken } from "@/hooks/token";
import { api_route } from '..';

interface Estacao {
nome: string;
endereco: number;
topico: string;
ativo: boolean;
criado: string;
modificado: string;
parametros: Number[];
}

interface Endereco {
logradouro: string;
bairro: string;
cidade: string;
estado: string;
numero: number;
complemento: string;
cep: string;
latitude: number;
longitude: number;
}

// Remova o segundo parâmetro 'endereco' porque ele já foi tratado antes
export const criarEstacao = async (estacao: Estacao, enderecoId: number): Promise<any> => {
const token = useToken()

if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
}
console.log('Função criarEstacao foi chamada');
export const criarEstacao = async (estacao: Estacao, token: any): Promise<any> => {
try {
const estacaoComEndereco = { ...estacao, endereco: enderecoId };

console.log('Payload enviado:', estacaoComEndereco);
if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
}

const response = await fetch(`${api_route}estacoes`, {
method: "POST",
const responseEstacao = await fetch(`${api_route}estacoes`, {
method: 'POST',
headers: {
"Authorization": `Token ${token}`,
"Content-Type": "application/json",
Authorization: `Token ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(estacaoComEndereco),
body: JSON.stringify(estacao),
});

console.log('Resposta da API ao criar a estação:', response);

if (!response.ok) {
const errorData = await response.json();
console.error('Erro ao criar a estação:', errorData);
throw new Error(`Erro ao criar a estação: ${response.statusText}`);
if (!responseEstacao.ok) {
throw new Error(`Erro ao criar a estação: ${responseEstacao.statusText}`);
}

return await response.json();
return await responseEstacao.json();
} catch (error) {
console.error("Erro na requisição:", error);
console.error(error);
throw error;
}
};
};
1 change: 1 addition & 0 deletions tupan/src/app/_api/post/login.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useSetTokenUser } from '@/hooks/token';
import { api_route } from '..';

interface Usuario {
Expand Down
4 changes: 1 addition & 3 deletions tupan/src/app/_api/post/usuarios.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { api_route } from "..";
import { useToken } from "@/hooks/token";

interface Usuario {
email: string;
password: string;
}

export const criarUsuario = async (usuario: Usuario): Promise<any> => {
const token = useToken()
export const criarUsuario = async (usuario: Usuario, token: any): Promise<any> => {
try {
if (!token) {
throw new Error('Token não encontrado. Por favor, faça login.');
Expand Down
14 changes: 9 additions & 5 deletions tupan/src/app/_page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// pages/_app.tsx
import "leaflet/dist/leaflet.css";
import { AppProps } from "next/app";

import { AppProps } from 'next/app';
import AuthGuard from '@/app/authGuard';

function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
return (
<AuthGuard>
<Component {...pageProps} />
</AuthGuard>
);
}

export default MyApp;
export default MyApp;
Loading

0 comments on commit ced9abb

Please sign in to comment.