-
Notifications
You must be signed in to change notification settings - Fork 366
(feat) Improve appointments calendar with monthly, weekly, and daily drill-down views #2321
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
Open
Brijesh03032001
wants to merge
2
commits into
openmrs:main
Choose a base branch
from
Brijesh03032001:feat/improved-appointments-calendar-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
114 changes: 114 additions & 0 deletions
114
packages/esm-appointments-app/src/calendar/daily/daily-calendar-view.component.test.tsx
This file contains hidden or 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,114 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import dayjs from 'dayjs'; | ||
| import { appointmentsStore } from '../../store'; | ||
| import { omrsDateFormat } from '../../constants'; | ||
| import { type DailyAppointmentsCountByService } from '../../types'; | ||
| import DailyCalendarView from './daily-calendar-view.component'; | ||
|
|
||
| const SELECTED_DATE = '2024-01-15'; // Monday | ||
|
|
||
| const mockEvents: Array<DailyAppointmentsCountByService> = [ | ||
| { | ||
| appointmentDate: SELECTED_DATE, | ||
| services: [ | ||
| { serviceName: 'HIV Clinic', serviceUuid: 'uuid-hiv', count: 4 }, | ||
| { serviceName: 'TB Clinic', serviceUuid: 'uuid-tb', count: 1 }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| function setupStore(date = SELECTED_DATE) { | ||
| appointmentsStore.setState({ | ||
| selectedDate: dayjs(date).format(omrsDateFormat), | ||
| calendarView: 'daily', | ||
| appointmentServiceTypes: [], | ||
| }); | ||
| } | ||
|
|
||
| describe('DailyCalendarView', () => { | ||
| beforeEach(() => setupStore()); | ||
|
|
||
| it('renders Prev and Next day navigation buttons', () => { | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: /previous day/i })).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: /next day/i })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows no appointments message when there are no events for the selected day', () => { | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| expect(screen.getByText(/no appointments scheduled for this day/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows the service table when events exist for the selected date', () => { | ||
| render(<DailyCalendarView events={mockEvents} />); | ||
|
|
||
| expect(screen.getByText('HIV Clinic')).toBeInTheDocument(); | ||
| expect(screen.getByText('TB Clinic')).toBeInTheDocument(); | ||
| expect(screen.getByText('4')).toBeInTheDocument(); | ||
| expect(screen.getByText('1')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows total appointment count', () => { | ||
| render(<DailyCalendarView events={mockEvents} />); | ||
|
|
||
| expect(screen.getByText(/total appointments/i)).toBeInTheDocument(); | ||
| expect(screen.getByText('5')).toBeInTheDocument(); // 4 + 1 | ||
| }); | ||
|
|
||
| it('shows Today badge when the selected date is today', () => { | ||
| setupStore(dayjs().format('YYYY-MM-DD')); | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| expect(screen.getByText('Today')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('does not show Today badge for a past date', () => { | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| expect(screen.queryByText('Today')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('opens a modal when a service row is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| render(<DailyCalendarView events={mockEvents} />); | ||
|
|
||
| await user.click(screen.getByText('HIV Clinic')); | ||
|
|
||
| expect(screen.getByRole('dialog')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('modal contains service details', async () => { | ||
| const user = userEvent.setup(); | ||
| render(<DailyCalendarView events={mockEvents} />); | ||
|
|
||
| await user.click(screen.getByText('HIV Clinic')); | ||
|
|
||
| const dialog = screen.getByRole('dialog'); | ||
| expect(dialog).toHaveTextContent('HIV Clinic'); | ||
| expect(dialog).toHaveTextContent('TB Clinic'); | ||
| }); | ||
|
|
||
| it('navigates to the previous day when Prev is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: /previous day/i })); | ||
|
|
||
| const newDate = dayjs(appointmentsStore.getState().selectedDate); | ||
| expect(newDate.format('YYYY-MM-DD')).toBe('2024-01-14'); | ||
| }); | ||
|
|
||
| it('navigates to the next day when Next is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| render(<DailyCalendarView events={[]} />); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: /next day/i })); | ||
|
|
||
| const newDate = dayjs(appointmentsStore.getState().selectedDate); | ||
| expect(newDate.format('YYYY-MM-DD')).toBe('2024-01-16'); | ||
| }); | ||
| }); |
126 changes: 126 additions & 0 deletions
126
packages/esm-appointments-app/src/calendar/daily/daily-calendar-view.component.tsx
This file contains hidden or 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,126 @@ | ||
| import React, { useCallback, useMemo, useState } from 'react'; | ||
| import dayjs from 'dayjs'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { Button, Modal } from '@carbon/react'; | ||
| import { User } from '@carbon/react/icons'; | ||
| import { formatDate } from '@openmrs/esm-framework'; | ||
| import { omrsDateFormat } from '../../constants'; | ||
| import { useAppointmentsStore, setSelectedDate } from '../../store'; | ||
| import { type DailyAppointmentsCountByService } from '../../types'; | ||
| import styles from './daily-calendar-view.scss'; | ||
|
|
||
| interface DailyCalendarViewProps { | ||
| events: Array<DailyAppointmentsCountByService>; | ||
| } | ||
|
|
||
| const DailyCalendarView: React.FC<DailyCalendarViewProps> = ({ events }) => { | ||
| const { t } = useTranslation(); | ||
| const { selectedDate } = useAppointmentsStore(); | ||
| const [modalOpen, setModalOpen] = useState(false); | ||
|
|
||
| const handlePrevDay = useCallback(() => { | ||
| setSelectedDate(dayjs(selectedDate).subtract(1, 'day').format(omrsDateFormat)); | ||
| }, [selectedDate]); | ||
|
|
||
| const handleNextDay = useCallback(() => { | ||
| setSelectedDate(dayjs(selectedDate).add(1, 'day').format(omrsDateFormat)); | ||
| }, [selectedDate]); | ||
|
|
||
| const currentData = useMemo( | ||
| () => | ||
| events?.find((e) => dayjs(e.appointmentDate).format('YYYY-MM-DD') === dayjs(selectedDate).format('YYYY-MM-DD')), | ||
| [events, selectedDate], | ||
| ); | ||
|
|
||
| const totalCount = currentData?.services?.reduce((sum, { count = 0 }) => sum + count, 0) ?? 0; | ||
| const isToday = dayjs(selectedDate).isSame(dayjs(), 'day'); | ||
|
|
||
| return ( | ||
| <div className={styles.dailyContainer}> | ||
| <div className={styles.dailyHeader}> | ||
| <Button kind="tertiary" size="sm" onClick={handlePrevDay} aria-label={t('previousDay', 'Previous day')}> | ||
| {t('prev', 'Prev')} | ||
| </Button> | ||
| <div className={styles.dateDisplay}> | ||
| <span className={styles.dayName}>{dayjs(selectedDate).format('dddd')}</span> | ||
| <span className={styles.fullDate}> | ||
| {formatDate(new Date(selectedDate), { day: true, time: false, noToday: true })} | ||
| </span> | ||
| {isToday && <span className={styles.todayBadge}>{t('today', 'Today')}</span>} | ||
| </div> | ||
| <Button kind="tertiary" size="sm" onClick={handleNextDay} aria-label={t('nextDay', 'Next day')}> | ||
| {t('next', 'Next')} | ||
| </Button> | ||
| </div> | ||
|
|
||
| <div className={styles.dailyBody}> | ||
| <div className={styles.summaryCard}> | ||
| <div className={styles.summaryHeader}> | ||
| <User size={20} /> | ||
| <span className={styles.totalLabel}> | ||
| {t('totalAppointments', 'Total appointments')}: <strong>{totalCount}</strong> | ||
| </span> | ||
| </div> | ||
|
|
||
| {currentData?.services?.length ? ( | ||
| <table className={styles.serviceTable}> | ||
| <thead> | ||
| <tr> | ||
| <th>{t('service', 'Service')}</th> | ||
| <th>{t('appointments', 'Appointments')}</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {currentData.services.map(({ serviceName, serviceUuid, count }) => ( | ||
| <tr | ||
| key={serviceUuid} | ||
| role="button" | ||
| tabIndex={0} | ||
| className={styles.serviceRow} | ||
| onClick={() => setModalOpen(true)} | ||
| onKeyDown={(e) => e.key === 'Enter' && setModalOpen(true)}> | ||
| <td>{serviceName}</td> | ||
| <td>{count}</td> | ||
| </tr> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| ) : ( | ||
| <p className={styles.noData}>{t('noAppointmentsForDay', 'No appointments scheduled for this day.')}</p> | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| {modalOpen && ( | ||
| <Modal | ||
| open={modalOpen} | ||
| modalHeading={`${t('appointments', 'Appointments')} — ${dayjs(selectedDate).format('dddd, MMMM D YYYY')}`} | ||
| passiveModal | ||
| onRequestClose={() => setModalOpen(false)}> | ||
| {currentData?.services?.length ? ( | ||
| <table className={styles.serviceTable}> | ||
| <thead> | ||
| <tr> | ||
| <th>{t('service', 'Service')}</th> | ||
| <th>{t('count', 'Count')}</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {currentData.services.map(({ serviceName, serviceUuid, count }) => ( | ||
| <tr key={serviceUuid}> | ||
| <td>{serviceName}</td> | ||
| <td>{count}</td> | ||
| </tr> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| ) : ( | ||
| <p>{t('noAppointmentsForDay', 'No appointments scheduled for this day.')}</p> | ||
| )} | ||
| </Modal> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default DailyCalendarView; |
103 changes: 103 additions & 0 deletions
103
packages/esm-appointments-app/src/calendar/daily/daily-calendar-view.scss
This file contains hidden or 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,103 @@ | ||
| @use '@carbon/colors'; | ||
| @use '@carbon/layout'; | ||
| @use '@carbon/type'; | ||
|
|
||
| .dailyContainer { | ||
| margin: layout.$spacing-05; | ||
| } | ||
|
|
||
| .dailyHeader { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: layout.$spacing-05; | ||
| margin-bottom: layout.$spacing-06; | ||
| } | ||
|
|
||
| .dateDisplay { | ||
| flex: 1; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| gap: layout.$spacing-01; | ||
| } | ||
|
|
||
| .dayName { | ||
| @include type.type-style('heading-04'); | ||
| color: colors.$gray-100; | ||
| } | ||
|
|
||
| .fullDate { | ||
| @include type.type-style('body-compact-01'); | ||
| color: colors.$gray-60; | ||
| } | ||
|
|
||
| .todayBadge { | ||
| @include type.type-style('label-01'); | ||
| background-color: colors.$blue-60; | ||
| color: colors.$white; | ||
| border-radius: 1rem; | ||
| padding: layout.$spacing-01 layout.$spacing-03; | ||
| font-weight: 600; | ||
| } | ||
|
|
||
| .dailyBody { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: layout.$spacing-05; | ||
| } | ||
|
|
||
| .summaryCard { | ||
| background: colors.$white; | ||
| border: 1px solid colors.$gray-20; | ||
| border-radius: 4px; | ||
| padding: layout.$spacing-05; | ||
| } | ||
|
|
||
| .summaryHeader { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: layout.$spacing-03; | ||
| margin-bottom: layout.$spacing-05; | ||
| padding-bottom: layout.$spacing-04; | ||
| border-bottom: 1px solid colors.$gray-20; | ||
| } | ||
|
|
||
| .totalLabel { | ||
| @include type.type-style('heading-02'); | ||
| color: colors.$gray-100; | ||
| } | ||
|
|
||
| .serviceTable { | ||
| width: 100%; | ||
| border-collapse: collapse; | ||
|
|
||
| th, | ||
| td { | ||
| text-align: left; | ||
| padding: layout.$spacing-03 layout.$spacing-04; | ||
| border-bottom: 1px solid colors.$gray-20; | ||
| @include type.type-style('body-compact-01'); | ||
| } | ||
|
|
||
| th { | ||
| @include type.type-style('label-02'); | ||
| font-weight: 600; | ||
| background: colors.$gray-10; | ||
| } | ||
| } | ||
|
|
||
| .serviceRow { | ||
| cursor: pointer; | ||
| transition: background-color 0.15s ease; | ||
|
|
||
| &:hover { | ||
| background-color: colors.$blue-10; | ||
| } | ||
| } | ||
|
|
||
| .noData { | ||
| @include type.type-style('body-compact-01'); | ||
| color: colors.$gray-60; | ||
| font-style: italic; | ||
| padding: layout.$spacing-05 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consider using a component map here instead of three conditional renders. Makes it easier to extend without modifying this file.