Skip to content

Instantly share code, notes, and snippets.

@mohtada-h
Forked from arastu/snapp-total-price.js
Last active January 28, 2019 07:55
Show Gist options
  • Save mohtada-h/418f03e5d2d719877517bb308ac9dd17 to your computer and use it in GitHub Desktop.
Save mohtada-h/418f03e5d2d719877517bb308ac9dd17 to your computer and use it in GitHub Desktop.
(async function () {
const token = JSON.parse(localStorage.getItem('user')).token;
const headers = new Headers();
const url = 'https://web-api.snapp.ir/api/v1/ride/history';
const query = '?page=';
let total = 0;
let page = 1;
headers.append('Authorization', token);
headers.append('Content-Type', 'application/json');
while (true) {
const response = await fetch(`${url}${query}${page}`, {
method: 'GET',
headers
});
const data = await response.json();
if (data.rides.length === 0) {
break;
}
total += data.rides
.filter(it => it.latest_ride_status !== 6 && it.latest_ride_status !== 7)
.map(it => it.price)
.reduce((sum, price) => sum + price, 0);
page += 1;
}
console.log(total);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment