Skip to content

Commit f9a92eb

Browse files
committed
made code a little bit more readable
1 parent d9dcca5 commit f9a92eb

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

src/App.js

+31-48
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,8 @@
1-
import React from "react";
2-
import "./App.scss";
3-
4-
5-
import CitiesGrid from "./components/cities-grid";
6-
71
/**
8-
* # Desafio front-end
9-
*
10-
* É um componente que terá um select e um botão, e terá
11-
* uma listagem ou grid com dados logo abaixo.
12-
*
13-
* O componente deve ser montado inicialmente com um
14-
* placeholder.
15-
*
16-
* Deve então fazer um fetch na API para buscar a lista
17-
* de todos os estados do Brasil.
18-
*
19-
* Depois, listar no elemento do tipo select todos estes
20-
* países como opções.
21-
*
22-
* Quando o usuário selecionar um estado, o botão fica ativo.
23-
*
24-
* Se o usuário clicar no botão, deve então buscar a lista
25-
* de cidades naquele estado.
26-
*
27-
* Quando obtiver o resultado, deve listar as cidades daquele
28-
* estado exibindo o nome da cidade, e o nome da microrregião
29-
* daquela cidade.
30-
*
31-
* # Bonus points
32-
* - Colocar tanto os estados quanto as cidades em ordem
33-
* alfabética
34-
* - Se o dispositivo for pequeno (mobile), este grid pode
35-
* mostrar apenas o nome da cidade.
36-
*
2+
* Busca a lista de estados no Brasil.
3+
* Ao clicar no botão, se um estado estiver selecionado, lista as cidades
4+
* daquele estado.
5+
*
376
* # Helpers
387
* API para pegar a lista de estados do Brasil:
398
* https://servicodados.ibge.gov.br/api/v1/localidades/estados?orderBy=nome
@@ -42,27 +11,45 @@ import CitiesGrid from "./components/cities-grid";
4211
* https://servicodados.ibge.gov.br/api/v1/localidades/estados/${UF}/distritos
4312
*
4413
*/
14+
import React from "react";
15+
import CitiesGrid from "./components/cities-grid";
16+
17+
import "./App.scss";
4518

19+
/**
20+
* Generates the url used to retrieve the list of cities for a given state.
21+
*
22+
* @param {String} UF The acronym for the state
23+
* @returns String The parsed URL for the API to return the list of cities within that state
24+
*/
4625
const getCitiesAPI_URL = function (UF = "") {
4726
UF = UF.substring(0, 2);
4827
return `https://servicodados.ibge.gov.br/api/v1/localidades/estados/${UF}/distritos`;
4928
};
5029

30+
// The url for the API to retrieve the list of states.
5131
const API_STATES_UF = `https://servicodados.ibge.gov.br/api/v1/localidades/estados?orderBy=nome`;
5232

33+
/**
34+
* This object contains the list of cities already downloaded
35+
* for the states.
36+
* Each state is a `key` in the object.
37+
*/
5338
const CACHED_CITIES = {};
5439

40+
/**
41+
* Our main App entrypoint.
42+
*
43+
* @returns ReactComponent
44+
*/
5545
export default function App() {
56-
const [statesList, setStatesList] = React.useState(null);
57-
5846

47+
const [statesList, setStatesList] = React.useState(null);
5948
const [loadingCities, setLoadingCities] = React.useState(null);
60-
61-
6249
const [selectedState, setSelectedState] = React.useState(null);
6350
const [citiesForState, setCitiesForCurrentUF] = React.useState(null);
6451

65-
52+
// will load the list of states as soon as the component is ready (mount)
6653
React.useEffect((_) => {
6754
fetch(API_STATES_UF)
6855
.then(async (response) => {
@@ -76,17 +63,14 @@ export default function App() {
7663
}, []);
7764

7865
/**
79-
* in the first render, we show only the placeholder
66+
* If the list of states is not yet available, we show only a placeholder
8067
*/
8168
if (!statesList) {
8269
return <div className="no-data">Building up...</div>;
8370
}
8471

8572
/**
8673
* Sorts a list based on item's names.
87-
*
88-
* As both states and cities coming from the api have a
89-
* name, we can use the same function to sort them both.
9074
*/
9175
function sortByName(a, b) {
9276
return a.nome.localeCompare(b.nome);
@@ -98,8 +82,6 @@ export default function App() {
9882
*
9983
* @param {Event} event The onChange event
10084
*/
101-
102-
10385
function onStateChange(event) {
10486
setCitiesForCurrentUF(null);
10587
setSelectedState(event.target.value);
@@ -115,7 +97,9 @@ export default function App() {
11597
return setCitiesForCurrentUF(CACHED_CITIES[selectedState]);
11698
}
11799

100+
// mark it as in the "loading" state
118101
setLoadingCities(true);
102+
119103
fetch(getCitiesAPI_URL(selectedState))
120104
.then(async (response) => {
121105
const data = await response.json();
@@ -148,8 +132,6 @@ export default function App() {
148132
Ok
149133
</button>
150134
</div>
151-
{/* <div className="main">{getCitiesGrid()}</div> */}
152-
153135

154136
<div className="main">
155137
<CitiesGrid
@@ -158,6 +140,7 @@ export default function App() {
158140
cities={citiesForState}
159141
/>
160142
</div>
143+
161144
</div>
162145
);
163146
}

src/components/cities-grid/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import React /*, { useEffect, useState, useCallback, useRef }*/ from 'react';
1+
import React from 'react';
22

33
import './index.scss';
44

55
/**
6-
* React component: ComponentName
6+
* CitiesGrid
77
*
8-
* Description
8+
* Responsible for rendering the list of cities or possible
9+
* alternative states, such as "loading" or "empty".
910
*
1011
* @param {Object} props Component's properties
1112
* @return {ReactComponent} ComponentName
1213
*/
13-
14-
1514
export default function CitiesGrid (props) {
15+
1616
const {
1717
className = '',
1818
children,
@@ -22,10 +22,12 @@ export default function CitiesGrid (props) {
2222
...other
2323
} = props;
2424

25+
// when loading
2526
if (loading) {
2627
return <div>Carregando: {state}</div>;
2728
}
2829

30+
// if not loading, but has no cities to list
2931
if (!loading && !cities?.length) {
3032
return <div>Selecione um estado e clique em OK</div>;
3133
}

0 commit comments

Comments
 (0)