Make 'form' state key singular rather than plural

This commit is contained in:
David Craig 2016-04-26 12:01:40 +12:00 committed by Ingo Schommer
parent 25f1f992a9
commit 0b9a339b79
12 changed files with 40 additions and 39 deletions

View File

@ -6,7 +6,7 @@ import reducerRegister from 'lib/ReducerRegister';
import * as configActions from 'state/config/ConfigActions';
import ConfigReducer from 'state/config/ConfigReducer';
import FormsReducer from 'state/forms/FormsReducer';
import FormReducer from 'state/form/FormReducer';
import SchemaReducer from 'state/schema/SchemaReducer';
import RecordsReducer from 'state/records/RecordsReducer';
import CampaignReducer from 'state/campaign/CampaignReducer';
@ -17,7 +17,7 @@ import CampaignAdmin from 'containers/CampaignAdmin/index';
function appBoot() {
reducerRegister.add('config', ConfigReducer);
reducerRegister.add('forms', FormsReducer);
reducerRegister.add('form', FormReducer);
reducerRegister.add('schemas', SchemaReducer);
reducerRegister.add('records', RecordsReducer);
reducerRegister.add('campaign', CampaignReducer);

View File

@ -1,7 +1,7 @@
import React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as formsActions from 'state/forms/FormsActions';
import * as formActions from 'state/form/FormActions';
import * as schemaActions from 'state/schema/SchemaActions';
import SilverStripeComponent from 'lib/SilverStripeComponent';
import FormComponent from 'components/Form/Form';
@ -161,7 +161,7 @@ export class FormBuilderComponent extends SilverStripeComponent {
}
if (typeof formState.id !== 'undefined') {
this.props.formsActions.addForm(formState);
this.props.formActions.addForm(formState);
}
});
@ -209,9 +209,9 @@ export class FormBuilderComponent extends SilverStripeComponent {
*/
handleFieldUpdate(event, updates, fn) {
if (typeof fn !== 'undefined') {
fn(this.getFormId(), this.props.formsActions.updateField);
fn(this.getFormId(), this.props.formActions.updateField);
} else {
this.props.formsActions.updateField(this.getFormId(), updates);
this.props.formActions.updateField(this.getFormId(), updates);
}
}
@ -248,13 +248,13 @@ export class FormBuilderComponent extends SilverStripeComponent {
*/
handleSubmit(event) {
const schemaFields = this.props.schemas[this.props.schemaUrl].schema.fields;
const fieldValues = this.props.forms[this.getFormId()].fields
const fieldValues = this.props.form[this.getFormId()].fields
.reduce((prev, curr) => Object.assign({}, prev, {
[schemaFields.find(schemaField => schemaField.id === curr.id).name]: curr.value,
}), {});
const submitFn = () => {
this.props.formsActions.submitForm(
this.props.formActions.submitForm(
this.submitApi,
this.getFormId(),
fieldValues
@ -372,7 +372,7 @@ export class FormBuilderComponent extends SilverStripeComponent {
* @param {string} formId - ID of the form to clean up.
*/
removeForm(formId) {
this.props.formsActions.removeForm(formId);
this.props.formActions.removeForm(formId);
}
render() {
@ -381,7 +381,7 @@ export class FormBuilderComponent extends SilverStripeComponent {
return null;
}
const formSchema = this.getFormSchema();
const formState = this.props.forms[formId];
const formState = this.props.form[formId];
// If the response from fetching the initial data
// hasn't come back yet, don't render anything.
@ -423,8 +423,8 @@ export class FormBuilderComponent extends SilverStripeComponent {
FormBuilderComponent.propTypes = {
config: React.PropTypes.object,
createFn: React.PropTypes.func,
forms: React.PropTypes.object.isRequired,
formsActions: React.PropTypes.object.isRequired,
form: React.PropTypes.object.isRequired,
formActions: React.PropTypes.object.isRequired,
handleSubmit: React.PropTypes.func,
schemas: React.PropTypes.object.isRequired,
schemaActions: React.PropTypes.object.isRequired,
@ -434,14 +434,14 @@ FormBuilderComponent.propTypes = {
function mapStateToProps(state) {
return {
config: state.config,
forms: state.forms,
form: state.form,
schemas: state.schemas,
};
}
function mapDispatchToProps(dispatch) {
return {
formsActions: bindActionCreators(formsActions, dispatch),
formActions: bindActionCreators(formActions, dispatch),
schemaActions: bindActionCreators(schemaActions, dispatch),
};
}

View File

@ -12,8 +12,8 @@ describe('FormBuilderComponent', () => {
beforeEach(() => {
const props = {
forms: {},
formsActions: {},
form: {},
formActions: {},
schemas: {},
schemaActions: {},
schemaUrl: 'admin/assets/schema/1',

View File

@ -84,7 +84,7 @@ class CampaignAdmin extends SilverStripeComponent {
* @return object
*/
renderIndexView() {
const schemaUrl = this.props.sectionConfig.forms.EditForm.schemaUrl;
const schemaUrl = this.props.sectionConfig.form.EditForm.schemaUrl;
const formActionProps = {
label: i18n._t('Campaigns.ADDCAMPAIGN'),
icon: 'plus',
@ -138,7 +138,7 @@ class CampaignAdmin extends SilverStripeComponent {
* Renders the Detail Edit Form for a Campaign.
*/
renderDetailEditView() {
const baseSchemaUrl = this.props.sectionConfig.forms.DetailEditForm.schemaUrl;
const baseSchemaUrl = this.props.sectionConfig.form.DetailEditForm.schemaUrl;
const schemaUrl = `${baseSchemaUrl}/ChangeSet/${this.props.campaignId}`;
return (
@ -159,7 +159,7 @@ class CampaignAdmin extends SilverStripeComponent {
* Render the view for creating a new Campaign.
*/
renderCreateView() {
const baseSchemaUrl = this.props.sectionConfig.forms.DetailEditForm.schemaUrl;
const baseSchemaUrl = this.props.sectionConfig.form.DetailEditForm.schemaUrl;
const formBuilderProps = {
createFn: this.campaignCreationView,
schemaUrl: `${baseSchemaUrl}/ChangeSet`,
@ -294,7 +294,7 @@ CampaignAdmin.propTypes = {
actions: React.PropTypes.object.isRequired,
campaignId: React.PropTypes.string,
config: React.PropTypes.shape({
forms: React.PropTypes.shape({
form: React.PropTypes.shape({
editForm: React.PropTypes.shape({
schemaUrl: React.PropTypes.string,
}),

View File

@ -1,4 +1,4 @@
import { ACTION_TYPES } from './FormsActionTypes';
import { ACTION_TYPES } from './FormActionTypes';
/**
* Removes a form from state.

View File

@ -1,9 +1,9 @@
import deepFreeze from 'deep-freeze';
import { ACTION_TYPES } from './FormsActionTypes';
import { ACTION_TYPES } from './FormActionTypes';
const initialState = deepFreeze({});
function formsReducer(state = initialState, action) {
function formReducer(state = initialState, action) {
switch (action.type) {
case ACTION_TYPES.REMOVE_FORM:
@ -47,4 +47,4 @@ function formsReducer(state = initialState, action) {
}
}
export default formsReducer;
export default formReducer;

View File

@ -1,11 +1,11 @@
# forms
# form
This state key holds form and form field data. Forms built using the `FormBuilder` component
have their state stored in child keys of `forms` (keyed by form ID) automatically.
have their state stored in child keys of `form` (keyed by form ID) automatically.
```js
{
forms: {
form: {
DetailEditForm: {
fields: [
{

View File

@ -1,13 +1,14 @@
/* global jest, describe, expect, it, beforeEach */
jest.unmock('deep-freeze');
jest.unmock('../FormsReducer');
jest.unmock('../FormsActionTypes');
jest.unmock('../FormReducer');
jest.unmock('../FormActionTypes');
import deepFreeze from 'deep-freeze';
import { ACTION_TYPES } from '../FormsActionTypes';
import formsReducer from '../FormsReducer';
describe('formsReducer', () => {
import { ACTION_TYPES } from '../FormActionTypes';
import formReducer from '../FormReducer';
describe('formReducer', () => {
describe('ADD_FORM', () => {
const initialState = deepFreeze({
DetailEditForm: {
@ -40,7 +41,7 @@ describe('formsReducer', () => {
},
};
const nextState = formsReducer(initialState, {
const nextState = formReducer(initialState, {
type: ACTION_TYPES.ADD_FORM,
payload,
});
@ -83,7 +84,7 @@ describe('formsReducer', () => {
});
it('should remove the form', () => {
const nextState = formsReducer(initialState, {
const nextState = formReducer(initialState, {
type: ACTION_TYPES.REMOVE_FORM,
payload: { formId: 'DetailEditForm' },
});
@ -109,7 +110,7 @@ describe('formsReducer', () => {
});
it('should update properties of a form field', () => {
const nextState = formsReducer(initialState, {
const nextState = formReducer(initialState, {
type: ACTION_TYPES.UPDATE_FIELD,
payload: {
formId: 'DetailEditForm',
@ -140,7 +141,7 @@ describe('formsReducer', () => {
});
it('should add top level form messages', () => {
const nextState = formsReducer(initialState, {
const nextState = formReducer(initialState, {
type: ACTION_TYPES.SUBMIT_FORM_SUCCESS,
payload: {
id: 'DetailEditForm',

View File

@ -5,4 +5,4 @@ Manages state associated with the FormFieldSchema.
When dependency injection is implemented, this will be moved into either Framework or CMS.
We can't move it sooner because there's no way of extending state.
Note form state is stored under the `forms` _not_ the `schema` key.
Note form state is stored under the `form` _not_ the `schema` key.

View File

@ -52,7 +52,7 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider {
public function getClientConfig() {
return array_merge(parent::getClientConfig(), [
'forms' => [
'form' => [
// TODO Use schemaUrl instead
'EditForm' => [
'schemaUrl' => $this->Link('schema/EditForm')