Feature/analytics#20
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| const data = useMemo(() => { | ||
| const billsReduce = bills.reduce((prev, curr) => { | ||
| const dueDate = new Date(curr.dueDate); | ||
| const month = `${dueDate.getMonth() + 1}/${dueDate.getFullYear()}`; |
There was a problem hiding this comment.
I would rename the variable name month to FormattedDueDate, as it doesn't actually represent the month, but rather the month/year of the due date
| if (!prev[month]) { | ||
| prev[month] = {}; | ||
|
|
||
| if (curr.type === BillTypes.EXPENSE) { |
There was a problem hiding this comment.
From what I understand, this first check is to see if there are already values for that specific month/year, if not yet, I put the curr values as default
If my understanding is right
This part is not necessary if you initialize the expense and income values to 0
Because in the next if you check if curr.type === billTypes.EXPENSE and do a prev[month].expense += curr.value;
If not, do: prev[month].income += curr.value;
Thus, repeating the code
So if the values are initialized with zero, the += operation will work correctly and for cases where the information in the month/year did not yet exist, it becomes the curr value, exactly what the first if does
There was a problem hiding this comment.
if (!prev[month]) {
prev[month] = {
expense: 0,
income: 0,
month,
rest: 0
};
}There was a problem hiding this comment.
But thinking here,
the code checks if there is no value in that table/year, if not, it initializes
there depending on whether the value of curr.type === BillTypes.EXPENSE
it initializes the expense with the value of curr.value
Then, as the value has already been initialized, it checks whether there is a value for the month/year ( if (prev?.[month]) )
as the prev for this month/year was initialized previously, it does: prev[month].expense += curr.value;
so it's as if it did a curr.values + curr.values for cases where the prev didn't yet have a value for that month
Screen.Recording.2023-11-21.at.21.28.16.mov