Skip to content

Commit

Permalink
fix: discard changes
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Aug 7, 2024
1 parent 2ad5b1f commit 6c4f914
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/library-authoring/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const useRevertLibraryChanges = () => {
mutationFn: revertLibraryChanges,
onSettled: (_data, _error, libraryId) => {
queryClient.invalidateQueries({ queryKey: libraryAuthoringQueryKeys.contentLibrary(libraryId) });
queryClient.invalidateQueries({ queryKey: ['content_search'] });
},
});
};
26 changes: 26 additions & 0 deletions src/library-authoring/library-info/LibraryInfo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,30 @@ describe('<LibraryInfo />', () => {

await waitFor(() => expect(axiosMock.history.post[0].url).toEqual(url));
});

it('should discard changes', async () => {
const url = getCommitLibraryChangesUrl(libraryData.id);
axiosMock.onDelete(url).reply(200);

render(<RootWrapper data={libraryData} />);
const discardButton = screen.getByRole('button', { name: /discard changes/i });
fireEvent.click(discardButton);

expect(await screen.findByText('Library changes reverted successfully')).toBeInTheDocument();

await waitFor(() => expect(axiosMock.history.delete[0].url).toEqual(url));
});

it('should show error on discard changes', async () => {
const url = getCommitLibraryChangesUrl(libraryData.id);
axiosMock.onDelete(url).reply(500);

render(<RootWrapper data={libraryData} />);
const discardButton = screen.getByRole('button', { name: /discard changes/i });
fireEvent.click(discardButton);

expect(await screen.findByText('There was an error reverting changes in the library.')).toBeInTheDocument();

await waitFor(() => expect(axiosMock.history.delete[0].url).toEqual(url));
});
});
21 changes: 7 additions & 14 deletions src/library-authoring/library-info/LibraryPublishStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback, useContext, useMemo } from 'react';
import classNames from 'classnames';
import { Button, Container, Stack } from '@openedx/paragon';
import { FormattedDate, FormattedTime, useIntl } from '@edx/frontend-platform/i18n';
import { useCommitLibraryChanges } from '../data/apiHooks';
import { useCommitLibraryChanges, useRevertLibraryChanges } from '../data/apiHooks';
import { ContentLibrary } from '../data/api';
import { ToastContext } from '../../generic/toast-context';
import messages from './messages';
Expand All @@ -14,6 +14,7 @@ type LibraryPublishStatusProps = {
const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
const intl = useIntl();
const commitLibraryChanges = useCommitLibraryChanges();
const revertLibraryChanges = useRevertLibraryChanges();
const { showToast } = useContext(ToastContext);

const commit = useCallback(() => {
Expand All @@ -25,9 +26,6 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
});
}, []);

/**
* TODO, the discard changes breaks the library.
* Discomment this when discard changes is fixed.
const revert = useCallback(() => {
revertLibraryChanges.mutateAsync(library.id)
.then(() => {
Expand All @@ -36,7 +34,6 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
showToast(intl.formatMessage(messages.revertErrorMsg));
});
}, []);
*/

const {
isPublished,
Expand Down Expand Up @@ -153,15 +150,11 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
<Button disabled={isPublished} onClick={commit}>
{intl.formatMessage(messages.publishButtonLabel)}
</Button>
{ /*
* TODO, the discard changes breaks the library.
* Discomment this when discard changes is fixed.
<div className="d-flex justify-content-end">
<Button disabled={isPublished} variant="link" onClick={revert}>
{intl.formatMessage(messages.discardChangesButtonLabel)}
</Button>
</div>
*/ }
<div className="d-flex justify-content-end">
<Button disabled={isPublished} variant="link" onClick={revert}>
{intl.formatMessage(messages.discardChangesButtonLabel)}
</Button>
</div>
</Stack>
</Container>
</Stack>
Expand Down

0 comments on commit 6c4f914

Please sign in to comment.