Cadastro de van agora faz requisições ao backend
This commit is contained in:
@@ -17,8 +17,9 @@ import Login from './pages/Login';
|
|||||||
import Home from './pages/Home';
|
import Home from './pages/Home';
|
||||||
import Perfil from './pages/Perfil';
|
import Perfil from './pages/Perfil';
|
||||||
import PerfilEditar from './pages/PerfilEditar';
|
import PerfilEditar from './pages/PerfilEditar';
|
||||||
import CadastroCompletar from './pages/CadastroCompletar/CadastroCompletar';
|
|
||||||
import CadastroVan from './pages/CadastroVan';
|
import CadastroVan from './pages/CadastroVan';
|
||||||
|
import CadastroCompletar from './pages/CadastroCompletar/CadastroCompletar';
|
||||||
|
import CompletarDocumentos from './pages/CadastroCompletar/CompletarDocumentos';
|
||||||
|
|
||||||
/* Core CSS required for Ionic components to work properly */
|
/* Core CSS required for Ionic components to work properly */
|
||||||
import '@ionic/react/css/core.css';
|
import '@ionic/react/css/core.css';
|
||||||
@@ -48,11 +49,16 @@ const routes = (
|
|||||||
<>
|
<>
|
||||||
<Route exact path="/cadastro" component={Cadastro}></Route>
|
<Route exact path="/cadastro" component={Cadastro}></Route>
|
||||||
<Route exact path="/login" component={Login}></Route>
|
<Route exact path="/login" component={Login}></Route>
|
||||||
|
|
||||||
<Route exact path="/home" component={Home}></Route>
|
<Route exact path="/home" component={Home}></Route>
|
||||||
|
|
||||||
<Route exact path="/perfil" component={Perfil}></Route>
|
<Route exact path="/perfil" component={Perfil}></Route>
|
||||||
<Route exact path="/perfil/editar" component={PerfilEditar}></Route>
|
<Route exact path="/perfil/editar" component={PerfilEditar}></Route>
|
||||||
<Route exact path="/perfil/completar" component={CadastroCompletar}></Route>
|
<Route exact path="/perfil/completar" component={CadastroCompletar}></Route>
|
||||||
|
<Route exact path="/perfil/completar/documentos" component={CompletarDocumentos}></Route>
|
||||||
|
|
||||||
<Route exact path="/usuario/:id" component={Perfil}></Route>
|
<Route exact path="/usuario/:id" component={Perfil}></Route>
|
||||||
|
|
||||||
<Route exact path="/cadastro-van" component={CadastroVan}></Route>
|
<Route exact path="/cadastro-van" component={CadastroVan}></Route>
|
||||||
<Route exact path="/">
|
<Route exact path="/">
|
||||||
<Redirect to="/login" />
|
<Redirect to="/login" />
|
||||||
|
|||||||
17
src/constants/routes/vansLocatorsRoutes.ts
Normal file
17
src/constants/routes/vansLocatorsRoutes.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
const vansRoutesDefault = '/vans/locator';
|
||||||
|
const vansRoutes = {
|
||||||
|
list: {
|
||||||
|
url: `${vansRoutesDefault}/list`
|
||||||
|
},
|
||||||
|
getById: {
|
||||||
|
url: `${vansRoutesDefault}/`
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
url: `${vansRoutesDefault}/`
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
url: `${vansRoutesDefault}/edit`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default vansRoutes;
|
||||||
17
src/constants/routes/vansRoutes.ts
Normal file
17
src/constants/routes/vansRoutes.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
const vansRoutesDefault = '/vans';
|
||||||
|
const vansRoutes = {
|
||||||
|
list: {
|
||||||
|
url: `${vansRoutesDefault}/list`
|
||||||
|
},
|
||||||
|
getById: {
|
||||||
|
url: `${vansRoutesDefault}/`
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
url: `${vansRoutesDefault}/`
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
url: `${vansRoutesDefault}/`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default vansRoutes;
|
||||||
@@ -20,12 +20,12 @@ import {
|
|||||||
|
|
||||||
import React, { useEffect, useReducer, useState } from "react";
|
import React, { useEffect, useReducer, useState } from "react";
|
||||||
|
|
||||||
import * as yup from 'yup';
|
// import * as yup from 'yup';
|
||||||
|
|
||||||
import { ApiClient } from "../services/api-client.service";
|
|
||||||
|
|
||||||
import carsService from '../services/functions/carsService'
|
import carsService from '../services/functions/carsService'
|
||||||
|
|
||||||
|
import * as vansRoutes from '../services/api/vans';
|
||||||
|
|
||||||
import "./CadastroVan.css";
|
import "./CadastroVan.css";
|
||||||
|
|
||||||
const CadastroVan: React.FC = () => {
|
const CadastroVan: React.FC = () => {
|
||||||
@@ -43,70 +43,19 @@ const CadastroVan: React.FC = () => {
|
|||||||
carPlate: '',
|
carPlate: '',
|
||||||
carBrand: '',
|
carBrand: '',
|
||||||
carModel: '',
|
carModel: '',
|
||||||
maxPassengers: 1,
|
seats_number: 1,
|
||||||
isRent: false,
|
isRented: false,
|
||||||
carRentalName: '',
|
locator_name: '',
|
||||||
postalCode: '',
|
locator_address: '',
|
||||||
street: '',
|
locator_complement: '',
|
||||||
number: '',
|
locator_city: '',
|
||||||
complement: '',
|
locator_state: '',
|
||||||
city: '',
|
|
||||||
state: '',
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO, yup
|
|
||||||
let schema = yup.object().shape({
|
|
||||||
carPlate: yup.string().required(),
|
|
||||||
carBrand: yup.string().required(),
|
|
||||||
carModel: yup.string().required(),
|
|
||||||
maxPassengers: yup.number().integer().min(1).max(100).required(),
|
|
||||||
isRented: yup.boolean().required(),
|
|
||||||
carRentalName: yup.string(), // .required(),
|
|
||||||
postalCode: yup.string(), // .required(),
|
|
||||||
street: yup.string(), // .required(),
|
|
||||||
number: yup.number().integer(), // .required(),
|
|
||||||
complement: yup.string(), // .required(),
|
|
||||||
city: yup.string(), // .required(),
|
|
||||||
state: yup.string(), // .required(),
|
|
||||||
|
|
||||||
// name: yup.string().required(),
|
|
||||||
// age: yup.number().required().positive().integer(),
|
|
||||||
// email: yup.string().email(),
|
|
||||||
// website: yup.string().url(),
|
|
||||||
// createdOn: yup.date().default(function () {
|
|
||||||
// return new Date();
|
|
||||||
// }),
|
|
||||||
});
|
|
||||||
|
|
||||||
const vanForm = {
|
|
||||||
carPlate: inputValues.carPlate,
|
|
||||||
carBrand: inputValues.carBrand,
|
|
||||||
carModel: inputValues.carModel,
|
|
||||||
maxPassengers: inputValues.maxPassengers,
|
|
||||||
isRented: inputValues.isRented,
|
|
||||||
carRentalName: inputValues.carRentalName,
|
|
||||||
carRentalAddress: {
|
|
||||||
postalCode: inputValues.postalCode,
|
|
||||||
street: inputValues.street,
|
|
||||||
number: inputValues.number,
|
|
||||||
complement: inputValues.complement,
|
|
||||||
city: inputValues.city,
|
|
||||||
state: inputValues.state,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const clearRentalData = () => {
|
const clearRentalData = () => {
|
||||||
setInputValues({
|
setInputValues({
|
||||||
carPlate: '',
|
|
||||||
carBrand: '',
|
|
||||||
carModel: '',
|
|
||||||
maxPassengers: 1,
|
|
||||||
isRent: false,
|
|
||||||
carRentalName: '',
|
carRentalName: '',
|
||||||
postalCode: '',
|
|
||||||
street: '',
|
|
||||||
number: '',
|
|
||||||
complement: '',
|
complement: '',
|
||||||
city: '',
|
city: '',
|
||||||
state: '',
|
state: '',
|
||||||
@@ -114,6 +63,14 @@ const CadastroVan: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const validateForm = (): boolean => {
|
const validateForm = (): boolean => {
|
||||||
|
const vanForm = {
|
||||||
|
carPlate: inputValues.carPlate,
|
||||||
|
carBrand: inputValues.carBrand,
|
||||||
|
carModel: inputValues.carModel,
|
||||||
|
seats_number: inputValues.seats_number,
|
||||||
|
isRented: inputValues.isRented,
|
||||||
|
};
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!vanForm.carPlate ||
|
!vanForm.carPlate ||
|
||||||
vanForm.carPlate.length !== 7 ||
|
vanForm.carPlate.length !== 7 ||
|
||||||
@@ -136,12 +93,18 @@ const CadastroVan: React.FC = () => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!vanForm.maxPassengers || !parseInt(`${vanForm.maxPassengers}`)) {
|
if (!vanForm.seats_number || !parseInt(`${vanForm.seats_number}`)) {
|
||||||
setToastMessage("Número de passageiros inválido");
|
setToastMessage("Número de passageiros inválido");
|
||||||
setShowToast(true);
|
setShowToast(true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((Number)(vanForm.seats_number) < 1) {
|
||||||
|
setToastMessage("Número de passageiros deve ser positivo!");
|
||||||
|
setShowToast(true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (vanForm.isRented) {
|
if (vanForm.isRented) {
|
||||||
return validateRentalForm();
|
return validateRentalForm();
|
||||||
} else {
|
} else {
|
||||||
@@ -152,43 +115,32 @@ const CadastroVan: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const validateRentalForm = (): boolean => {
|
const validateRentalForm = (): boolean => {
|
||||||
if (!vanForm.carRentalName) {
|
const locatorForm = {
|
||||||
|
locator_name: inputValues.locator_name,
|
||||||
|
locator_address: inputValues.locator_address,
|
||||||
|
locator_complement: inputValues.locator_complement,
|
||||||
|
locator_city: inputValues.locator_city,
|
||||||
|
locator_state: inputValues.locator_state,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!locatorForm.locator_name) {
|
||||||
setToastMessage("Nome do Locador é obrigatório");
|
setToastMessage("Nome do Locador é obrigatório");
|
||||||
setShowToast(true);
|
setShowToast(true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!vanForm.carRentalAddress.postalCode ||
|
!locatorForm.locator_city ||
|
||||||
vanForm.carRentalAddress.postalCode.length !== 8 ||
|
!locatorForm.locator_city.match(/([A-zà-úÀ-Ú])/g)
|
||||||
!vanForm.carRentalAddress.postalCode.match(/([0-9]){8}/g)
|
|
||||||
) {
|
) {
|
||||||
setToastMessage("Cep inválido");
|
setToastMessage("Cidade inválida");
|
||||||
setShowToast(true);
|
setShowToast(true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!vanForm.carRentalAddress.number ||
|
!locatorForm.locator_state ||
|
||||||
!parseInt(`${vanForm.carRentalAddress.number}`)
|
!locatorForm.locator_state.match(/([A-zà-úÀ-Ú])/g)
|
||||||
) {
|
|
||||||
setToastMessage("Número inválido");
|
|
||||||
setShowToast(true);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!vanForm.carRentalAddress.city ||
|
|
||||||
!vanForm.carRentalAddress.city.match(/([A-zà-úÀ-Ú])/g)
|
|
||||||
) {
|
|
||||||
setToastMessage("Cidade inválido");
|
|
||||||
setShowToast(true);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!vanForm.carRentalAddress.state ||
|
|
||||||
!vanForm.carRentalAddress.state.match(/([A-zà-úÀ-Ú])/g)
|
|
||||||
) {
|
) {
|
||||||
setToastMessage("Estado inválido");
|
setToastMessage("Estado inválido");
|
||||||
setShowToast(true);
|
setShowToast(true);
|
||||||
@@ -199,9 +151,34 @@ const CadastroVan: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (validateForm()) {
|
if (!validateForm()) {
|
||||||
await ApiClient.doPost("/cadastro-van", vanForm);
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cria registro da van
|
||||||
|
await vansRoutes.create({
|
||||||
|
plate: inputValues.carPlate,
|
||||||
|
brand: inputValues.carBrand,
|
||||||
|
model: inputValues.carModel,
|
||||||
|
seats_number: inputValues.seats_number,
|
||||||
|
locator_name: inputValues.locator_name,
|
||||||
|
locator_address: inputValues.locator_address,
|
||||||
|
locator_complement: inputValues.locator_complement,
|
||||||
|
locator_city: inputValues.locator_city,
|
||||||
|
locator_state: inputValues.locator_state
|
||||||
|
}).then(response => {
|
||||||
|
if (response.status === 'error') {
|
||||||
|
setToastMessage(response.message);
|
||||||
|
setShowToast(true);
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(response)
|
||||||
|
}).catch((err) => {
|
||||||
|
setToastMessage(err);
|
||||||
|
setShowToast(true);
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -248,24 +225,15 @@ const CadastroVan: React.FC = () => {
|
|||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
|
maxlength={7}
|
||||||
placeholder='Digite a Placa do Veículo'
|
placeholder='Digite a Placa do Veículo'
|
||||||
onIonInput={(e: any) => setInputValues({ carPlate: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ carPlate: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</IonItem>
|
</IonItem>
|
||||||
|
|
||||||
{/* <IonItem>
|
|
||||||
<IonLabel position='floating'>Marca </IonLabel>
|
|
||||||
<IonInput
|
|
||||||
type='text'
|
|
||||||
clearInput
|
|
||||||
placeholder='Digite a Marca do Veículo'
|
|
||||||
onIonInput={(e: any) => setInputValues({ carBrand: e.target.value })}
|
|
||||||
/>
|
|
||||||
</IonItem> */}
|
|
||||||
|
|
||||||
<IonItem>
|
<IonItem>
|
||||||
<IonLabel>Marca</IonLabel>
|
<IonLabel>Marca</IonLabel>
|
||||||
<IonSelect value={inputValues.marca}>
|
<IonSelect value={inputValues.carBrand} onIonChange={(e: any) => setInputValues({ carBrand: e.target.value })}>
|
||||||
{ carModels ? carModels.map((carModel, index) => {
|
{ carModels ? carModels.map((carModel, index) => {
|
||||||
return (<IonSelectOption key={index} value={carModel.name}>{carModel.name}</IonSelectOption>)
|
return (<IonSelectOption key={index} value={carModel.name}>{carModel.name}</IonSelectOption>)
|
||||||
}) : <></> }
|
}) : <></> }
|
||||||
@@ -278,19 +246,20 @@ const CadastroVan: React.FC = () => {
|
|||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Digite o Modelo do Veículo'
|
placeholder='Digite o Modelo do Veículo'
|
||||||
onIonInput={(e: any) => setInputValues({ carModel: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ carModel: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</IonItem>
|
</IonItem>
|
||||||
|
|
||||||
<IonItem>
|
<IonItem>
|
||||||
<IonLabel position='floating'>
|
<IonLabel position='floating'>
|
||||||
Número Máximo de Passageiros
|
Número de assentos
|
||||||
</IonLabel>
|
</IonLabel>
|
||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='number'
|
||||||
|
min={1}
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Digite o número máximo de passageiros'
|
placeholder='podem ser ocupados por passageiros'
|
||||||
onIonInput={(e: any) => setInputValues({ maxPassengers: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ seats_number: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</IonItem>
|
</IonItem>
|
||||||
</IonList>
|
</IonList>
|
||||||
@@ -313,38 +282,32 @@ const CadastroVan: React.FC = () => {
|
|||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Nome completo do Locador'
|
placeholder='Nome completo do Locador'
|
||||||
onIonInput={(e: any) => setInputValues({ carRentalName: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ locator_name: e.target.value })}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Endereço do locador'
|
placeholder='Endereço do locador'
|
||||||
onIonInput={(e: any) => setInputValues({ postalCode: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ locator_address: e.target.value })}
|
||||||
/>
|
|
||||||
<IonInput
|
|
||||||
type='text'
|
|
||||||
clearInput
|
|
||||||
placeholder='Número'
|
|
||||||
onIonInput={(e: any) => setInputValues({ number: e.target.value })}
|
|
||||||
/>
|
/>
|
||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Complemento'
|
placeholder='Complemento'
|
||||||
onIonInput={(e: any) => setInputValues({ complement: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ locator_complement: e.target.value })}
|
||||||
/>
|
/>
|
||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Cidade'
|
placeholder='Cidade'
|
||||||
onIonInput={(e: any) => setInputValues({ city: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ locator_city: e.target.value })}
|
||||||
/>
|
/>
|
||||||
<IonInput
|
<IonInput
|
||||||
type='text'
|
type='text'
|
||||||
clearInput
|
clearInput
|
||||||
placeholder='Estado'
|
placeholder='Estado'
|
||||||
onIonInput={(e: any) => setInputValues({ state: e.target.value })}
|
onIonChange={(e: any) => setInputValues({ locator_state: e.target.value })}
|
||||||
/>
|
/>
|
||||||
</IonItem>
|
</IonItem>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -37,9 +37,11 @@ export interface CadastroRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateUserRequest {
|
export interface UpdateUserRequest {
|
||||||
name: string;
|
name?: string;
|
||||||
email: string;
|
email?: string;
|
||||||
bio: string;
|
bio?: string;
|
||||||
|
cpf?: string;
|
||||||
|
cnpj?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// export async function get(cpf) {
|
// export async function get(cpf) {
|
||||||
|
|||||||
63
src/services/api/vans.ts
Normal file
63
src/services/api/vans.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -7,6 +7,8 @@ interface getByIdReturn {
|
|||||||
email: string;
|
email: string;
|
||||||
birth_date: string;
|
birth_date: string;
|
||||||
bio: string;
|
bio: string;
|
||||||
|
cpf: string;
|
||||||
|
cnpj: string;
|
||||||
},
|
},
|
||||||
error?: {
|
error?: {
|
||||||
errorMessage: string;
|
errorMessage: string;
|
||||||
@@ -23,6 +25,8 @@ interface getByIdRes {
|
|||||||
email: string;
|
email: string;
|
||||||
birth_date: string;
|
birth_date: string;
|
||||||
bio: string;
|
bio: string;
|
||||||
|
cpf: string;
|
||||||
|
cnpj: string;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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