Skip to content
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

fix: discard changes [FC-0062] #58

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'] });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: is it really necessary to invalidate all cached data related to content search?

Nit: it would be nice to convert this to the query key pattern like we're doing above for library authoring query keys.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: is it really necessary to invalidate all cached data related to content search?

@bradenmacdonald Actually, I only want to invalidate content search queries related to this library. So updated the new PR: openedx#1214 to include this commit: openedx@9a3fc04

I tried changing content search queries to use query key pattern but it has a lot of dynamic variables and I could not think of a nice way to implement it.

},
});
};
40 changes: 40 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,44 @@ 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));
});

it('discard changes btn should be disabled for new libraries', async () => {
render(<RootWrapper data={{ ...libraryData, lastPublished: null, numBlocks: 0 }} />);
const discardButton = screen.getByRole('button', { name: /discard changes/i });

expect(discardButton).toBeDisabled();
});

it('discard changes btn should be enabled for new libraries if components are added', async () => {
render(<RootWrapper data={{ ...libraryData, lastPublished: null, numBlocks: 2 }} />);
const discardButton = screen.getByRole('button', { name: /discard changes/i });

expect(discardButton).not.toBeDisabled();
});
});
25 changes: 11 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,15 +34,16 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
showToast(intl.formatMessage(messages.revertErrorMsg));
});
}, []);
*/

const {
isPublished,
isNew,
statusMessage,
extraStatusMessage,
bodyMessage,
} = useMemo(() => {
let isPublishedResult: boolean;
let isNewResult = false;
let statusMessageResult : string;
let extraStatusMessageResult : string | undefined;
let bodyMessageResult : string | undefined;
Expand Down Expand Up @@ -94,6 +93,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {

if (!library.lastPublished) {
// Library is never published (new)
isNewResult = library.numBlocks === 0; // allow discarding if components are added
isPublishedResult = false;
statusMessageResult = intl.formatMessage(messages.draftStatusLabel);
extraStatusMessageResult = intl.formatMessage(messages.neverPublishedLabel);
Expand Down Expand Up @@ -123,6 +123,7 @@ const LibraryPublishStatus = ({ library } : LibraryPublishStatusProps) => {
}
return {
isPublished: isPublishedResult,
isNew: isNewResult,
statusMessage: statusMessageResult,
extraStatusMessage: extraStatusMessageResult,
bodyMessage: bodyMessageResult,
Expand Down Expand Up @@ -153,15 +154,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 || isNew} variant="link" onClick={revert}>
{intl.formatMessage(messages.discardChangesButtonLabel)}
</Button>
</div>
</Stack>
</Container>
</Stack>
Expand Down
Loading