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

[RFR] Fix create to edit form values #2339

Merged
merged 7 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 28 additions & 1 deletion cypress/integration/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Create Page', () => {
);
});

it('should redirect to show page after create success', () => {
it('should redirect to edit page after create success', () => {
const values = [
{
type: 'input',
Expand All @@ -79,6 +79,33 @@ describe('Create Page', () => {

CreatePage.setValues(values);
CreatePage.submit();
EditPage.waitUntilVisible();
cy.get(EditPage.elements.input('title')).should(el =>
expect(el).to.have.value('Test title')
);
cy.get(EditPage.elements.input('teaser')).should(el =>
expect(el).to.have.value('Test teaser')
);

EditPage.delete();
});

it('should redirect to show page after create success with "Save and show"', () => {
const values = [
{
type: 'input',
name: 'title',
value: 'Test title',
},
{
type: 'textarea',
name: 'teaser',
value: 'Test teaser',
},
];

CreatePage.setValues(values);
CreatePage.submitAndShow();
ShowPage.waitUntilVisible();
EditPage.navigate();
EditPage.delete();
Expand Down
11 changes: 10 additions & 1 deletion cypress/support/CreatePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export default url => ({
inputs: `.ra-input`,
snackbar: 'div[role="alertdialog"]',
submitButton: ".create-page button[type='submit']",
submitAndAddButton:
submitAndShowButton:
".create-page form>div:last-child button[type='button']:nth-child(2)",
submitAndAddButton:
".create-page form>div:last-child button[type='button']:nth-child(3)",
submitCommentable:
".create-page form>div:last-child button[type='button']:last-child",
descInput: '.ql-editor',
Expand Down Expand Up @@ -55,6 +57,13 @@ export default url => ({
cy.wait(200); // let the notification disappear (could block further submits)
},

submitAndShow() {
cy.get(this.elements.submitAndShowButton).click();
cy.get(this.elements.snackbar);
cy.get(this.elements.body).click(); // dismiss notification
cy.wait(200); // let the notification disappear (could block further submits)
},

submitAndAdd() {
cy.get(this.elements.submitAndAddButton).click();
cy.get(this.elements.snackbar);
Expand Down
1 change: 1 addition & 0 deletions examples/simple/src/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const messages = {
title: 'Post "%{title}"',
},
action: {
save_and_edit: 'Save and Edit',
save_and_add: 'Save and Add',
save_and_show: 'Save and Show',
save_with_average_note: 'Save with Note',
Expand Down
8 changes: 7 additions & 1 deletion examples/simple/src/posts/PostCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ const SaveWithNoteButton = connect(

const PostCreateToolbar = props => (
<Toolbar {...props}>
<SaveButton
label="post.action.save_and_edit"
redirect="edit"
submitOnEnter={true}
/>
<SaveButton
label="post.action.save_and_show"
redirect="show"
submitOnEnter={true}
submitOnEnter={false}
variant="flat"
/>
<SaveButton
label="post.action.save_and_add"
Expand Down
7 changes: 7 additions & 0 deletions packages/ra-core/src/actions/formActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const INITIALIZE_FORM = 'RA/INITIALIZE_FORM';
export const RESET_FORM = 'RA/RESET_FORM';
export const BEFORE_LOCATION_CHANGE = 'RA/BEFORE_LOCATION_CHANGE';

export const initializeForm = initialValues => ({
type: INITIALIZE_FORM,
Expand All @@ -9,3 +10,9 @@ export const initializeForm = initialValues => ({
export const resetForm = () => ({
type: RESET_FORM,
});

export const beforeLocationChange = ({ payload, meta }) => ({
type: BEFORE_LOCATION_CHANGE,
payload,
meta,
});
7 changes: 6 additions & 1 deletion packages/ra-core/src/createAdminStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { USER_LOGOUT } from './actions/authActions';
import createAppReducer from './reducer';
import { adminSaga } from './sideEffect';
import { defaultI18nProvider } from './i18n';
import formMiddleware from './form/formMiddleware';

export default ({
authProvider,
Expand Down Expand Up @@ -36,7 +37,11 @@ export default ({
resettableAppReducer,
initialState,
compose(
applyMiddleware(sagaMiddleware, routerMiddleware(history)),
applyMiddleware(
sagaMiddleware,
formMiddleware,
routerMiddleware(history)
),
typeof window !== 'undefined' && window.devToolsExtension
? window.devToolsExtension()
: f => f
Expand Down
43 changes: 43 additions & 0 deletions packages/ra-core/src/form/formMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { LOCATION_CHANGE } from 'react-router-redux';
import { destroy } from 'redux-form';
import isEqual from 'lodash/isEqual';

import { resetForm } from '../actions/formActions';
import { REDUX_FORM_NAME } from '../form/constants';

/**
* This middleware ensure that whenever a location change happen, we get the
* chance to properly reset the redux-form record form, preventing data to be
* kept between different resources or form types (CREATE, EDIT).
*
* A middleware is needed instead of a saga because we need to control the actions
* order: we need to ensure we reset the redux form BEFORE the location actually
* change. Otherwise, the new page which may contains a record redux-form might
Copy link
Member

Choose a reason for hiding this comment

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

s/change/changes/
s/may contains/may contain/

* initialize before our reset and loose its data.
*/
const formMiddleware = () => {
let previousLocation;
return next => action => {
if (
action.type !== LOCATION_CHANGE ||
(action.payload.state && action.payload.state.skipFormReset)
) {
return next(action);
}

// history allows one to redirect to the same location which can happen
// when using a special menu for a create page for instance. In this case,
// we don't want to reset the form.
// See https://github.com/marmelab/react-admin/issues/2291
if (isEqual(action.payload, previousLocation)) {
return next(action);
}

previousLocation = action.payload;
next(resetForm());
next(destroy(REDUX_FORM_NAME));
return next(action);
};
};

export default formMiddleware;
62 changes: 62 additions & 0 deletions packages/ra-core/src/form/formMiddleware.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { LOCATION_CHANGE } from 'react-router-redux';
import { destroy } from 'redux-form';

import formMiddleware from './formMiddleware';
import { REDUX_FORM_NAME } from '../form/constants';
import { resetForm } from '../actions/formActions';

describe('form middleware', () => {
it('does not prevent actions other than LOCATION_CHANGE to be handled', () => {
const next = jest.fn();
const action = { type: '@@redux-form/INITIALIZE' };

formMiddleware()(next)(action);

expect(next).toHaveBeenCalledWith(action);
expect(next).toHaveBeenCalledTimes(1);
});

it('does not prevent LOCATION_CHANGE actions to be handled if their state contains a skipFormReset set to true', () => {
const next = jest.fn();
const action = {
type: LOCATION_CHANGE,
payload: { state: { skipFormReset: true } },
};
formMiddleware()(next)(action);

expect(next).toHaveBeenCalledWith(action);
});

it('resets the record state and destroy the redux form before letting the location change to be handled', () => {
const next = jest.fn();
const action = {
type: LOCATION_CHANGE,
payload: {
pathname: '/posts/create',
},
};
formMiddleware()(next)(action);

expect(next).toHaveBeenCalledWith(resetForm());
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME));
expect(next).toHaveBeenCalledWith(action);
expect(next).toHaveBeenCalledTimes(3);
});

it('does not resets the record and form if LOCATION_CHANGE targets the same location', () => {
const next = jest.fn();
const action = {
type: LOCATION_CHANGE,
payload: {
pathname: '/posts/create',
},
};
const middleware = formMiddleware()(next);
middleware(action);
middleware(action);
expect(next).toHaveBeenCalledWith(resetForm());
expect(next).toHaveBeenCalledWith(destroy(REDUX_FORM_NAME));
expect(next).toHaveBeenCalledWith(action);
expect(next).toHaveBeenCalledTimes(4);
});
});
2 changes: 0 additions & 2 deletions packages/ra-core/src/sideEffect/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import redirection from './redirection';
import accumulate from './accumulate';
import refresh from './refresh';
import undo from './undo';
import recordForm from './recordForm';

/**
* @param {Object} dataProvider A Data Provider function
Expand All @@ -27,6 +26,5 @@ export default (dataProvider, authProvider, i18nProvider) =>
refresh(),
notification(),
callback(),
recordForm(),
]);
};
1 change: 0 additions & 1 deletion packages/ra-core/src/sideEffect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ export accumulateSaga from './accumulate';
export refreshSaga from './refresh';
export i18nSaga from './i18n';
export undoSaga from './undo';
export recordForm from './recordForm';
28 changes: 0 additions & 28 deletions packages/ra-core/src/sideEffect/recordForm.js

This file was deleted.

51 changes: 0 additions & 51 deletions packages/ra-core/src/sideEffect/recordForm.spec.js

This file was deleted.