Commit c4a627758a7fa3bde68ceee4fea1ebffb0acc295
1 parent
88fb7b9e
Exists in
master
and in
1 other branch
V. 0.6.1 - Funcionou a procura inteligente. Pode-se melhorar a performance agora
Showing
4 changed files
with
36 additions
and
48 deletions
Show diff stats
client/package.json
client/src/components/findConsumer.js
| ... | ... | @@ -16,10 +16,7 @@ export default class FindConsumer extends Component<*, State> { |
| 16 | 16 | const { selectedOption } = this.state; |
| 17 | 17 | const value = selectedOption && selectedOption.title; |
| 18 | 18 | const getOptions = input => { |
| 19 | - console.log("Input 1: " + input); | |
| 20 | 19 | return FindLikeConsumers(input).then(response => { |
| 21 | - console.log("Resposta do FindLikeConsumers"); | |
| 22 | - console.log(response); | |
| 23 | 20 | return { options: response }; |
| 24 | 21 | }); |
| 25 | 22 | }; | ... | ... |
client/src/components/findLikeConsumers.js
| 1 | 1 | // React client will ask nodejs server for the list of all consumers that match part of its name |
| 2 | 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 | 40 | -export default FindLikeConsumers; |
| 41 | +export default FindLikeConsumers; | |
| 50 | 42 | \ No newline at end of file | ... | ... |