diff --git a/src/components/Home.vue b/src/components/Home.vue index e2141b3..b7851bd 100644 --- a/src/components/Home.vue +++ b/src/components/Home.vue @@ -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 diff --git a/src/functions/weight-read-all.js b/src/functions/weight-read-all.js index 8b33098..37103cb 100644 --- a/src/functions/weight-read-all.js +++ b/src/functions/weight-read-all.js @@ -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', @@ -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), + }); };