Skip to content

Commit

Permalink
Merge pull request #52 from bikingbadger/51-get-all-pagination-values…
Browse files Browse the repository at this point in the history
…-for-weight

Get all pagination values for weight
  • Loading branch information
bikingbadger authored Jun 17, 2020
2 parents 989b13b + 1c06985 commit a90755a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 52 deletions.
18 changes: 0 additions & 18 deletions src/components/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ export default {
},
];
/**
* It will also sort the data from oldest to newest based on the date
*/
this.weightData.sort((a, b) => {
const dateA = new Date(a.weightDate);
const dateB = new Date(b.weightDate);
/**
* Return result based on whether the date is bigger or smaller
* The return is based on sorting oldest to newest so
* newer values should come after older ones
*/
if (dateA > dateB) return 1;
if (dateA < dateB) return -1;
return 0;
});
// console.log(this.weightData);
/**
* Fill the array with empty dates where a weight has not been input
* This is to give the graph x-axis the correct number of days
Expand Down
115 changes: 81 additions & 34 deletions src/functions/weight-read-all.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require('dotenv').config();
const axios = require('axios');

exports.handler = (event, context, callback) => {
console.log('===============================================');
console.log('===============================================');
console.log(`${event.httpMethod}: ${event.path}`);
return axios({
const getResponse = async (after = null) => {
// Calculate size of the pagination
const size = `_size: 90`;

// Create a cursor if pagination value is given for the next page
// This value is used in the query parameter below
const cursor = after ? `,_cursor: "${after}"` : '';
// console.log(cursor);
return await axios({
url: 'https://graphql.fauna.com/graphql',
headers: {
'Content-Type': 'application/json',
Expand All @@ -15,36 +19,79 @@ exports.handler = (event, context, callback) => {
method: 'post',
data: {
query: `
query FindAllWeights {
allWeights(_size: 90) {
data {
weightDate
weightKilograms
}
before
after
query FindAllWeights {
allWeights(${size} ${cursor}) {
data {
weightDate
weightKilograms
}
before
after
}
`,
}
`,
},
})
.then((result) => {
const weightArray = result.data.data.allWeights.data;
console.log('weightArray',weightArray);
console.log('===============================================');
console.log('===============================================');
return callback(null, {
statusCode: 200,
body: JSON.stringify(weightArray),
});
})
.catch((error) => {
console.log(error);
console.log('===============================================');
console.log('===============================================');
return callback(null, {
statusCode: 400,
body: JSON.stringify(error),
});
});
});
};

const getWeights = async () => {
let response = await getResponse();
let weightArray = [];

// Push the array if there is data
response.data.data.allWeights.data &&
weightArray.push(response.data.data.allWeights.data);

// Find the next page
let nextPage = response.data.data.allWeights.after;

// Check if there is another page in the pagination, using recursion
// get the next values
while (nextPage) {
// Get the next page of results using the nextPage value returned
// by the previous query
response = await getResponse(nextPage);

// Push the array if there is data
response.data.data.allWeights.data &&
weightArray.push(response.data.data.allWeights.data);

// Find the next page
nextPage = response.data.data.allWeights.after;
}

// Return flattened the array that is for each page into a single array
return weightArray.flat(1);
};

exports.handler = async (event, context, callback) => {
console.log('===============================================');
console.log('===============================================');
console.log(`${event.httpMethod}: ${event.path}`);

const weightArray = await getWeights();

/**
* Sort the data from oldest to newest based on the date
*/
weightArray.sort((a, b) => {
const dateA = new Date(a.weightDate);
const dateB = new Date(b.weightDate);

/**
* Return result based on whether the date is bigger or smaller
* The return is based on sorting oldest to newest so
* newer values should come after older ones
*/
if (dateA > dateB) return 1;
if (dateA < dateB) return -1;
return 0;
});

console.log('===============================================');
console.log('===============================================');
return callback(null, {
statusCode: 200,
body: JSON.stringify(weightArray),
});
};

0 comments on commit a90755a

Please sign in to comment.