Skip to content

Commit

Permalink
feat: display all keyword set pages in read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
jorilindell committed May 16, 2024
1 parent b5e8149 commit 4b976bb
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 498 deletions.
6 changes: 5 additions & 1 deletion src/domain/admin/layout/hooks/useAdminFormStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { useMemo } from 'react';

import {
KeywordFieldsFragment,
KeywordSetFieldsFragment,
OrganizationFieldsFragment,
} from '../../../../generated/graphql';
import styles from '../form.module.scss';

type UseAdminFormStylesProps = {
isEditingAllowed: boolean;
instance?: KeywordFieldsFragment | OrganizationFieldsFragment;
instance?:
| KeywordFieldsFragment
| KeywordSetFieldsFragment
| OrganizationFieldsFragment;
};

type UseAdminFormStylesState = {
Expand Down
10 changes: 5 additions & 5 deletions src/domain/app/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"noRightsUpdateImageLabel": "No permissions to edit images.",
"noRightsUpdateKeyword": "Keywords cannot be edited through the service.",
"noRightsUpdateKeywordLabel": "Keywords cannot be edited.",
"noRightsUpdateKeywordSet": "You do not have permission to edit keyword sets.",
"noRightsUpdateKeywordSetLabel": "No permissions to edit keyword sets.",
"noRightsUpdateKeywordSet": "Keyword sets cannot be edited through the service.",
"noRightsUpdateKeywordSetLabel": "Keyword sets cannot be edited.",
"noRightsUpdateOrganization": "You do not have permission to edit organizations.",
"noRightsUpdateOrganizationLabel": "No permissions to edit organizations.",
"noRightsUpdatePlace": "You do not have permission to edit places.",
Expand Down Expand Up @@ -1540,7 +1540,7 @@
"labelUsage": "Usage",
"notificationKeywordSetDeleted": "The keyword set has been deleted",
"notificationKeywordSetUpdated": "The keyword set has been saved",
"notificationTitleCannotEdit": "The keyword set cannot be edited"
"notificationTitleCannotEdit": "Keyword sets cannot be edited"
}
},
"keywordSetsPage": {
Expand Down Expand Up @@ -1572,8 +1572,8 @@
"audience": "Audience",
"keyword": "Keyword"
},
"warningNoRightsToCreate": "You have insufficient rights to create keyword sets.",
"warningNoRightsToEdit": "You have insufficient rights to edit this keyword set."
"warningNoRightsToCreate": "Keyword sets cannot be edited through the service.",
"warningNoRightsToEdit": "Keyword sets cannot be edited through the service."
},
"landingPage": {
"heroTitle": "Events and hobbies in Helsinki",
Expand Down
10 changes: 5 additions & 5 deletions src/domain/app/i18n/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"noRightsUpdateImageLabel": "Ei oikeuksia muokata kuvia.",
"noRightsUpdateKeyword": "Avainsanoja ei voi muokata palvelun kautta.",
"noRightsUpdateKeywordLabel": "Avainsanoja ei voi muokata.",
"noRightsUpdateKeywordSet": "Sinulla ei ole oikeuksia muokata avainsanaryhmiä.",
"noRightsUpdateKeywordSetLabel": "Ei oikeuksia muokata avainsanaryhmiä.",
"noRightsUpdateKeywordSet": "Avainsanaryhmiä ei voi muokata palvelun kautta.",
"noRightsUpdateKeywordSetLabel": "Avainsanaryhmiä ei voi muokata.",
"noRightsUpdateOrganization": "Sinulla ei ole oikeuksia muokata organisaatioita.",
"noRightsUpdateOrganizationLabel": "Ei oikeuksia muokata organisaatioita.",
"noRightsUpdatePlace": "Sinulla ei ole oikeuksia muokata paikkoja.",
Expand Down Expand Up @@ -1540,7 +1540,7 @@
"labelUsage": "Käyttötarkoitus",
"notificationKeywordSetDeleted": "Avainsanaryhmä on poistettu",
"notificationKeywordSetUpdated": "Avainsanaryhmä on tallennettu",
"notificationTitleCannotEdit": "Avainsanaryhmää ei voi muokata"
"notificationTitleCannotEdit": "Avainsanaryhmiä ei voi muokata"
}
},
"keywordSetsPage": {
Expand Down Expand Up @@ -1572,8 +1572,8 @@
"audience": "Kohderyhmä",
"keyword": "Avainsana"
},
"warningNoRightsToCreate": "Sinulla ei ole oikeuksia luoda avainsanaryhmiä.",
"warningNoRightsToEdit": "Sinulla ei ole oikeuksia muokata tätä avainsanaryhmää."
"warningNoRightsToCreate": "Avainsanaryhmiä ei voi muokata palvelun kautta.",
"warningNoRightsToEdit": "Avainsanaryhmiä ei voi muokata palvelun kautta."
},
"landingPage": {
"heroTitle": "Helsingin tapahtumat ja harrastukset",
Expand Down
10 changes: 5 additions & 5 deletions src/domain/app/i18n/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"noRightsUpdateImageLabel": "Inga rättigheter att redigera bilder.",
"noRightsUpdateKeyword": "Nyckelord kan inte redigeras via tjänsten.",
"noRightsUpdateKeywordLabel": "Nyckelord kan inte redigeras.",
"noRightsUpdateKeywordSet": "Du har inte rättigheter att redigera nyckelordsuppsättningar.",
"noRightsUpdateKeywordSetLabel": "Inga rättigheter att redigera nyckelordsuppsättningar.",
"noRightsUpdateKeywordSet": "Nyckelordsuppsättningar kan inte redigeras via tjänsten.",
"noRightsUpdateKeywordSetLabel": "Nyckelordsuppsättningar kan inte redigeras.",
"noRightsUpdateOrganization": "Du har inte rättigheter att redigera organisationer.",
"noRightsUpdateOrganizationLabel": "Inga rättigheter att redigera organisationer.",
"noRightsUpdatePlace": "Du har inte rättigheter att redigera platser.",
Expand Down Expand Up @@ -1540,7 +1540,7 @@
"labelUsage": "Användning",
"notificationKeywordSetDeleted": "Nyckelordsuppsättningen har tagits bort",
"notificationKeywordSetUpdated": "Nyckelordsuppsättningen har sparats",
"notificationTitleCannotEdit": "Nyckelordsuppsättningen kan inte redigeras"
"notificationTitleCannotEdit": "Nyckelordsuppsättningar kan inte redigeras"
}
},
"keywordSetsPage": {
Expand Down Expand Up @@ -1572,8 +1572,8 @@
"audience": "Målgrupp",
"keyword": "Nyckelord"
},
"warningNoRightsToCreate": "Du har inte tillräckliga rättigheter för att skapa nyckelordsuppsättningar.",
"warningNoRightsToEdit": "Du har inte tillräckliga rättigheter för att redigera denna nyckelordsuppsättning."
"warningNoRightsToCreate": "Nyckelordsuppsättningar kan inte redigeras via tjänsten.",
"warningNoRightsToEdit": "Nyckelordsuppsättningar kan inte redigeras via tjänsten."
},
"landingPage": {
"heroTitle": "Evenemang och hobbyer i Helsingfors",
Expand Down
7 changes: 0 additions & 7 deletions src/domain/app/routes/adminRoutes/AdminRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import {
const CreateImagePage = React.lazy(
() => import('../../../image/CreateImagePage')
);
const CreateKeywordSetPage = React.lazy(
() => import('../../../keywordSet/CreateKeywordSetPage')
);
const CreateOrganizationPage = React.lazy(
() => import('../../../organization/CreateOrganizationPage')
);
Expand Down Expand Up @@ -93,10 +90,6 @@ const AdminPageRoutes: React.FC = () => {
element={<KeywordsPage />}
/>

<Route
path={getAdminRoutePath(ROUTES.CREATE_KEYWORD_SET)}
element={<CreateKeywordSetPage />}
/>
<Route
path={getAdminRoutePath(ROUTES.EDIT_KEYWORD_SET)}
element={<EditKeywordSetPage />}
Expand Down
18 changes: 0 additions & 18 deletions src/domain/app/routes/localeRoutes/__tests__/LocaleRoutes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -564,24 +564,6 @@ it('should render keyword sets page', async () => {
});
});

it('should render create keyword set page', async () => {
const { history } = renderRoute({
mocks: [
mockedOrganizationResponse,
mockedKeywordSelectorKeywordsResponse,
mockedSuperuserResponse,
mockedOrganizationAncestorsResponse,
],
route: `${ROUTES.CREATE_KEYWORD_SET}`,
});

await isPageRendered({
history,
pageTitle: 'Lisää avainsanaryhmä - Linked Events',
pathname: '/fi/administration/keyword-sets/create',
});
});

it('should render edit keyword set page', async () => {
const id = getValue(keywordSet.id, '');
const { history } = renderRoute({
Expand Down
35 changes: 2 additions & 33 deletions src/domain/keywordSet/EditKeywordSetPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-len */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Button } from 'hds-react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate, useParams } from 'react-router';
Expand All @@ -18,21 +17,15 @@ import getValue from '../../utils/getValue';
import PageWrapper from '../app/layout/pageWrapper/PageWrapper';
import TitleRow from '../app/layout/titleRow/TitleRow';
import { useNotificationsContext } from '../app/notificationsContext/hooks/useNotificationsContext';
import useAuth from '../auth/hooks/useAuth';
import NotFound from '../notFound/NotFound';
import useOrganizationAncestors from '../organization/hooks/useOrganizationAncestors';
import useUser from '../user/hooks/useUser';
import { KEYWORD_SET_ACTIONS } from './constants';
import useKeywordSetUpdateActions, {
KEYWORD_SET_MODALS,
} from './hooks/useKeywordSetActions';
import KeywordSetForm from './keywordSetForm/KeywordSetForm';
import ConfirmDeleteKeywordSetModal from './modals/confirmDeleteKeywordSetModal/ConfirmDeleteKeywordSetModal';
import {
getEditButtonProps,
getKeywordSetFields,
keywordSetPathBuilder,
} from './utils';
import { keywordSetPathBuilder } from './utils';

type Props = {
keywordSet: KeywordSetFieldsFragment;
Expand All @@ -43,12 +36,8 @@ const EditKeywordSetPage: React.FC<Props> = ({ keywordSet }) => {
const { addNotification } = useNotificationsContext();
const locale = useLocale();
const navigate = useNavigate();
const { authenticated } = useAuth();
const { organization } = getKeywordSetFields(keywordSet, locale);
const { user } = useUser();
const { organizationAncestors } = useOrganizationAncestors(organization);

const { closeModal, deleteKeywordSet, openModal, setOpenModal, saving } =
const { closeModal, deleteKeywordSet, openModal, saving } =
useKeywordSetUpdateActions({
keywordSet,
});
Expand All @@ -69,16 +58,6 @@ const EditKeywordSetPage: React.FC<Props> = ({ keywordSet }) => {
});
};

const buttonProps = getEditButtonProps({
action: KEYWORD_SET_ACTIONS.DELETE,
authenticated,
onClick: () => setOpenModal(KEYWORD_SET_MODALS.DELETE),
organizationAncestors,
organization,
t,
user,
});

return (
<div>
<ConfirmDeleteKeywordSetModal
Expand All @@ -99,16 +78,6 @@ const EditKeywordSetPage: React.FC<Props> = ({ keywordSet }) => {
]}
/>
}
button={
<Button
{...buttonProps}
fullWidth={true}
iconLeft={buttonProps.icon}
variant="danger"
>
{buttonProps.label}
</Button>
}
title={t('editKeywordSetPage.title')}
/>

Expand Down
120 changes: 4 additions & 116 deletions src/domain/keywordSet/__tests__/CreateKeywordSetPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@ import {
mockedKeywordResponse as mockedKeywordSelectorKeywordResponse,
mockedKeywordsResponse as mockedKeywordSelectorKeywordsResponse,
} from '../../../common/components/keywordSelector/__mocks__/keywordSelector';
import getValue from '../../../utils/getValue';
import { mockAuthenticatedLoginState } from '../../../utils/mockLoginHooks';
import {
configure,
loadingSpinnerIsNotInDocument,
render,
screen,
shouldApplyExpectedMetaData,
userEvent,
waitFor,
} from '../../../utils/testUtils';
import { mockedOrganizationResponse } from '../../organization/__mocks__/organization';
import { mockedOrganizationAncestorsResponse } from '../../organization/__mocks__/organizationAncestors';
import { mockedUserResponse } from '../../user/__mocks__/user';
import {
keywordSetValues,
mockedCreateKeywordSetResponse,
mockedInvalidCreateKeywordSetResponse,
} from '../__mocks__/createKeywordSetPage';
import CreateKeywordSetPage from '../CreateKeywordSetPage';

configure({ defaultHidden: true });
Expand All @@ -47,56 +39,7 @@ const defaultMocks = [
const renderComponent = (mocks: MockedResponse[] = defaultMocks) =>
render(<CreateKeywordSetPage />, { mocks });

const findElement = (key: 'saveButton') => {
switch (key) {
case 'saveButton':
return screen.findByRole('button', { name: /tallenna/i });
}
};

const getElement = (
key:
| 'keywordsToggleButton'
| 'nameInput'
| 'originIdInput'
| 'saveButton'
| 'usageToggleButton'
) => {
switch (key) {
case 'keywordsToggleButton':
return screen.getByRole('button', { name: /avainsanat/i });
case 'nameInput':
return screen.getByLabelText(/nimi \(suomeksi\)/i);
case 'originIdInput':
return screen.getByLabelText(/lähdetunniste/i);
case 'saveButton':
return screen.getByRole('button', { name: /tallenna/i });
case 'usageToggleButton':
return screen.getByRole('button', { name: /käyttötarkoitus/i });
}
};

const fillInputValues = async () => {
const user = userEvent.setup();
await user.type(getElement('originIdInput'), keywordSetValues.originId);
await user.type(getElement('nameInput'), keywordSetValues.name);

await user.click(getElement('keywordsToggleButton'));
const keywordsOption = await screen.findByRole(
'option',
{ name: getValue(keywordSetValues.keyword?.name?.fi, '') },
{ timeout: 10000 }
);
await user.click(keywordsOption);

await user.click(getElement('usageToggleButton'));
const usageOption = await screen.findByRole(
'option',
{ name: 'Yleinen' },
{ timeout: 10000 }
);
await user.click(usageOption);
};
const getNameInput = () => screen.getByLabelText(/nimi \(suomeksi\)/i);

test('applies expected metadata', async () => {
renderComponent();
Expand All @@ -109,65 +52,10 @@ test('applies expected metadata', async () => {
});
});

test('should focus to first validation error when trying to save new keyword set', async () => {
const user = userEvent.setup();
test('form should be disabled', async () => {
renderComponent();

await loadingSpinnerIsNotInDocument();

const saveButton = await findElement('saveButton');
await waitFor(() => expect(saveButton).toBeEnabled());

const originIdInput = getElement('originIdInput');
await user.click(saveButton);
await waitFor(() => expect(originIdInput).toHaveFocus());
await user.type(originIdInput, keywordSetValues.originId);

const nameInput = getElement('nameInput');
await user.click(saveButton);
await waitFor(() => expect(nameInput).toHaveFocus());
await user.type(nameInput, keywordSetValues.name);

const keywordsToggleButton = getElement('keywordsToggleButton');
await user.click(saveButton);

await waitFor(() => expect(keywordsToggleButton).toHaveFocus());
});

test('should move to keywords page after creating new keyword', async () => {
const user = userEvent.setup();
const { history } = renderComponent([
...defaultMocks,
mockedCreateKeywordSetResponse,
]);

await loadingSpinnerIsNotInDocument();

const saveButton = await findElement('saveButton');
await waitFor(() => expect(saveButton).toBeEnabled());

await fillInputValues();

await user.click(saveButton);

await waitFor(() =>
expect(history.location.pathname).toBe(`/fi/administration/keyword-sets`)
);
});

test('should show server errors', async () => {
const user = userEvent.setup();
renderComponent([...defaultMocks, mockedInvalidCreateKeywordSetResponse]);

await loadingSpinnerIsNotInDocument();

const saveButton = await findElement('saveButton');
await waitFor(() => expect(saveButton).toBeEnabled());

await fillInputValues();

await user.click(saveButton);

await screen.findByText(/lomakkeella on seuraavat virheet/i);
screen.getByText(/Tämän kentän arvo ei voi olla "null"./i);
const nameInput = getNameInput();
expect(nameInput).toHaveAttribute('readOnly');
});
Loading

0 comments on commit 4b976bb

Please sign in to comment.