diff --git a/client/package.json b/client/package.json index 257fad1..b5c1945 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "client", - "version": "0.1.0", + "version": "0.6.1", "private": true, "proxy": { "/map": { diff --git a/client/src/components/findConsumer.js b/client/src/components/findConsumer.js index a78a238..5347bba 100644 --- a/client/src/components/findConsumer.js +++ b/client/src/components/findConsumer.js @@ -16,10 +16,7 @@ export default class FindConsumer extends Component<*, State> { const { selectedOption } = this.state; const value = selectedOption && selectedOption.title; const getOptions = input => { - console.log("Input 1: " + input); return FindLikeConsumers(input).then(response => { - console.log("Resposta do FindLikeConsumers"); - console.log(response); return { options: response }; }); }; diff --git a/client/src/components/findLikeConsumers.js b/client/src/components/findLikeConsumers.js index 16db520..7e1022f 100644 --- a/client/src/components/findLikeConsumers.js +++ b/client/src/components/findLikeConsumers.js @@ -1,49 +1,40 @@ // React client will ask nodejs server for the list of all consumers that match part of its name function FindLikeConsumers(partOfConsumerName) { - return new Promise(function(resolve, reject) { - fetch("consumidores").then(res => { - // asks node the list of all consumers - res - .json() - .then(allConsumers => { - var foundConsumers = []; - var maximumSearchLines = 10; - allConsumers.forEach(element => { - if (element.label.indexOf(partOfConsumerName) > -1 && maximumSearchLines > 0) { - var obj = {}; - obj.label = element.label; - obj.value = element.value; - foundConsumers.push(obj); - maximumSearchLines--; - } - }); - if (foundConsumers !== undefined){ - console.log (foundConsumers); - resolve (foundConsumers); - } else { - console.log("Não encontrou consumidores com o texto digitado."); - reject ("indefinido"); - } - }); - }); -}); + var partOfConsumerNameUppercase = partOfConsumerName.toUpperCase(); + return new Promise(function(resolve, reject) { + fetch('consumidores').then((res) => { + // asks node the list of all consumers + res.json().then((allConsumers) => { + var foundConsumers = []; + var maximumSearchLines = 10; + var context = { foundConsumers, maximumSearchLines, partOfConsumerNameUppercase }; + allConsumers.every(testIfContainsText, context); + if (foundConsumers === null) { + console.log('Não encontrou consumidores com o texto digitado.'); + reject('indefinido'); + } else { + resolve(context.foundConsumers); + } + }); + }); + }); } -function getFoundConsumers (allConsumers, partOfConsumerName, callback) { - var foundConsumers = []; - var maximumSearchLines = 10; - allConsumers.forEach(element => { - if (element.label.indexOf(partOfConsumerName) > -1 && maximumSearchLines > 0) { - var obj = {}; - obj.label = element.label; - obj.value = element.value; - foundConsumers.push(obj); - maximumSearchLines--; - } - else { - return foundConsumers; - } - }); +function testIfContainsText(element, index) { + if (element.label.indexOf(this.partOfConsumerNameUppercase) !== -1 && this.maximumSearchLines > 0) { + var obj = {}; + obj.label = element.label; + obj.value = element.value; + this.foundConsumers.push(obj); + this.maximumSearchLines--; + return true; + } else { + if (this.maximumSearchLines > 0) { + return true; + } else { + return false; + } + } } -export default FindLikeConsumers; +export default FindLikeConsumers; \ No newline at end of file diff --git a/package.json b/package.json index d5bdc9f..acdfead 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "GeoSanWeb", - "version": "0.2.1", + "version": "0.6.1", "description": "Map application for water companies", "repository": "git@bitbucket.org:nexusbr/geosanweb.git", "private": true, -- libgit2 0.21.2