getAllConsumers.js 925 Bytes
// Get an array with the list of all consumers
// React client will ask nodejs server for the list of all consumers
//
/**
 * Get all consumer names and codes
 */
function GetAllConsumers() {
	return new Promise(function(resolve, reject) {
		setTimeout(() => {
			var foundConsumers = [];
			fetch('consumidores').then((res) => {
				// asks node the list of all consumers
				res
					.json()
					.then((allConsumers) => {
						allConsumers.forEach((element) => {
							foundConsumers.push(element);
						});
					})
					.then((result) => {
						if (foundConsumers !== undefined) {
							const optionsArray = Array.from(new Array(foundConsumers.length), (_, index) => ({
								label: foundConsumers[index].label,
								value: foundConsumers[index].value
							}));
							resolve(optionsArray);
						} else {
							reject(undefined);
						}
					});
			});
		}, 3000);
	});
}

export default GetAllConsumers;