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

Feat/migrate family menu #901

Merged
merged 4 commits into from
Aug 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/packages/components/page-title-block/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type PageTitleBlockTypes = {
titleLg1: string;
titleLg1?: string;
titleLg2?: string;
secondLang?: boolean;
};
Expand Down
8 changes: 8 additions & 0 deletions src/packages/model/operations/family.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ValidationState } from '../../components';

export type Family = {
id: string;
prefLabelLg1?: string;
prefLabelLg2?: string;
validationState: ValidationState;
};
2 changes: 1 addition & 1 deletion src/packages/modules-classifications/item/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const fetchingPreviousLevels = (classificationId, general) => {
general.broaderURI.lastIndexOf('/')
);
const previousLevel =
levels[levels.findIndex((level) => level.indexOf(currentLevel) === 0)];
levels[levels.findIndex((level) => level.startsWith(currentLevel))];

if (!!previousLevel) {
return ClassificationsApi.getClassificationLevelMembers(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import D from '../../../deprecated-locales';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import { Button, ActionToolbar, ReturnButton } from '@inseefr/wilco';
import {
Loading,
ErrorBloc,
ValidationButton,
PageTitleBlock,
CheckSecondLang,
} from '../../../components';

import { useGoBack } from '../../../utils/hooks/useGoBack';

import { useCallback, useEffect, useState } from 'react';
import OperationsFamilyVisualization from '../../../modules-operations/families/visualization/visualization';
import { containUnsupportedStyles } from '../../../utils/html-utils';
import { getLocales } from '../../../redux/selectors';
import { getSecondLang } from '../../../redux/second-lang';
import { OperationsApi } from '../../../sdk/operations-api';
import Auth from '../../../auth/components/auth';
import { ADMIN } from '../../../auth/roles';
const Family = () => {
const { id } = useParams();
const langs = useSelector((state) => getLocales(state));
const secondLang = useSelector((state) => getSecondLang(state));
const goBack = useGoBack();
import { Menu } from './menu';
import { Family } from '../../../model/operations/family';

const FamilyView = () => {
const { id } = useParams<{ id: string }>();
const langs = useSelector(getLocales);
const secondLang = useSelector(getSecondLang);

const [family, setFamily] = useState({});
const [family, setFamily] = useState<Family>();
const [serverSideError, setServerSideError] = useState();
const [publishing, setPublishing] = useState(false);

Expand All @@ -41,42 +36,21 @@ const Family = () => {
.then(() => {
return OperationsApi.getFamilyById(id).then(setFamily);
})
.catch((error) => setServerSideError(error))
.catch((error: any) => setServerSideError(error))
.finally(() => setPublishing(false));
}, [family, id]);

if (!family.id) return <Loading />;
if (publishing) return <Loading text={'publishing'} />;
if (!family) return <Loading />;
if (publishing) return <Loading text="publishing" />;

/*
* The publication button should be enabled only if RICH_TEXT value do not
* have unsupported styles like STRIKETHROUGH, color or background color
*/
const publicationDisabled = containUnsupportedStyles(family);
return (
<div className="container">
<PageTitleBlock
titleLg1={family.prefLabelLg1}
titleLg2={family.prefLabelLg2}
secondLang={secondLang}
/>
<ActionToolbar>
<ReturnButton action={() => goBack('/operations/families')} />

<Auth roles={[ADMIN]}>
<ValidationButton
object={family}
callback={publish}
disabled={publicationDisabled}
/>
</Auth>
<Auth roles={[ADMIN]}>
<Button
action={`/operations/family/${family.id}/modify`}
label={D.btnUpdate}
/>
</Auth>
</ActionToolbar>
<Menu family={family} publish={publish} />
{serverSideError && <ErrorBloc error={serverSideError} D={D} />}
`
<CheckSecondLang />
Expand All @@ -88,4 +62,4 @@ const Family = () => {
</div>
);
};
export default Family;
export default FamilyView;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { render, screen } from '@testing-library/react';
import { Menu } from './menu';
import { RBACMock } from '../../../tests-utils/rbac';
import { ADMIN } from '../../../auth/roles';
import { Family } from '../../../model/operations/family';

describe('Family Home Page Menu', () => {
it('an admin can update and publish a family', () => {
render(
<RBACMock roles={[ADMIN]}>
<Menu family={{} as Family} publish={jest.fn()} />
</RBACMock>
);

screen.getByText('Update');
screen.getByText('Publish');
screen.getByText('Back');
});

it('a user without Admin cannot create or publish a family', () => {
render(
<RBACMock roles={[]}>
<Menu family={{} as Family} publish={jest.fn()} />
</RBACMock>
);

expect(screen.queryByText('Update')).toBeNull();
expect(screen.queryByText('Publish')).toBeNull();
screen.getByText('Back');
});
});
43 changes: 43 additions & 0 deletions src/packages/modules-operations/families/visualization/menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Family } from '../../../model/operations/family';
import { ActionToolbar, Button, ReturnButton } from '@inseefr/wilco';
import Auth from '../../../auth/components/auth';
import { ADMIN } from '../../../auth/roles';
import { ValidationButton } from '../../../components';
import D from '../../../deprecated-locales/build-dictionary';
import { containUnsupportedStyles } from '../../../utils/html-utils';
import { useGoBack } from '../../../utils/hooks/useGoBack';

type MenuTypes = {
family: Family;
publish: () => void;
};

export const Menu = ({ family, publish }: Readonly<MenuTypes>) => {
const goBack = useGoBack();

/*
* The publication button should be enabled only if RICH_TEXT value do not
* have unsupported styles like STRIKETHROUGH, color or background color
*/
const publicationDisabled = containUnsupportedStyles(family);

return (
<ActionToolbar>
<ReturnButton action={() => goBack('/operations/families')} />

<Auth roles={[ADMIN]}>
<ValidationButton
object={family}
callback={publish}
disabled={publicationDisabled}
/>
</Auth>
<Auth roles={[ADMIN]}>
<Button
action={`/operations/family/${family.id}/modify`}
label={D.btnUpdate}
/>
</Auth>
</ActionToolbar>
);
};