Repository navigation
View for comparing published version of library block to previous [FC-0062] #1393
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
Merged
bradenmacdonald
merged 5 commits into
openedx:master
from
open-craft:braden/review-changes-modal
Oct 18, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d27341
feat: view for comparing published version of library block to previous
bradenmacdonald 52356f7
refactor: shuffle things around properly and fix i18n
bradenmacdonald 2175fd9
refactor: drive by fix: remove unneeded code
bradenmacdonald fde9199
test: add tests
bradenmacdonald f15a12e
refactor: Define 'VersionSpec' as an explicit type
bradenmacdonald 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
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
77 changes: 77 additions & 0 deletions
77
src/library-authoring/component-comparison/CompareChangesWidget.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,77 @@ | ||
| import { | ||
| fireEvent, | ||
| render, | ||
| screen, | ||
| initializeMocks, | ||
| within, | ||
| } from '../../testUtils'; | ||
|
|
||
| import CompareChangesWidget from './CompareChangesWidget'; | ||
|
|
||
| const usageKey = 'lb:org:lib:type:id'; | ||
|
|
||
| describe('<CompareChangesWidget />', () => { | ||
| beforeEach(() => { | ||
| initializeMocks(); | ||
| }); | ||
|
|
||
| it('can compare published (old) and draft (new) versions by default', async () => { | ||
| render(<CompareChangesWidget usageKey={usageKey} />); | ||
|
|
||
| // By default we see the new version: | ||
| const newTab = screen.getByRole('tab', { name: 'New version' }); | ||
| expect(newTab).toBeInTheDocument(); | ||
| expect(newTab).toHaveClass('active'); | ||
|
|
||
| const newTabPanel = screen.getByRole('tabpanel', { name: 'New version' }); | ||
| const newIframe = within(newTabPanel).getByTitle('Preview'); | ||
| expect(newIframe).toBeVisible(); | ||
| expect(newIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=draft`, | ||
| ); | ||
|
|
||
| // Now switch to the "old version" tab: | ||
| const oldTab = screen.getByRole('tab', { name: 'Old version' }); | ||
| fireEvent.click(oldTab); | ||
|
|
||
| const oldTabPanel = screen.getByRole('tabpanel', { name: 'Old version' }); | ||
| expect(oldTabPanel).toBeVisible(); | ||
| const oldIframe = within(oldTabPanel).getByTitle('Preview'); | ||
| expect(oldIframe).toBeVisible(); | ||
| expect(oldIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=published`, | ||
| ); | ||
| }); | ||
|
|
||
| it('can compare a specific old and published (new) version', async () => { | ||
| render(<CompareChangesWidget usageKey={usageKey} oldVersion={7} newVersion="published" />); | ||
|
|
||
| // By default we see the new version: | ||
| const newTab = screen.getByRole('tab', { name: 'New version' }); | ||
| expect(newTab).toBeInTheDocument(); | ||
| expect(newTab).toHaveClass('active'); | ||
|
|
||
| const newTabPanel = screen.getByRole('tabpanel', { name: 'New version' }); | ||
| const newIframe = within(newTabPanel).getByTitle('Preview'); | ||
| expect(newIframe).toBeVisible(); | ||
| expect(newIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=published`, | ||
| ); | ||
|
|
||
| // Now switch to the "old version" tab: | ||
| const oldTab = screen.getByRole('tab', { name: 'Old version' }); | ||
| fireEvent.click(oldTab); | ||
|
|
||
| const oldTabPanel = screen.getByRole('tabpanel', { name: 'Old version' }); | ||
| expect(oldTabPanel).toBeVisible(); | ||
| const oldIframe = within(oldTabPanel).getByTitle('Preview'); | ||
| expect(oldIframe).toBeVisible(); | ||
| expect(oldIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=7`, | ||
| ); | ||
| }); | ||
| }); |
43 changes: 43 additions & 0 deletions
43
src/library-authoring/component-comparison/CompareChangesWidget.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,43 @@ | ||
| import { useIntl } from '@edx/frontend-platform/i18n'; | ||
| import { Tab, Tabs } from '@openedx/paragon'; | ||
|
|
||
| import { LibraryBlock } from '../LibraryBlock'; | ||
|
|
||
| import messages from './messages'; | ||
|
|
||
| interface Props { | ||
| usageKey: string; | ||
| oldVersion?: number | 'draft' | 'published'; | ||
| newVersion?: number | 'draft' | 'published'; | ||
| } | ||
|
|
||
| /** | ||
| * A widget with tabs that can be used to show the old version and the new | ||
| * version of a component (XBlock), so users can switch back and forth to spot | ||
| * the differences. | ||
| * | ||
| * In the future, it would be better to have a way of highlighting the changes | ||
| * or showing a diff. | ||
| */ | ||
| const CompareChangesWidget = ({ usageKey, oldVersion = 'published', newVersion = 'draft' }: Props) => { | ||
| const intl = useIntl(); | ||
|
|
||
| return ( | ||
| <div> | ||
| <Tabs variant="tabs" defaultActiveKey="new" id="preview-version-toggle"> | ||
| <Tab eventKey="old" title={intl.formatMessage(messages.oldVersionTitle)}> | ||
| <div className="p-2 bg-white"> | ||
| <LibraryBlock usageKey={usageKey} version={oldVersion} /> | ||
| </div> | ||
| </Tab> | ||
| <Tab eventKey="new" title={intl.formatMessage(messages.newVersionTitle)}> | ||
| <div className="p-2 bg-white"> | ||
| <LibraryBlock usageKey={usageKey} version={newVersion} /> | ||
| </div> | ||
| </Tab> | ||
| </Tabs> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default CompareChangesWidget; |
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,22 @@ | ||
| import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
|
||
| const messages = defineMessages({ | ||
| oldVersionTitle: { | ||
| id: 'course-authoring.library-authoring.component-comparison.oldVersion', | ||
| defaultMessage: 'Old version', | ||
| description: 'Title shown for old version when comparing changes', | ||
| }, | ||
| newVersionTitle: { | ||
| id: 'course-authoring.library-authoring.component-comparison.newVersion', | ||
| defaultMessage: 'New version', | ||
| description: 'Title shown for new version when comparing changes', | ||
| }, | ||
| iframeTitlePrefix: { | ||
| // This is only used in the "PreviewChangesEmbed" iframe for the legacy UI | ||
| id: 'course-authoring.library-authoring.component-comparison.iframeTitlePrefix', | ||
| defaultMessage: 'Compare Changes', | ||
| description: 'Title used for the compare changes dialog', | ||
| }, | ||
| }); | ||
|
|
||
| export default messages; |
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
53 changes: 53 additions & 0 deletions
53
src/library-authoring/legacy-integration/PreviewChangesEmbed.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,53 @@ | ||
| import { mockContentLibrary, mockLibraryBlockMetadata } from '../data/api.mocks'; | ||
| import { | ||
| fireEvent, | ||
| render, | ||
| screen, | ||
| initializeMocks, | ||
| within, | ||
| } from '../../testUtils'; | ||
|
|
||
| import PreviewChangesEmbed from './PreviewChangesEmbed'; | ||
|
|
||
| mockContentLibrary.applyMock(); | ||
| mockLibraryBlockMetadata.applyMock(); | ||
| const usageKey = mockLibraryBlockMetadata.usageKeyWithCollections; | ||
|
|
||
| describe('<CompareChangesWidget />', () => { | ||
| beforeEach(() => { | ||
| initializeMocks(); | ||
| }); | ||
|
|
||
| it('can compare published (old) and draft (new) versions by default', async () => { | ||
| render(<PreviewChangesEmbed />, { | ||
| path: '/legacy/preview-changes/:usageKey', | ||
| routerProps: { initialEntries: [`/legacy/preview-changes/${usageKey}/?old=13`] }, | ||
| }); | ||
|
|
||
| // By default we see the new version (the published version): | ||
| const newTab = screen.getByRole('tab', { name: 'New version' }); | ||
| expect(newTab).toBeInTheDocument(); | ||
| expect(newTab).toHaveClass('active'); | ||
|
|
||
| const newTabPanel = screen.getByRole('tabpanel', { name: 'New version' }); | ||
| const newIframe = within(newTabPanel).getByTitle('Preview'); | ||
| expect(newIframe).toBeVisible(); | ||
| expect(newIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=published`, | ||
| ); | ||
|
|
||
| // Now switch to the "old version" tab: | ||
| const oldTab = screen.getByRole('tab', { name: 'Old version' }); | ||
| fireEvent.click(oldTab); | ||
|
|
||
| const oldTabPanel = screen.getByRole('tabpanel', { name: 'Old version' }); | ||
| expect(oldTabPanel).toBeVisible(); | ||
| const oldIframe = within(oldTabPanel).getByTitle('Preview'); | ||
| expect(oldIframe).toBeVisible(); | ||
| expect(oldIframe).toHaveAttribute( | ||
| 'src', | ||
| `http://localhost:18010/xblocks/v2/${usageKey}/embed/student_view/?version=13`, | ||
| ); | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
src/library-authoring/legacy-integration/PreviewChangesEmbed.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,44 @@ | ||
| import { useIntl } from '@edx/frontend-platform/i18n'; | ||
| import { useParams, useSearchParams } from 'react-router-dom'; | ||
| import { Helmet } from 'react-helmet'; | ||
|
|
||
| import { LibraryProvider } from '../common/context'; | ||
| import { getLibraryId } from '../../generic/key-utils'; | ||
| import CompareChangesWidget from '../component-comparison/CompareChangesWidget'; | ||
| import { useLibraryBlockMetadata } from '../data/apiHooks'; | ||
| import messages from '../component-comparison/messages'; | ||
|
|
||
| /** | ||
| * This view is only used to support the legacy UI. | ||
| * On the legacy unit page, when a v2 library block has been used in a course | ||
| * AND an updated version of that block is available, this view is rendered in | ||
| * an iframe in a modal, and allows the author to preview the changes before | ||
| * accepting them (before syncing the course version with the latest library | ||
| * version). | ||
| * | ||
| * The inner <CompareChangesWidget> will be used by this MFE as well, on the | ||
| * new MFE unit page. | ||
| */ | ||
| const PreviewChangesEmbed = () => { | ||
| const intl = useIntl(); | ||
| const { usageKey } = useParams(); | ||
| if (usageKey === undefined) { | ||
| // istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker. | ||
| throw new Error('Error: route is missing usageKey.'); | ||
| } | ||
| const [queryString] = useSearchParams(); | ||
| const oldVersion = parseInt(queryString.get('old') ?? '', 10) || 'published'; | ||
| const libraryId = getLibraryId(usageKey); | ||
| const { data: metadata } = useLibraryBlockMetadata(usageKey); | ||
|
|
||
| return ( | ||
| <LibraryProvider libraryId={libraryId}> | ||
| {/* It's not necessary since this will usually be in an <iframe>, | ||
| but it's good practice to set a title for any top level page */} | ||
| <Helmet><title>{intl.formatMessage(messages.iframeTitlePrefix)} | {metadata?.displayName ?? ''} | {process.env.SITE_NAME}</title></Helmet> | ||
| <CompareChangesWidget usageKey={usageKey} oldVersion={oldVersion} newVersion="published" /> | ||
| </LibraryProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default PreviewChangesEmbed; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.