Skip to content

Commit

Permalink
feat: update code list for org (#14632)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrt95 authored Feb 13, 2025
1 parent 8828edd commit cde4e0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { queriesMock } from 'app-shared/mocks/queriesMock';
import { SelectedContextType } from '../../context/HeaderContext';
import { Route, Routes } from 'react-router-dom';

const updateCodeListButtonTextMock: string = 'Update Code List';
const uploadCodeListButtonTextMock: string = 'Upload Code List';
const deleteCodeListButtonTextMock: string = 'Delete Code List';
const codeListNameMock: string = 'codeListNameMock';
Expand All @@ -26,8 +27,13 @@ const mockOrgPath: string = '/testOrg';
jest.mock(
'../../../libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage',
() => ({
CodeListPage: ({ onDeleteCodeList, onUploadCodeList }: any) => (
CodeListPage: ({ onDeleteCodeList, onUpdateCodeList, onUploadCodeList }: any) => (
<div>
<button
onClick={() => onUpdateCodeList({ title: codeListNameMock, codeList: codeListMock })}
>
{updateCodeListButtonTextMock}
</button>
<button
onClick={() =>
onUploadCodeList(
Expand Down Expand Up @@ -95,6 +101,20 @@ describe('OrgContentLibrary', () => {
expect(codeListMenuElement).toBeInTheDocument();
});

it('calls onUpdateCodeList when onUpdateCodeList is triggered', async () => {
const user = userEvent.setup();
renderOrgContentLibraryWithCodeLists({ initialEntries: [mockOrgPath] });
await goToLibraryPage(user, 'code_lists');
const updateCodeListButton = screen.getByRole('button', { name: updateCodeListButtonTextMock });
await user.click(updateCodeListButton);
expect(queriesMock.updateCodeListForOrg).toHaveBeenCalledTimes(1);
expect(queriesMock.updateCodeListForOrg).toHaveBeenCalledWith(
org,
codeListNameMock,
codeListMock,
);
});

it('calls onUploadCodeList when onUploadCodeList is triggered', async () => {
const user = userEvent.setup();
renderOrgContentLibraryWithCodeLists({ initialEntries: [mockOrgPath] });
Expand Down
11 changes: 9 additions & 2 deletions frontend/dashboard/pages/OrgContentLibrary/OrgContentLibrary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ReactElement } from 'react';
import React, { useCallback } from 'react';
import { ResourceContentLibraryImpl } from '@studio/content-library';
import type { CodeListWithMetadata } from '@studio/content-library';
import { useUpdateOrgCodeListMutation } from 'app-shared/hooks/mutations/useUpdateOrgCodeListMutation';
import { useTranslation } from 'react-i18next';
import { isErrorUnknown } from 'app-shared/utils/ApiErrorUtils';
import type { ApiError } from 'app-shared/types/api/ApiError';
Expand All @@ -25,9 +27,14 @@ export function OrgContentLibrary(): ReactElement {
function OrgContentLibraryWithContext(): ReactElement {
const selectedContext = useSelectedContext();

const { mutate: updateOptionList } = useUpdateOrgCodeListMutation(selectedContext);
const { mutate: deleteCodeList } = useDeleteOrgCodeListMutation(selectedContext);

const handleUpload = useUploadCodeList(selectedContext);

const { mutate: deleteCodeList } = useDeleteOrgCodeListMutation(selectedContext);
const handleUpdate = ({ title, codeList }: CodeListWithMetadata): void => {
updateOptionList({ title, data: codeList });
};

const { getContentResourceLibrary } = new ResourceContentLibraryImpl({
pages: {
Expand All @@ -36,7 +43,7 @@ function OrgContentLibraryWithContext(): ReactElement {
codeListsData: [],
onDeleteCodeList: deleteCodeList,
onUpdateCodeListId: () => {},
onUpdateCodeList: () => {},
onUpdateCodeList: handleUpdate,
onUploadCodeList: handleUpload,
},
},
Expand Down

0 comments on commit cde4e0f

Please sign in to comment.