Skip to content

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Nov 13, 2023
1 parent 2d2a3b4 commit 9ea4cd5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import { useTranslation } from 'react-i18next';
import theme from 'src/theme';
import { useLocale } from 'src/hooks/useLocale';
import { currencyFormat } from 'src/lib/intlFormat';
import { dateFormatWithoutYear } from 'src/lib/intlFormat/intlFormat';
import { useAppointmentResultsQuery } from './AppointmentResults.generated';
import { CoachingPeriodEnum } from '../CoachingDetail';
import { MultilineSkeleton } from '../MultilineSkeleton';
import { MultilineSkeleton } from '../../../Shared/MultilineSkeleton';

const RootContainer = styled(Paper)(({ theme }) => ({
paddingTop: theme.spacing(1),
Expand All @@ -39,24 +40,18 @@ const AlignedTableCell = styled(TableCell)({

interface AppointmentResultsProps {
accountListId: string;
currency?: string;
period: CoachingPeriodEnum;
currency?: string;
}

export const AppointmentResults: React.FC<AppointmentResultsProps> = ({
accountListId,
currency,
period,
currency,
}) => {
const { t } = useTranslation();
const locale = useLocale();

const dayMonthFormat = (date: DateTime) =>
new Intl.DateTimeFormat(locale, {
day: 'numeric',
month: 'short',
}).format(date.toJSDate());

const { data, loading } = useAppointmentResultsQuery({
variables: {
accountListId,
Expand All @@ -66,23 +61,24 @@ export const AppointmentResults: React.FC<AppointmentResultsProps> = ({

const averages = useMemo(
() =>
Object.fromEntries(
[
'appointmentsScheduled',
'individualAppointments',
'monthlyDecrease',
'monthlyIncrease',
'newMonthlyPartners',
'newSpecialPledges',
'specialGifts',
].map((field) => [
field,
data
[
'appointmentsScheduled',
'individualAppointments',
'monthlyDecrease',
'monthlyIncrease',
'newMonthlyPartners',
'newSpecialPledges',
'specialGifts',
].reduce(
(averages, field) => ({
...averages,
[field]: data
? data.appointmentResults.reduce<number>((total, period) => {
return total + period[field];
}, 0) / data.appointmentResults.length
: 0,
]),
}),
{} as Record<string, number>,
),
[data],
);
Expand Down Expand Up @@ -117,7 +113,11 @@ export const AppointmentResults: React.FC<AppointmentResultsProps> = ({
<AlignedTableCell>{t('Appointments')}</AlignedTableCell>
{data?.appointmentResults.map(({ id, startDate }) => (
<AlignedTableCell key={id}>
{startDate && dayMonthFormat(DateTime.fromISO(startDate))}
{startDate &&
dateFormatWithoutYear(
DateTime.fromISO(startDate),
locale,
)}
</AlignedTableCell>
))}
<AlignedTableCell>{t('Average')}</AlignedTableCell>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Coaching/CoachingDetail/CoachingDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useLocale } from 'src/hooks/useLocale';
import DonationHistories from 'src/components/Dashboard/DonationHistories';
import { useGetDonationGraphQuery } from 'src/components/Reports/DonationsReport/GetDonationGraph.generated';
import { AppointmentResults } from './AppointmentResults/AppointmentResults';
import { MultilineSkeleton } from './MultilineSkeleton';
import { MultilineSkeleton } from '../../Shared/MultilineSkeleton';

export enum CoachingPeriodEnum {
Weekly = 'Weekly',
Expand Down
File renamed without changes.
15 changes: 14 additions & 1 deletion src/lib/intlFormat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import {
percentageFormat,
currencyFormat,
dayMonthFormat,
monthYearFormat,
dateFormat,
dateFormatShort,
dateFormatWithoutYear,
} from './intlFormat';

export { numberFormat, percentageFormat, currencyFormat, dayMonthFormat };
export {
numberFormat,
percentageFormat,
currencyFormat,
dayMonthFormat,
monthYearFormat,
dateFormat,
dateFormatShort,
dateFormatWithoutYear,
};
14 changes: 14 additions & 0 deletions src/lib/intlFormat/intlFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ export const dateFormatShort = (date: DateTime, locale: string): string =>
year: 'numeric',
}).format(date.toJSDate());

export const dateFormatWithoutYear = (
date: DateTime | null,
locale: string,
): string => {
if (date === null) {
return '';
}
return new Intl.DateTimeFormat(locale, {
day: 'numeric',
month: 'short',
year: 'numeric',
}).format(date.toJSDate());
};

const intlFormat = {
numberFormat,
percentageFormat,
Expand Down

0 comments on commit 9ea4cd5

Please sign in to comment.