Skip to content

Commit

Permalink
feat(address): implementation of address search with viacep
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Eduardo committed Nov 22, 2020
1 parent 0223668 commit 09339cb
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"import/no-unresolved": 0,
"import/prefer-default-export": "off",
"no-use-before-define": "off",
"no-useless-constructor": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.1.0",
"private": true,
"dependencies": {
"axios": "^0.21.0",
"polished": "^4.0.4",
"react": "^17.0.1",
"react-dom": "^17.0.1",
Expand All @@ -17,6 +18,7 @@
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.1",
"@testing-library/user-event": "^12.2.2",
"@types/axios": "^0.14.0",
"@types/jest": "^26.0.15",
"@types/node": "^12.19.6",
"@types/react": "^16.9.56",
Expand Down
22 changes: 9 additions & 13 deletions src/miragejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ createServer({
routes() {
this.namespace = 'api';

this.get('/persons', () => [
{
name: 'Lucas',
dateOfBirth: '30/06/1997',
cpf: '0000',
cep: '919281982',
publicPlace: 'Algum lugar',
neighborhood: 'Outro lugar',
number: 345,
city: 'Alguma cidade',
state: 'SP',
},
]);
this.get('/persons', () => []);

this.post('/persons', (_: void, request: any) => {
const attrs = JSON.parse(request.requestBody);

return attrs;
});

this.passthrough('https://viacep.com.br/ws/**');
},
});
7 changes: 7 additions & 0 deletions src/modules/Address/dtos/IAddressDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IAddressDto {
cep: number;
publicPlace: string;
neighborhood: string;
city: string;
state: string;
}
8 changes: 8 additions & 0 deletions src/modules/Address/dtos/IViaCepDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface IViaCepDto {
cep: string;
logradouro: string;
complemento: string;
bairro: string;
localidade: string;
uf: string;
}
31 changes: 31 additions & 0 deletions src/modules/Address/infra/http/AxiosRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IAddressDto } from 'modules/Address/dtos/IAddressDto';
import { IViaCepDto } from 'modules/Address/dtos/IViaCepDto';
import { IAddressRepository } from 'modules/Address/repositories/IAddressRepository';

import { axios } from 'shared/infra/http/HttpAxios';

class AxiosRepository implements IAddressRepository {
searchAddress(cep: string): Promise<IAddressDto> {
const url = process.env.REACT_APP_SEARCH_ADDRESS?.replace(
'{CEP}',
cep,
) as string;

const address: Promise<IAddressDto> = axios
.get<IViaCepDto>(url)
.then(({ data }) => ({
cep: Number(data.cep.replace('-', '')),
publicPlace: data.logradouro,
neighborhood: data.bairro,
city: data.localidade,
state: data.uf,
}))
.catch(err => {
throw new Error(err);
});

return address;
}
}

export default AxiosRepository;
5 changes: 5 additions & 0 deletions src/modules/Address/repositories/IAddressRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IAddressDto } from '../dtos/IAddressDto';

export interface IAddressRepository {
searchAddress(cep: string): Promise<IAddressDto>;
}
20 changes: 20 additions & 0 deletions src/modules/Address/services/SearchAddressService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IAddressDto } from '../dtos/IAddressDto';
import AxiosRepository from '../infra/http/AxiosRepository';
import { IAddressRepository } from '../repositories/IAddressRepository';

class SearchAddressService {
constructor(private readonly addressRepository: IAddressRepository) {}

execute(cep: string): Promise<IAddressDto> {
const address = this.addressRepository
.searchAddress(cep)
.then(data => data)
.catch(err => {
throw new Error(err);
});

return address;
}
}

export default new SearchAddressService(new AxiosRepository());
22 changes: 21 additions & 1 deletion src/pages/Home/ModalAddPerson/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useState, useCallback, useEffect, FormEvent } from 'react';
import { MdClose } from 'react-icons/md';

import { Input } from '../../../components/Input';
import searchAddressService from 'modules/Address/services/SearchAddressService';

import { Input } from 'components/Input';

import { IModalAddPersonProps } from './IModalAddPerson';

Expand Down Expand Up @@ -57,6 +59,24 @@ const ModalAddPerson = ({ instanceModal }: IModalAddPersonProps) => {
],
);

const getAddress = useCallback((cep: string) => {
searchAddressService
.execute(cep)
.then(({ publicPlace, neighborhood, city, state }) => {
setPublicPlace(publicPlace);
setNeighborhood(neighborhood);
setCity(city);
setState(state);
})
.catch(err => console.error(err));
}, []);

useEffect(() => {
if (cep.length === 8 || cep.length === 9) {
getAddress(cep);
}
}, [cep, getAddress]);

useEffect(() => {
setIsCenterContent(true);
}, [setIsCenterContent]);
Expand Down
7 changes: 7 additions & 0 deletions src/shared/infra/http/HttpAxios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import axios from 'axios';

const api = axios.create({
baseURL: process.env.REACT_APP_API,
});

export { api, axios };
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"isolatedModules": true,
"noEmit": true,
"baseUrl": "src",
"jsx": "react-jsx"
"jsx": "preserve"
},
"include": [
"src"
Expand Down

0 comments on commit 09339cb

Please sign in to comment.