Skip to content

Commit

Permalink
Merge pull request #14 from opencollective/fix/diffs
Browse files Browse the repository at this point in the history
Fix Percentage diffs
  • Loading branch information
Betree authored Nov 18, 2022
2 parents c57a1ed + 6cc2320 commit 5b32a46
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
18 changes: 12 additions & 6 deletions components/PercentageDiff.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import React from 'react';

import { formatAmount } from '../lib/amounts';

import { Span } from '@opencollective/frontend-components/components/Text';

export const PercentageDiff = ({ previousValue, newValue }) => {
if (newValue === 0 || previousValue === 0 || newValue === previousValue) {
return null;
export const PercentageDiff = ({ previousValue, newValue, currency }) => {
let percentage;
if (newValue && !previousValue) {
percentage = 100;
} else if (!newValue && previousValue) {
percentage = -100;
} else {
percentage = Math.round(((newValue - previousValue) / previousValue) * 100);
}

const percentage = Math.round(((newValue - previousValue) / previousValue) * 100);
if (!percentage) {
return null;
} else if (percentage > 0) {
return (
<Span color="green.700">
<Span color="green.700" title={`Previous period: ${formatAmount({ value: previousValue, currency })}`}>
<Span color="green.500"></Span> {percentage}%
</Span>
);
} else {
return (
<Span color="red.700">
<Span color="red.700" title={`Previous period: ${formatAmount({ value: previousValue, currency })}`}>
<Span color="red.500"></Span> {percentage}%
</Span>
);
Expand Down
7 changes: 7 additions & 0 deletions lib/amounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Amount } from './graphql/types/v2/graphql';

export const formatAmount = ({ value, currency }: Amount, abs = false) => {
return new Intl.NumberFormat('en-US', { style: 'currency', currency, maximumFractionDigits: 0 }).format(
abs ? Math.abs(value) : value,
);
};
57 changes: 35 additions & 22 deletions pages/funders-dashboard/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';
import { gql } from '@apollo/client';
import dayjs from 'dayjs';
import { isNil, uniqBy } from 'lodash';
import type { NextPageContext } from 'next';
import { useRouter } from 'next/router';
import { FormattedMessage } from 'react-intl';
import styled from 'styled-components';

import { formatAmount } from '../../lib/amounts';
import { initializeApollo } from '../../lib/apollo-client';
import { Amount } from '../../lib/graphql/types/v2/graphql';
import { useLoggedInUser } from '../../lib/hooks/useLoggedInUser';
import { parseDateInterval } from '@opencollective/frontend-components/lib/date-utils';

Expand All @@ -21,12 +22,6 @@ import LoadingPlaceholder from '@opencollective/frontend-components/components/L
import StyledLink from '@opencollective/frontend-components/components/StyledLink';
import { P, Span } from '@opencollective/frontend-components/components/Text';

const formatAmount = ({ value, currency }: Amount, abs = false) => {
return new Intl.NumberFormat('en-US', { style: 'currency', currency, maximumFractionDigits: 0 }).format(
abs ? Math.abs(value) : value,
);
};

const Table = styled.table`
width: 100%;
border-collapse: collapse;
Expand Down Expand Up @@ -60,7 +55,13 @@ const Table = styled.table`
`;

const funderQuery = gql`
query ContributorsDashboard($slug: String, $dateFrom: DateTime, $dateTo: DateTime) {
query ContributorsDashboard(
$slug: String
$dateFrom: DateTime
$dateTo: DateTime
$previousDateFrom: DateTime
$previousDateTo: DateTime
) {
account(slug: $slug) {
slug
name
Expand All @@ -78,29 +79,29 @@ const funderQuery = gql`
name
imageUrl(height: 80)
stats {
totalAmountReceivedPastMonth: totalAmountReceived(
totalAmountReceivedPeriod: totalAmountReceived(
dateFrom: $dateFrom
dateTo: $dateTo
includeChildren: true
) {
value
currency
}
totalAmountSpentPastMonth: totalAmountSpent(dateFrom: $dateFrom, dateTo: $dateTo, includeChildren: true) {
totalAmountSpentPeriod: totalAmountSpent(dateFrom: $dateFrom, dateTo: $dateTo, includeChildren: true) {
value
currency
}
totalAmountReceivedPreviousMonth: totalAmountReceived(
dateFrom: $dateFrom
dateTo: $dateTo
totalAmountReceivedPreviousPeriod: totalAmountReceived(
dateFrom: $previousDateFrom
dateTo: $previousDateTo
includeChildren: true
) {
value
currency
}
totalAmountSpentPreviousMonth: totalAmountSpent(
dateFrom: $dateFrom
dateTo: $dateTo
totalAmountSpentPreviousPeriod: totalAmountSpent(
dateFrom: $previousDateFrom
dateTo: $previousDateTo
includeChildren: true
) {
value
Expand Down Expand Up @@ -132,12 +133,22 @@ const funderQuery = gql`

const getVariablesFromQuery = query => {
const { from: dateFrom, to: dateTo } = parseDateInterval(query.period);
const [dateFromDayJs, dateToDayJs] = [dateFrom, dateTo].map(dayjs);
let previousDateFrom = dateFrom;
let previousDateTo = dateTo;
const period = dateToDayJs.diff(dateFromDayJs, 'day');
if (period > 0) {
previousDateFrom = dateFromDayJs.subtract(period, 'day').toISOString();
previousDateTo = dateToDayJs.subtract(period, 'day').toISOString();
}
return {
slug: query.slug,
offset: parseInt(query.offset) || 0,
limit: parseInt(query.limit) || 100,
dateFrom,
dateTo,
previousDateFrom,
previousDateTo,
};
};

Expand Down Expand Up @@ -245,20 +256,22 @@ export default function ContributorDashboard({ account = null }) {
</td>
<td style={{ textAlign: 'center' }}>{formatAmount(node.totalDonations)}</td>
<td style={{ textAlign: 'center' }}>
{formatAmount(node.account.stats.totalAmountReceivedPastMonth)}
{formatAmount(node.account.stats.totalAmountReceivedPeriod)}
<Span fontSize="14px" ml="8px">
<PercentageDiff
previousValue={node.account.stats.totalAmountReceivedPreviousMonth.value}
newValue={node.account.stats.totalAmountReceivedPastMonth.value}
previousValue={node.account.stats.totalAmountReceivedPeriod.value}
newValue={node.account.stats.totalAmountReceivedPeriod.value}
currency={node.account.stats.totalAmountReceivedPeriod.currency}
/>
</Span>
</td>
<td style={{ textAlign: 'center' }}>
{formatAmount(node.account.stats.totalAmountSpentPastMonth)}
{formatAmount(node.account.stats.totalAmountSpentPeriod)}
<Span fontSize="14px" ml="8px">
<PercentageDiff
previousValue={node.account.stats.totalAmountSpentPreviousMonth.value}
newValue={node.account.stats.totalAmountSpentPastMonth.value}
previousValue={node.account.stats.totalAmountSpentPreviousPeriod.value}
newValue={node.account.stats.totalAmountSpentPeriod.value}
currency={node.account.stats.totalAmountSpentPeriod.currency}
/>
</Span>
</td>
Expand Down

1 comment on commit 5b32a46

@vercel
Copy link

@vercel vercel bot commented on 5b32a46 Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.