Cadastro de van agora faz requisições ao backend

This commit is contained in:
Matheus Albino Brunhara
2022-06-20 00:51:08 -05:00
parent 6d8e918074
commit c368324747
8 changed files with 222 additions and 125 deletions

View File

@@ -37,9 +37,11 @@ export interface CadastroRequest {
}
export interface UpdateUserRequest {
name: string;
email: string;
bio: string;
name?: string;
email?: string;
bio?: string;
cpf?: string;
cnpj?: string;
}
// export async function get(cpf) {

63
src/services/api/vans.ts Normal file
View File

@@ -0,0 +1,63 @@
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 getById(vanId: string) {
updateHeader();
const response = await instance.get(vansRoutes.getById.url + `/${vanId}`, {
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);
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;
}

View File

@@ -7,6 +7,8 @@ interface getByIdReturn {
email: string;
birth_date: string;
bio: string;
cpf: string;
cnpj: string;
},
error?: {
errorMessage: string;
@@ -23,6 +25,8 @@ interface getByIdRes {
email: string;
birth_date: string;
bio: string;
cpf: string;
cnpj: string;
},
}

View 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;
};