-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path\
114 lines (102 loc) · 4.22 KB
/
\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from "react";
import { View, Text } from "react-native";
import firebasDateToDate from "../../../firebasDateToDate";
import getUserWeightLiftedProgressDataByWeekForGraph from "./getUserWeightLiftedProgressDataByWeekForGraph";
import getUserWeightLiftedProgressDataByMonthForGraph from "./getUserWeightLiftedProgressDataByMonthForGraph";
/** getUserWeightLiftedProgressDataForGraph
* @param {object} weightLiftedRecord - the user's weight lifted record object
* @returns {array} - an array of objects that can be used to create a graph of the user's weight lifted progress over time
* @description - This function will take in the user's weight lifted history (record) and return an array of
* objects that can be used to create a graph of the user's weight lifted progress over time.
* The function should return an array of objects, in which each object represents a weight lifted record,
* if there is no record for a date, the weight lifted record for that date should be 0
* The first record will have the same weight lifted as the first record.
* If the weight lifted record is empty, the function should return an empty array.
*/
export default function getUserWeightLiftedProgressDataForGraph({
weightLiftedRecord,
}) {
if (weightLiftedRecord.length === 0) {
return [];
}
for (let i = 0; i < weightLiftedRecord.length; i++) {
weightLiftedRecord[i].date = firebasDateToDate(weightLiftedRecord[i].date);
}
weightLiftedRecord.sort((a, b) => {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
return dateA - dateB;
});
let weightLiftedProgressData = [];
const today = new Date();
let currentDate = weightLiftedRecord[0].date;
let currentWeightLifted = weightLiftedRecord[0].weightLifted;
let nextRecordIndex = 1;
let nextRecordDate = null;
if (nextRecordIndex < weightLiftedRecord.length) {
nextRecordDate = weightLiftedRecord[nextRecordIndex].date;
}
let totalWeightLifted = currentWeightLifted;
while (currentDate <= today) {
// first check if the next date is the same as the current one
if (
nextRecordDate &&
currentDate.getFullYear() === nextRecordDate.getFullYear() &&
currentDate.getMonth() === nextRecordDate.getMonth() &&
currentDate.getDate() === nextRecordDate.getDate()
) {
currentWeightLifted = weightLiftedRecord[nextRecordIndex].weightLifted;
totalWeightLifted += currentWeightLifted;
nextRecordIndex++;
if (nextRecordIndex < weightLiftedRecord.length) {
nextRecordDate = weightLiftedRecord[nextRecordIndex].date;
}
// check if there are no more records with the same date
while (
nextRecordDate &&
currentDate.getFullYear() === nextRecordDate.getFullYear() &&
currentDate.getMonth() === nextRecordDate.getMonth() &&
currentDate.getDate() === nextRecordDate.getDate()
) {
nextRecordIndex++;
if (nextRecordIndex < weightLiftedRecord.length) {
nextRecordDate = weightLiftedRecord[nextRecordIndex].date;
} else {
nextRecordDate = null;
}
}
}
// at the end of the loop, add the current date and weight lifted to the progress data
weightLiftedProgressData.push({
date: `${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}`,
value: currentWeightLifted,
dataPointText: currentWeightLifted.toString(),
label: (
<View style={{ width: 20, marginLeft: 10 }}>
<Text
style={{ color: "#a0a0a0", fontSize: 10 }}
>{`${currentDate.getMonth() + 1}/${currentDate.getDate()}`}</Text>
</View>
),
});
currentDate.setDate(currentDate.getDate() + 1);
currentWeightLifted = 0;
}
weightLiftedProgressData.map((dataPoint) => {
console.log(dataPoint);
});
const weightLiftedProgressDataByWeek =
getUserWeightLiftedProgressDataByWeekForGraph({
weightLiftedProgressData,
});
const weightLiftedProgressDataByMonth =
getUserWeightLiftedProgressDataByMonthForGraph({
weightLiftedProgressData,
});
return {
weightLiftedProgressData,
totalWeightLifted: Math.round(totalWeightLifted),
weightLiftedProgressDataByWeek,
weightLiftedProgressDataByMonth,
};
}