Commit c4a627758a7fa3bde68ceee4fea1ebffb0acc295

Authored by José Maria Villac Pinheiro
1 parent 88fb7b9e

V. 0.6.1 - Funcionou a procura inteligente. Pode-se melhorar a performance agora

client/package.json
1 { 1 {
2 "name": "client", 2 "name": "client",
3 - "version": "0.1.0", 3 + "version": "0.6.1",
4 "private": true, 4 "private": true,
5 "proxy": { 5 "proxy": {
6 "/map": { 6 "/map": {
client/src/components/findConsumer.js
@@ -16,10 +16,7 @@ export default class FindConsumer extends Component<*, State> { @@ -16,10 +16,7 @@ export default class FindConsumer extends Component<*, State> {
16 const { selectedOption } = this.state; 16 const { selectedOption } = this.state;
17 const value = selectedOption && selectedOption.title; 17 const value = selectedOption && selectedOption.title;
18 const getOptions = input => { 18 const getOptions = input => {
19 - console.log("Input 1: " + input);  
20 return FindLikeConsumers(input).then(response => { 19 return FindLikeConsumers(input).then(response => {
21 - console.log("Resposta do FindLikeConsumers");  
22 - console.log(response);  
23 return { options: response }; 20 return { options: response };
24 }); 21 });
25 }; 22 };
client/src/components/findLikeConsumers.js
1 // React client will ask nodejs server for the list of all consumers that match part of its name 1 // React client will ask nodejs server for the list of all consumers that match part of its name
2 function FindLikeConsumers(partOfConsumerName) { 2 function FindLikeConsumers(partOfConsumerName) {
3 - return new Promise(function(resolve, reject) {  
4 - fetch("consumidores").then(res => {  
5 - // asks node the list of all consumers  
6 - res  
7 - .json()  
8 - .then(allConsumers => {  
9 - var foundConsumers = [];  
10 - var maximumSearchLines = 10;  
11 - allConsumers.forEach(element => {  
12 - if (element.label.indexOf(partOfConsumerName) > -1 && maximumSearchLines > 0) {  
13 - var obj = {};  
14 - obj.label = element.label;  
15 - obj.value = element.value;  
16 - foundConsumers.push(obj);  
17 - maximumSearchLines--;  
18 - }  
19 - });  
20 - if (foundConsumers !== undefined){  
21 - console.log (foundConsumers);  
22 - resolve (foundConsumers);  
23 - } else {  
24 - console.log("Não encontrou consumidores com o texto digitado.");  
25 - reject ("indefinido");  
26 - }  
27 - });  
28 - });  
29 -}); 3 + var partOfConsumerNameUppercase = partOfConsumerName.toUpperCase();
  4 + return new Promise(function(resolve, reject) {
  5 + fetch('consumidores').then((res) => {
  6 + // asks node the list of all consumers
  7 + res.json().then((allConsumers) => {
  8 + var foundConsumers = [];
  9 + var maximumSearchLines = 10;
  10 + var context = { foundConsumers, maximumSearchLines, partOfConsumerNameUppercase };
  11 + allConsumers.every(testIfContainsText, context);
  12 + if (foundConsumers === null) {
  13 + console.log('Não encontrou consumidores com o texto digitado.');
  14 + reject('indefinido');
  15 + } else {
  16 + resolve(context.foundConsumers);
  17 + }
  18 + });
  19 + });
  20 + });
30 } 21 }
31 22
32 -function getFoundConsumers (allConsumers, partOfConsumerName, callback) {  
33 - var foundConsumers = [];  
34 - var maximumSearchLines = 10;  
35 - allConsumers.forEach(element => {  
36 - if (element.label.indexOf(partOfConsumerName) > -1 && maximumSearchLines > 0) {  
37 - var obj = {};  
38 - obj.label = element.label;  
39 - obj.value = element.value;  
40 - foundConsumers.push(obj);  
41 - maximumSearchLines--;  
42 - }  
43 - else {  
44 - return foundConsumers;  
45 - }  
46 - }); 23 +function testIfContainsText(element, index) {
  24 + if (element.label.indexOf(this.partOfConsumerNameUppercase) !== -1 && this.maximumSearchLines > 0) {
  25 + var obj = {};
  26 + obj.label = element.label;
  27 + obj.value = element.value;
  28 + this.foundConsumers.push(obj);
  29 + this.maximumSearchLines--;
  30 + return true;
  31 + } else {
  32 + if (this.maximumSearchLines > 0) {
  33 + return true;
  34 + } else {
  35 + return false;
  36 + }
  37 + }
47 } 38 }
48 39
49 -export default FindLikeConsumers; 40 -export default FindLikeConsumers;
  41 +export default FindLikeConsumers;
50 \ No newline at end of file 42 \ No newline at end of file
1 { 1 {
2 "name": "GeoSanWeb", 2 "name": "GeoSanWeb",
3 - "version": "0.2.1", 3 + "version": "0.6.1",
4 "description": "Map application for water companies", 4 "description": "Map application for water companies",
5 "repository": "git@bitbucket.org:nexusbr/geosanweb.git", 5 "repository": "git@bitbucket.org:nexusbr/geosanweb.git",
6 "private": true, 6 "private": true,