-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/analytics #20
base: develop
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!prev[month]) {
prev[month] = {
expense: 0,
income: 0,
month,
rest: 0
};
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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