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(record): confirm modal on create record #431

Merged
merged 2 commits into from
Mar 25, 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 libs/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leav/ui",
"version": "0.2.8",
"version": "0.3.0",
"description": "Shared React components and hooks",
"scripts": {
"prepublishOnly": "yarn build",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {mockRecord} from '_ui/__mocks__/common/record';
import {IUseCanEditRecordHook} from '../../../hooks/useCanEditRecord/useCanEditRecord';
import {render, screen} from '../../../_tests/testUtils';
import {EditRecord} from './EditRecord';
import {Form} from 'antd';

jest.mock('../EditRecordContent', () => {
return function EditRecordContent() {
Expand All @@ -20,6 +21,12 @@ jest.mock('hooks/useCanEditRecord/useCanEditRecord', () => ({
useCanEditRecord: (): IUseCanEditRecordHook => ({loading: false, canEdit: true, isReadOnly: false})
}));

const EditRecordWithForm = props => {
const [form] = Form.useForm();

return <EditRecord form={form} {...props} />;
};

describe('EditRecord', () => {
const commonMocks = [
{
Expand Down Expand Up @@ -52,22 +59,17 @@ describe('EditRecord', () => {
});

test('Display form', async () => {
const _handleClose = jest.fn();

const CompWithButtons = () => {
const closeButtonRef = useRef<HTMLButtonElement>(null);

return (
<>
<button ref={closeButtonRef}>Close</button>;
<EditRecord
<EditRecordWithForm
withInfoButton={false}
library={mockRecord.library.id}
record={mockRecord}
onClose={_handleClose}
buttonsRefs={{
close: closeButtonRef
}}
buttonsRefs={{}}
/>
</>
);
Expand All @@ -79,35 +81,4 @@ describe('EditRecord', () => {

expect(screen.getByText('EditRecordContent')).toBeVisible();
});

test('Close form', async () => {
const _handleClose = jest.fn();

const CompWithButtons = () => {
const closeButtonRef = useRef<HTMLButtonElement>(null);

return (
<>
<button ref={closeButtonRef}>Close</button>;
<EditRecord
withInfoButton={false}
library={mockRecord.library.id}
record={mockRecord}
onClose={_handleClose}
buttonsRefs={{
close: closeButtonRef
}}
/>
</>
);
};

render(<CompWithButtons />, {
mocks: commonMocks
});

await user.click(screen.getByRole('button', {name: /Close/}));

expect(_handleClose).toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ import editRecordReducer, {EditRecordReducerActionsTypes, initialState} from '..
import {EditRecordReducerContext} from '../editRecordReducer/editRecordReducerContext';
import EditRecordSidebar from '../EditRecordSidebar';
import CreationErrorContext, {ICreationErrorByField} from './creationErrorContext';
import {FormInstance} from 'antd/lib/form/Form';

interface IEditRecordProps {
antdForm: FormInstance;
record: RecordIdentityFragment['whoAmI'] | null;
library: string;
onClose: () => void;
onCreate?: (newRecord: RecordIdentityFragment['whoAmI']) => void;
valuesVersion?: IValueVersion;
showSidebar?: boolean;
Expand All @@ -57,7 +58,6 @@ interface IEditRecordProps {
// Here we're not in charge of buttons position. It might on a modal footer or pretty much anywhere.
// We're using refs to still be able to handle the click on the buttons
buttonsRefs: {
close?: React.RefObject<HTMLButtonElement>;
refresh?: React.RefObject<HTMLButtonElement>;
valuesVersions?: React.RefObject<HTMLButtonElement>;
};
Expand Down Expand Up @@ -94,9 +94,9 @@ const Sidebar = styled.div`
`;

export const EditRecord: FunctionComponent<IEditRecordProps> = ({
antdForm,
record,
library,
onClose,
onCreate,
valuesVersion,
showSidebar = false,
Expand Down Expand Up @@ -424,7 +424,6 @@ export const EditRecord: FunctionComponent<IEditRecordProps> = ({
};

const listenersByButtonsName: Record<keyof IEditRecordProps['buttonsRefs'], () => void> = {
close: onClose,
refresh: () => {
dispatch({
type: EditRecordReducerActionsTypes.REQUEST_REFRESH
Expand All @@ -448,6 +447,7 @@ export const EditRecord: FunctionComponent<IEditRecordProps> = ({
<EditRecordSkeleton rows={5} />
) : canEdit ? (
<EditRecordContent
antdForm={antdForm}
record={record}
library={library}
onRecordSubmit={_handleRecordSubmit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import {mockRecordForm} from '_ui/__mocks__/common/form';
import {mockRecord} from '_ui/__mocks__/common/record';
import {render, screen} from '../../../_tests/testUtils';
import EditRecordContent from './EditRecordContent';
import {Form} from 'antd';

jest.mock('./uiElements/StandardField', () => {
return function StandardField() {
return <div>StandardField</div>;
};
});

const EditRecordContentWithForm = props => {
const [form] = Form.useForm();

return <EditRecordContent form={form} {...props} />;
};

describe('EditRecordContent', () => {
const mocks = [
{
Expand Down Expand Up @@ -86,7 +93,7 @@ describe('EditRecordContent', () => {
}));

render(
<EditRecordContent
<EditRecordContentWithForm
record={mockRecord}
library={mockRecord.library.id}
onRecordSubmit={jest.fn()}
Expand Down Expand Up @@ -119,7 +126,7 @@ describe('EditRecordContent', () => {
}));

render(
<EditRecordContent
<EditRecordContentWithForm
record={mockRecord}
library={mockRecord.library.id}
onRecordSubmit={jest.fn()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import extractFormElements from './helpers/extractFormElements';
import {RecordEditionContext} from './hooks/useRecordEditionContext';
import {formComponents} from './uiElements';
import {DeleteMultipleValuesFunc, DeleteValueFunc, FormElement, SubmitValueFunc} from './_types';
import {Form} from 'antd';
import {useForm} from 'antd/lib/form/Form';
import {Form, FormInstance} from 'antd';
import {Store} from 'antd/lib/form/interface';
import dayjs from 'dayjs';
import {EDIT_OR_CREATE_RECORD_FORM_ID} from './formConstants';

interface IEditRecordContentProps {
antdForm: FormInstance;
TdyP marked this conversation as resolved.
Show resolved Hide resolved
record: IRecordIdentityWhoAmI | null;
library: string;
onRecordSubmit: () => void;
Expand All @@ -34,6 +34,7 @@ interface IEditRecordContentProps {
}

function EditRecordContent({
antdForm,
record,
library,
onRecordSubmit,
Expand All @@ -45,10 +46,11 @@ function EditRecordContent({
const formId = record ? 'edition' : 'creation';
const {t} = useSharedTranslation();
const {state, dispatch} = useEditRecordReducer();
const [antForm] = useForm();

useRecordsConsultationHistory(record?.library?.id ?? null, record?.id ?? null);

const forminstance = Form.useFormInstance();

const {data: recordUpdateData} = useGetRecordUpdatesSubscription(
{records: [record?.id], ignoreOwnEvents: true},
!record?.id
Expand Down Expand Up @@ -171,7 +173,7 @@ function EditRecordContent({
return (
<Form
id={EDIT_OR_CREATE_RECORD_FORM_ID}
form={antForm}
form={antdForm}
initialValues={antdFormInitialValues}
onFinish={onRecordSubmit}
>
Expand Down
Loading
Loading