-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #828 from CruGlobal/7532-coaching-weekly-report
[MPDX-7532] Add coaching weekly report
- Loading branch information
Showing
6 changed files
with
290 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/components/Coaching/CoachingDetail/WeeklyReport/WeeklyReport.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
query WeeklyReports($accountListId: ID!) { | ||
coachingAnswerSets(accountListId: $accountListId, completed: true) { | ||
id | ||
answers { | ||
id | ||
response | ||
question { | ||
id | ||
position | ||
prompt | ||
} | ||
} | ||
completedAt | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/components/Coaching/CoachingDetail/WeeklyReport/WeeklyReport.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { render, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { DateTime, Settings } from 'luxon'; | ||
import { GqlMockedProvider } from '__tests__/util/graphqlMocking'; | ||
import { WeeklyReport } from './WeeklyReport'; | ||
import { WeeklyReportsQuery } from './WeeklyReport.generated'; | ||
|
||
const generateAnswer = (position: number, suffix: string) => ({ | ||
response: `Answer ${position}${suffix}`, | ||
question: { | ||
position, | ||
prompt: `Question ${position}${suffix}`, | ||
}, | ||
}); | ||
|
||
const mocks = { | ||
WeeklyReports: { | ||
coachingAnswerSets: [ | ||
{ | ||
completedAt: DateTime.local(2023, 11, 1).toISO(), | ||
answers: [ | ||
generateAnswer(3, 'a'), | ||
generateAnswer(1, 'a'), | ||
generateAnswer(2, 'a'), | ||
], | ||
}, | ||
{ | ||
completedAt: DateTime.local(2022, 10, 1).toISO(), | ||
answers: [ | ||
generateAnswer(3, 'b'), | ||
generateAnswer(1, 'b'), | ||
generateAnswer(2, 'b'), | ||
], | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
const accountListId = 'account-list-1'; | ||
|
||
const TestComponent: React.FC = () => ( | ||
<GqlMockedProvider<{ WeeklyReports: WeeklyReportsQuery }> mocks={mocks}> | ||
<WeeklyReport accountListId={accountListId} /> | ||
</GqlMockedProvider> | ||
); | ||
|
||
describe('WeeklyReport', () => { | ||
beforeEach(() => { | ||
Settings.now = () => new Date(2023, 12, 1).valueOf(); | ||
}); | ||
|
||
describe('previous/next buttons', () => { | ||
it('both buttons are disabled until the reports load', () => { | ||
const { getAllByTestId, getByRole } = render(<TestComponent />); | ||
|
||
expect(getAllByTestId('MultilineSkeletonLine')).not.toHaveLength(0); | ||
expect(getByRole('button', { name: 'Next' })).toBeDisabled(); | ||
expect(getByRole('button', { name: 'Previous' })).toBeDisabled(); | ||
}); | ||
|
||
it('next button is disabled when on the latest report', async () => { | ||
const { getByRole, getByTestId, queryByTestId } = render( | ||
<TestComponent />, | ||
); | ||
|
||
await waitFor(() => | ||
expect(queryByTestId('MultilineSkeletonLine')).not.toBeInTheDocument(), | ||
); | ||
|
||
const next = getByRole('button', { name: 'Next' }); | ||
const previous = getByRole('button', { name: 'Previous' }); | ||
const completed = getByTestId('CompletedText'); | ||
|
||
expect(next).toBeDisabled(); | ||
expect(previous).not.toBeDisabled(); | ||
expect(completed).toHaveTextContent('Nov 1'); | ||
|
||
userEvent.click(previous); | ||
expect(next).not.toBeDisabled(); | ||
expect(previous).toBeDisabled(); | ||
expect(completed).toHaveTextContent('Oct 1, 2022'); | ||
|
||
userEvent.click(next); | ||
expect(next).toBeDisabled(); | ||
expect(previous).not.toBeDisabled(); | ||
expect(completed).toHaveTextContent('Nov 1'); | ||
}); | ||
}); | ||
|
||
it('renders the questions and answers sorted by position', async () => { | ||
const { findAllByTestId } = render(<TestComponent />); | ||
|
||
const answers = await findAllByTestId('Answer'); | ||
expect(answers).toHaveLength(3); | ||
expect(answers[0]).toHaveTextContent('Question 1aAnswer 1a'); | ||
expect(answers[1]).toHaveTextContent('Question 2aAnswer 2a'); | ||
expect(answers[2]).toHaveTextContent('Question 3aAnswer 3a'); | ||
}); | ||
|
||
it('renders placeholder when there are no reports', async () => { | ||
const { findByText } = render( | ||
<GqlMockedProvider<{ WeeklyReports: WeeklyReportsQuery }> | ||
mocks={{ WeeklyReports: { coachingAnswerSets: [] } }} | ||
> | ||
<WeeklyReport accountListId={accountListId} /> | ||
</GqlMockedProvider>, | ||
); | ||
|
||
expect(await findByText('No completed reports found')).toBeInTheDocument(); | ||
}); | ||
}); |
156 changes: 156 additions & 0 deletions
156
src/components/Coaching/CoachingDetail/WeeklyReport/WeeklyReport.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { ChevronLeft, ChevronRight } from '@mui/icons-material'; | ||
import { | ||
Button, | ||
ButtonGroup, | ||
CardContent, | ||
CardHeader, | ||
Typography, | ||
} from '@mui/material'; | ||
import { styled } from '@mui/material/styles'; | ||
import { DateTime } from 'luxon'; | ||
import { useTranslation } from 'react-i18next'; | ||
import AnimatedCard from 'src/components/AnimatedCard'; | ||
import { MultilineSkeleton } from 'src/components/Shared/MultilineSkeleton'; | ||
import { useLocale } from 'src/hooks/useLocale'; | ||
import { dateFormat, dateFormatWithoutYear } from 'src/lib/intlFormat'; | ||
import { useWeeklyReportsQuery } from './WeeklyReport.generated'; | ||
|
||
const Header = styled(Typography)(({ theme }) => ({ | ||
display: 'flex', | ||
gap: theme.spacing(2), | ||
'@media (max-width: 900px)': { | ||
flexDirection: 'column', | ||
alignItems: 'center', | ||
}, | ||
})); | ||
|
||
const CompletedText = styled('span')({ | ||
textAlign: 'center', | ||
flex: 1, | ||
}); | ||
|
||
const StyledButton = styled(Button)({ | ||
width: '6rem', | ||
}); | ||
|
||
const ContentContainer = styled(CardContent)(({ theme }) => ({ | ||
padding: theme.spacing(2), | ||
overflowX: 'scroll', | ||
})); | ||
|
||
const AnswersContainer = styled('div')(({ theme }) => ({ | ||
display: 'grid', | ||
gridTemplateColumns: 'repeat(3, 1fr)', | ||
gap: theme.spacing(4), | ||
'@media (max-width: 1150px)': { | ||
gridTemplateColumns: 'repeat(2, 1fr)', | ||
}, | ||
'@media (max-width: 900px)': { | ||
gridTemplateColumns: '1fr', | ||
}, | ||
})); | ||
|
||
const AnswerContainer = styled('div')(({ theme }) => ({ | ||
padding: theme.spacing(1), | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: theme.spacing(2), | ||
})); | ||
|
||
const QuestionText = styled('p')({ | ||
fontWeight: 'bold', | ||
}); | ||
|
||
interface WeeklyReportProps { | ||
accountListId: string; | ||
} | ||
|
||
export const WeeklyReport: React.FC<WeeklyReportProps> = ({ | ||
accountListId, | ||
}) => { | ||
const { t } = useTranslation(); | ||
const locale = useLocale(); | ||
|
||
const [reportIndex, setReportIndex] = useState(0); | ||
|
||
const { data, loading } = useWeeklyReportsQuery({ | ||
variables: { accountListId }, | ||
}); | ||
const reports = data?.coachingAnswerSets ?? []; | ||
const report = reports[reportIndex]; | ||
|
||
// Sort the answers by their position | ||
const answers = useMemo(() => { | ||
if (!report) { | ||
return []; | ||
} | ||
|
||
const answers = report.answers.slice(0); | ||
answers.sort( | ||
(answer1, answer2) => | ||
answer1.question.position - answer2.question.position, | ||
); | ||
return answers; | ||
}, [report]); | ||
|
||
const completedDate = useMemo(() => { | ||
if (!report?.completedAt) { | ||
return null; | ||
} | ||
|
||
const date = DateTime.fromISO(report.completedAt); | ||
const format = | ||
date.year === DateTime.now().year ? dateFormatWithoutYear : dateFormat; | ||
return format(date, locale); | ||
}, [report, locale]); | ||
|
||
return ( | ||
<AnimatedCard> | ||
<CardHeader | ||
title={ | ||
<Header variant="h6"> | ||
<span>{t('Weekly Report')}</span> | ||
<CompletedText data-testid="CompletedText"> | ||
{completedDate} | ||
</CompletedText> | ||
<ButtonGroup> | ||
<StyledButton | ||
onClick={() => setReportIndex(reportIndex + 1)} | ||
disabled={reportIndex >= reports.length - 1} | ||
> | ||
<ChevronLeft /> | ||
{t('Previous')} | ||
</StyledButton> | ||
<StyledButton | ||
onClick={() => setReportIndex(reportIndex - 1)} | ||
disabled={reportIndex === 0} | ||
> | ||
{t('Next')} | ||
<ChevronRight /> | ||
</StyledButton> | ||
</ButtonGroup> | ||
</Header> | ||
} | ||
/> | ||
<ContentContainer> | ||
{loading ? ( | ||
<MultilineSkeleton lines={4} height={80} /> | ||
) : reports.length === 0 ? ( | ||
t('No completed reports found') | ||
) : ( | ||
<AnswersContainer> | ||
{answers.map((answer) => ( | ||
<AnswerContainer key={answer.id} data-testid="Answer"> | ||
<QuestionText role="heading"> | ||
{answer.question.prompt} | ||
</QuestionText> | ||
<p>{answer.response}</p> | ||
</AnswerContainer> | ||
))} | ||
</AnswersContainer> | ||
)} | ||
</ContentContainer> | ||
</AnimatedCard> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters