Skip to content

Commit

Permalink
updating entity form to use formik WIP #556
Browse files Browse the repository at this point in the history
  • Loading branch information
vbojilova committed Aug 8, 2024
1 parent 41356ab commit 4774734
Show file tree
Hide file tree
Showing 41 changed files with 554 additions and 924 deletions.
2 changes: 1 addition & 1 deletion app/components/fields/StatusField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StatusField extends React.PureComponent { // eslint-disable-line react/pre

StatusField.propTypes = {
field: PropTypes.object.isRequired,
intl: PropTypes.object.isRequired.isRequired,
intl: PropTypes.object.isRequired,
};

export default injectIntl(StatusField);
6 changes: 4 additions & 2 deletions app/components/formik/ControlCheckbox.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from 'react';
import styled from 'styled-components';

const ControlCheckbox = styled.input`
const StyledCheckbox = styled.input`
margin-right: 5px;
`;

export default <ControlCheckbox type='checkbox' />;
const ControlCheckbox = (props) => <StyledCheckbox type='checkbox' {...props} />;

export default ControlCheckbox;
2 changes: 2 additions & 0 deletions app/components/formik/DateControl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ const DateControl = (props) => {
parse(inputValue, DATE_FORMAT, new Date()),
DB_DATE_FORMAT,
);
setFieldValue(field.name, formattedDB);
onChange(formattedDB);
} else {
// wrong format but store value for input field
setFieldValue(field.name, inputValue);
onChange(inputValue);
}
};
Expand Down
4 changes: 2 additions & 2 deletions app/components/formik/MultiSelectField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class MultiSelectField extends React.Component { // eslint-disable-line react/pr
onClick: this.onCloseMultiselect,
},
]}
onChange={this.props.onChange}
onChange={this.props.handleUpdate}
{...controlProps}
/>
</MultiSelectWrapper>
Expand All @@ -319,7 +319,7 @@ class MultiSelectField extends React.Component { // eslint-disable-line react/pr
MultiSelectField.propTypes = {
field: PropTypes.object,
fieldData: PropTypes.object,
onChange: PropTypes.func,
handleUpdate: PropTypes.func,
closeOnClickOutside: PropTypes.bool,
scrollContainer: PropTypes.object,
intl: PropTypes.object.isRequired,
Expand Down
53 changes: 19 additions & 34 deletions app/containers/ActionEdit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import HelmetCanonical from 'components/HelmetCanonical';
import { FormattedMessage, injectIntl } from 'react-intl';
import { actions as formActions } from 'react-redux-form/immutable';
import { Map, fromJS } from 'immutable';

import {
Expand All @@ -26,7 +25,7 @@ import {
getTextareaField,
renderTaxonomyControl,
getReferenceFormField,
} from 'utils/forms';
} from 'utils/formik';

import {
getMetaField,
Expand All @@ -43,7 +42,6 @@ import {
loadEntitiesIfNeeded,
redirectIfNotPermitted,
updatePath,
updateEntityForm,
deleteEntity,
openNewEntityModal,
submitInvalid,
Expand Down Expand Up @@ -84,25 +82,20 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
constructor(props) {
super(props);
this.scrollContainer = React.createRef();
this.remoteSubmitForm = null;
}

UNSAFE_componentWillMount() {
this.props.loadEntitiesIfNeeded();
if (this.props.dataReady && this.props.viewEntity) {
this.props.initialiseForm('measureEdit.form.data', this.getInitialFormData());
}

}

UNSAFE_componentWillReceiveProps(nextProps) {
// reload entities if invalidated
if (!nextProps.dataReady) {
this.props.loadEntitiesIfNeeded();
}
// repopulate if new data becomes ready
if (nextProps.dataReady && !this.props.dataReady && nextProps.viewEntity) {
this.props.initialiseForm('measureEdit.form.data', this.getInitialFormData(nextProps));
}
//

if (nextProps.authReady && !this.props.authReady) {
this.props.redirectIfNotPermitted();
}
Expand All @@ -111,15 +104,16 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
}
}

getInitialFormData = (nextProps) => {
const props = nextProps || this.props;
const {
viewEntity, taxonomies, recommendationsByFw, indicators,
} = props;
bindHandleSubmit = (submitForm) => {
this.remoteSubmitForm = submitForm;
};

getInitialFormData = ({ viewEntity, taxonomies, recommendationsByFw, indicators }) => {
let attributes = viewEntity.get('attributes');
if (!attributes.get('reference') || attributes.get('reference') === '') {
attributes = attributes.set('reference', viewEntity.get('id'));
}

return viewEntity
? Map({
id: viewEntity.get('id'),
Expand Down Expand Up @@ -290,7 +284,11 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
{
type: 'save',
disabled: saveSending,
onClick: () => this.props.handleSubmitRemote('measureEdit.form.data'),
onClick: (e) => {
if (this.remoteSubmitForm) {
this.remoteSubmitForm(e);
}
},
}]
: null
}
Expand Down Expand Up @@ -329,8 +327,8 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
{viewEntity && dataReady && !deleteSending
&& (
<EntityForm
model="measureEdit.form.data"
formData={viewDomain.getIn(['form', 'data'])}
formData={this.getInitialFormData(this.props).toJS()}
bindHandleSubmit={this.bindHandleSubmit}
saving={saveSending}
handleSubmit={(formData) => this.props.handleSubmit(
formData,
Expand All @@ -341,7 +339,6 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
)}
handleSubmitFail={this.props.handleSubmitFail}
handleCancel={this.props.handleCancel}
handleUpdate={this.props.handleUpdate}
handleDelete={canUserDeleteEntities(this.props.highestRole) ? this.props.handleDelete : null}
fields={{
header: {
Expand Down Expand Up @@ -382,12 +379,9 @@ export class ActionEdit extends React.Component { // eslint-disable-line react/p
ActionEdit.propTypes = {
loadEntitiesIfNeeded: PropTypes.func,
redirectIfNotPermitted: PropTypes.func,
initialiseForm: PropTypes.func,
handleSubmitRemote: PropTypes.func.isRequired,
handleSubmitFail: PropTypes.func.isRequired,
handleSubmit: PropTypes.func.isRequired,
handleCancel: PropTypes.func.isRequired,
handleUpdate: PropTypes.func.isRequired,
handleDelete: PropTypes.func.isRequired,
viewDomain: PropTypes.object,
viewEntity: PropTypes.object,
Expand Down Expand Up @@ -431,10 +425,6 @@ function mapDispatchToProps(dispatch, props) {
redirectIfNotPermitted: () => {
dispatch(redirectIfNotPermitted(USER_ROLES.MANAGER.value));
},
initialiseForm: (model, formData) => {
dispatch(formActions.reset(model));
dispatch(formActions.change(model, formData, { silent: true }));
},
onErrorDismiss: () => {
dispatch(submitInvalid(true));
},
Expand All @@ -444,10 +434,8 @@ function mapDispatchToProps(dispatch, props) {
handleSubmitFail: () => {
dispatch(submitInvalid(false));
},
handleSubmitRemote: (model) => {
dispatch(formActions.submit(model));
},
handleSubmit: (formData, taxonomies, recommendationsByFw, indicators, viewEntity) => {
handleSubmit: (formValues, taxonomies, recommendationsByFw, indicators, viewEntity) => {
const formData = fromJS(formValues);
let saveData = formData
.set(
'measureCategories',
Expand Down Expand Up @@ -506,9 +494,6 @@ function mapDispatchToProps(dispatch, props) {
handleCancel: () => {
dispatch(updatePath(`/actions/${props.params.id}`, { replace: true }));
},
handleUpdate: (formData) => {
dispatch(updateEntityForm(formData));
},
handleDelete: () => {
dispatch(deleteEntity({
path: 'measures',
Expand Down
15 changes: 0 additions & 15 deletions app/containers/ActionEdit/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,9 @@
*
*/
import { combineReducers } from 'redux-immutable';
import { combineForms } from 'react-redux-form/immutable';

import { entityFormReducer } from 'containers/App/entityFormReducer';
import { UPDATE_ENTITY_FORM } from 'containers/App/constants';
import { FORM_INITIAL } from './constants';

function formReducer(state = FORM_INITIAL, action) {
switch (action.type) {
case UPDATE_ENTITY_FORM:
return action.data;
default:
return state;
}
}

export default combineReducers({
page: entityFormReducer,
form: combineForms({
data: formReducer,
}, 'measureEdit.form'),
});
Loading

0 comments on commit 4774734

Please sign in to comment.