Merge branch 'master' of https://github.com/CloudAlb/tcc-vamos-frontend into feature/buscar-transporte
This commit is contained in:
18
src/services/api-client.service.ts
Normal file
18
src/services/api-client.service.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import axios from "axios";
|
||||
|
||||
export class ApiClient{
|
||||
private static api = axios.create({
|
||||
baseURL: "http://localhost:8080"
|
||||
});
|
||||
|
||||
public static async doPost (url: string, body: any): Promise<any> {
|
||||
return await this.api
|
||||
.post(url, body)
|
||||
.then(res => {
|
||||
console.log(res.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import apiConfig from '../config/api.config';
|
||||
import apiConfig from '../../config/api.config';
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: apiConfig.getBaseUrl(),
|
||||
25
src/services/api/cars.ts
Normal file
25
src/services/api/cars.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import instance from './api';
|
||||
|
||||
import carsRoutes from '../../constants/routes/carsRoutes';
|
||||
import { AxiosRequestHeaders } from 'axios';
|
||||
import LocalStorage from '../../LocalStorage';
|
||||
|
||||
let token: string;
|
||||
let header: AxiosRequestHeaders;
|
||||
|
||||
function updateHeader() {
|
||||
token = LocalStorage.getToken();
|
||||
|
||||
header = {
|
||||
"Accept": 'application/json',
|
||||
"Content-Type": 'application/json',
|
||||
"Authorization": 'Bearer ' + token
|
||||
}
|
||||
}
|
||||
export async function list() {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(carsRoutes.list.url, { headers: header });
|
||||
|
||||
return response.data;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import instance from '../services/api';
|
||||
import sessionRoutes from '../constants/routes/sessionRoutes';
|
||||
import LocalStorage from '../LocalStorage';
|
||||
import instance from './api';
|
||||
import sessionRoutes from '../../constants/routes/sessionRoutes';
|
||||
import LocalStorage from '../../LocalStorage';
|
||||
import { AxiosRequestHeaders } from 'axios';
|
||||
|
||||
let token: string | null;
|
||||
@@ -14,8 +14,8 @@ interface createData {
|
||||
function updateHeader() {
|
||||
token = LocalStorage.getToken();
|
||||
header = {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
"Accept": 'application/json',
|
||||
"Content-Type": 'application/json',
|
||||
"Authorization": 'Bearer ' + token
|
||||
}
|
||||
}
|
||||
89
src/services/api/users.ts
Normal file
89
src/services/api/users.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import instance from './api';
|
||||
// import LocalStorage from '../LocalStorage';
|
||||
|
||||
import userRoutes from '../../constants/routes/usersRoutes';
|
||||
import { AxiosRequestHeaders } from 'axios';
|
||||
import LocalStorage from '../../LocalStorage';
|
||||
|
||||
let token: string;
|
||||
let header: AxiosRequestHeaders;
|
||||
|
||||
function updateHeader() {
|
||||
token = LocalStorage.getToken();
|
||||
|
||||
header = {
|
||||
"Accept": 'application/json',
|
||||
"Content-Type": 'application/json',
|
||||
"Authorization": 'Bearer ' + token
|
||||
}
|
||||
}
|
||||
|
||||
export interface CadastroResponse {
|
||||
message?: string;
|
||||
|
||||
token?: {
|
||||
token: string;
|
||||
};
|
||||
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface CadastroRequest {
|
||||
name: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
birth_date: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface UpdateUserRequest {
|
||||
name?: string;
|
||||
email?: string;
|
||||
bio?: string;
|
||||
document_type?: string;
|
||||
document?: string;
|
||||
phone_number?: string;
|
||||
}
|
||||
|
||||
// export async function get(cpf) {
|
||||
// updateHeader();
|
||||
|
||||
// const response = await instance.get(userRoutes.get.url + `/${cpf}`, { headers: header });
|
||||
// return response.data;
|
||||
// }
|
||||
|
||||
export async function create(CadastroRequest: any) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.post(userRoutes.create.url, CadastroRequest);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function getById(userId: string) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(userRoutes.get.url + `/${userId}`, { headers: header });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function update(userData: UpdateUserRequest) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.patch(userRoutes.update.url, userData, { headers: header });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function checkIfUserIsDriver(id_user: string) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(userRoutes.checkIfUserIsDriver.url + `/${id_user}`, { headers: header });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
// TODO, continuar
|
||||
export async function getSocialInfo(userId: string) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(userRoutes.getSocialInfo.url + `/${userId}`, { headers: header });
|
||||
return response.data;
|
||||
}
|
||||
73
src/services/api/vans.ts
Normal file
73
src/services/api/vans.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import instance from "./api";
|
||||
|
||||
import vansRoutes from "../../constants/routes/vansRoutes";
|
||||
import { AxiosRequestHeaders } from "axios";
|
||||
import LocalStorage from "../../LocalStorage";
|
||||
|
||||
let token: string;
|
||||
let header: AxiosRequestHeaders;
|
||||
|
||||
function updateHeader() {
|
||||
token = LocalStorage.getToken();
|
||||
|
||||
header = {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + token,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getByPlate(vanId: string) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(vansRoutes.getByPlate.url + `/${vanId}`, {
|
||||
headers: header,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function getByUserId(userId: string) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.get(vansRoutes.getByUserId.url + `/${userId}`, {
|
||||
headers: header,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
interface CreateVanBody {
|
||||
plate: string;
|
||||
brand: string;
|
||||
model: string;
|
||||
seats_number: string;
|
||||
locator_name: string;
|
||||
locator_address: string;
|
||||
locator_complement: string;
|
||||
locator_city: string;
|
||||
locator_state: string;
|
||||
}
|
||||
|
||||
export async function create(CreateVanBody: CreateVanBody) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.post(vansRoutes.create.url, CreateVanBody, { headers: header });
|
||||
return response.data;
|
||||
}
|
||||
|
||||
interface UpdateVanBody {
|
||||
brand?: string;
|
||||
model?: string;
|
||||
seats_number?: string;
|
||||
}
|
||||
|
||||
export async function update(vanData: UpdateVanBody) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.patch(vansRoutes.update.url, vanData, {
|
||||
headers: header,
|
||||
});
|
||||
|
||||
return response.data;
|
||||
}
|
||||
51
src/services/functions/carsService.ts
Normal file
51
src/services/functions/carsService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import * as carsRoutes from "../api/cars";
|
||||
|
||||
interface getAllCarModelsReturn {
|
||||
data?: {
|
||||
id_model: string;
|
||||
name: string;
|
||||
}[];
|
||||
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
interface getAllCarModelsRes {
|
||||
status?: string;
|
||||
|
||||
message: string
|
||||
|
||||
data?: {
|
||||
id_model: string;
|
||||
name: string;
|
||||
}[];
|
||||
|
||||
|
||||
}
|
||||
|
||||
const getAllCarModels = async (): Promise<getAllCarModelsReturn> => {
|
||||
try {
|
||||
let res: getAllCarModelsRes = await carsRoutes.list();
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: res.message,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
data: res.data,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export default { getAllCarModels };
|
||||
46
src/services/functions/sessionsService.ts
Normal file
46
src/services/functions/sessionsService.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import * as sessionRoutes from "../api/session";
|
||||
|
||||
interface refreshSessionReturn {
|
||||
userId?: string;
|
||||
error?: boolean;
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
interface refreshSessionResponse {
|
||||
status?: string;
|
||||
message?: string;
|
||||
userId?: string;
|
||||
}
|
||||
|
||||
const refreshSession = async (): Promise<refreshSessionReturn> => {
|
||||
try {
|
||||
let res: refreshSessionResponse = await sessionRoutes.refresh()
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: true,
|
||||
errorMessage: res.message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
userId: res.userId,
|
||||
};
|
||||
} catch(err) {
|
||||
return {
|
||||
error: true,
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
};
|
||||
}
|
||||
// catch (err: any) {
|
||||
// if (err.response) {
|
||||
// // The client was given an error response (5xx, 4xx)
|
||||
// } else if (err.request) {
|
||||
// // The client never received a response, and the request was never left
|
||||
// } else {
|
||||
// // Anything else
|
||||
// }
|
||||
// }
|
||||
};
|
||||
|
||||
export default { refreshSession }
|
||||
135
src/services/functions/usersService.ts
Normal file
135
src/services/functions/usersService.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import * as usersRoutes from "../api/users";
|
||||
|
||||
interface getByIdReturn {
|
||||
userData?: {
|
||||
name: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
phone_number: string;
|
||||
birth_date: string;
|
||||
bio: string;
|
||||
document_type: string;
|
||||
document: string;
|
||||
},
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
interface getByIdRes {
|
||||
status: string;
|
||||
message: string;
|
||||
userId?: string;
|
||||
data: {
|
||||
name: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
phone_number: string;
|
||||
birth_date: string;
|
||||
bio: string;
|
||||
document_type: string;
|
||||
document: string;
|
||||
},
|
||||
}
|
||||
|
||||
const getById = async (userId: string): Promise<getByIdReturn> => {
|
||||
try {
|
||||
let res: getByIdRes = await usersRoutes.getById(userId)
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: res.message,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
userData: res.data,
|
||||
};
|
||||
} catch(err) {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
interface getByIdReturn {
|
||||
data?: {
|
||||
phone: '',
|
||||
whatsapp: '',
|
||||
facebook: '',
|
||||
telegram: '',
|
||||
},
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
const getUserSocialInfo = async (userId: string): Promise<getByIdReturn> => {
|
||||
try {
|
||||
let res: getByIdRes = await usersRoutes.getSocialInfo(userId)
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: res.message,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
userData: res.data,
|
||||
};
|
||||
} catch(err) {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
interface checkIfUserIsDriverReturn {
|
||||
result?: boolean;
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
interface checkIfUserIsDriverResponse {
|
||||
status: string;
|
||||
message: string;
|
||||
result?: boolean;
|
||||
error?: {
|
||||
errorMessage: string;
|
||||
}
|
||||
}
|
||||
|
||||
const checkIfUserIsDriver = async (id_user: string): Promise<checkIfUserIsDriverReturn> => {
|
||||
try {
|
||||
let res: checkIfUserIsDriverResponse = await usersRoutes.checkIfUserIsDriver(id_user)
|
||||
|
||||
if (res.status === "error") {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: res.message,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: res.result,
|
||||
};
|
||||
} catch(err) {
|
||||
return {
|
||||
error: {
|
||||
errorMessage: "Por favor, autentique-se.",
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export default { getById, getUserSocialInfo, checkIfUserIsDriver }
|
||||
@@ -1,65 +0,0 @@
|
||||
import instance from '../services/api';
|
||||
import { setStore } from "../store/RecordsStore";
|
||||
// import LocalStorage from '../LocalStorage';
|
||||
|
||||
// let token:string;
|
||||
let header:string;
|
||||
|
||||
function updateHeader() {
|
||||
// token = LocalStorage.getToken();
|
||||
header = `{
|
||||
"Accept": 'application/json',
|
||||
"Content-Type": 'application/json',
|
||||
"Authorization": 'Bearer ' + token
|
||||
}`
|
||||
}
|
||||
|
||||
export interface CadastroResponse {
|
||||
message?: string;
|
||||
|
||||
token?: {
|
||||
token: string;
|
||||
};
|
||||
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface CadastroRequest {
|
||||
name: string;
|
||||
email: string;
|
||||
birth_date: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
// export async function get(cpf) {
|
||||
// updateHeader();
|
||||
|
||||
// const response = await instance.get(userRoutes.get.url + `/${cpf}`, { headers: header });
|
||||
// return response.data;
|
||||
// }
|
||||
|
||||
export async function create(CadastroRequest: any) {
|
||||
updateHeader();
|
||||
|
||||
const response = await instance.post("http://localhost:3333/users/", CadastroRequest);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
export async function getUsersSearching(currentPoint: any) {
|
||||
// Replace lat/long with values from get current location.
|
||||
// Allow choosing of radius?
|
||||
// Offset could = amount loaded in an infinite scroll?
|
||||
var latitude = currentPoint.latitude, longitude = currentPoint.longitude, radius = 3000, offset = 0;
|
||||
// const response = await fetch(`http://localhost:4000/get-records?latitude=${ latitude }&longitude=${ longitude }&radius=${ radius }&offset=${ offset }`);
|
||||
const response = await instance.post("http://localhost:3333/search/inraio", currentPoint)
|
||||
// const data = await response.json();
|
||||
console.log(response.data)
|
||||
setStore(response.data);
|
||||
}
|
||||
|
||||
export async function createUserSearch(latitude_from: any, longitude_from: any, addres_to: any) {
|
||||
const response = await instance.post("http://localhost:3333/search/", { latitude_from, longitude_from, addres_to });
|
||||
|
||||
console.log(response)
|
||||
setStore(response);
|
||||
}
|
||||
25
src/services/validateCpf.ts
Normal file
25
src/services/validateCpf.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export default function validateCpf(cpf: string): Boolean {
|
||||
let soma = 0, resto;
|
||||
|
||||
if (cpf === "00000000000") return false;
|
||||
|
||||
for (let i = 1; i <= 9; i++)
|
||||
soma = soma + parseInt(cpf.substring(i - 1, i)) * (11 - i);
|
||||
|
||||
resto = (soma * 10) % 11;
|
||||
|
||||
if (resto === 10 || resto === 11) resto = 0;
|
||||
if (resto !== parseInt(cpf.substring(9, 10))) return false;
|
||||
|
||||
soma = 0;
|
||||
for (let i = 1; i <= 10; i++)
|
||||
soma = soma + parseInt(cpf.substring(i - 1, i)) * (12 - i);
|
||||
|
||||
resto = (soma * 10) % 11;
|
||||
|
||||
if (resto === 10 || resto === 11) resto = 0;
|
||||
|
||||
if (resto !== parseInt(cpf.substring(10, 11))) return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
Reference in New Issue
Block a user