Skip to content

Commit

Permalink
prefill modified currency if validation fail in new ReferenceForm #58
Browse files Browse the repository at this point in the history
  • Loading branch information
mydu committed Jun 26, 2019
1 parent 97e8c69 commit 2dd3e03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions importApp/src/components/FieldInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class FieldInput extends React.Component {


renderField() {
const {fieldValue, foreignKeys, referenceTables, showNewReference, newReference, isNonchangable, suggestedOptions} = this.props;
const {fieldValue, foreignKeys, referenceTables, showNewReference, newReference, isNonchangable, isValidationField, suggestedOptions} = this.props;
const {fieldSchema, fieldValid, value, suggestions} = this.state;

// Autosuggest will pass through all these props to the input.
Expand Down Expand Up @@ -205,7 +205,7 @@ class FieldInput extends React.Component {
<div>{fieldValue}</div>
}
{
!this.state.value &&
(!this.state.value || isValidationField) &&
<Button isColor='info' onClick={this.handleClickCreate}>Create new item</Button>
}
</div>
Expand Down
20 changes: 16 additions & 4 deletions importApp/src/components/ReferenceResourceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ReferenceResourceForm extends React.Component {
}

getStateFromProps = () => {
const {resourceDescriptor, originalValues} = this.props;
const {resourceDescriptor, originalValues, prefixedValues} = this.props;
const {schema} = resourceDescriptor;
const newResource = schema.fields.reduce((res, field) => {
let value = '';
Expand All @@ -38,6 +38,10 @@ class ReferenceResourceForm extends React.Component {
value = originalValues.find((item) => item.referenceField=== field.name).value;
valid = true;
}
if (prefixedValues && prefixedValues[field.name]) {
value = prefixedValues[field.name];
valid = true
}
return {
...res,
[field.name]: {
Expand Down Expand Up @@ -111,6 +115,7 @@ class ReferenceResourceForm extends React.Component {
newReference: null,
referenceMap: null,
isRICentityGroup: false,
prefixedValues: null,
newResource: {
...this.state.newResource,
[field]: {
Expand Down Expand Up @@ -182,7 +187,7 @@ class ReferenceResourceForm extends React.Component {
}

render() {
const {descriptor, resourceDescriptor, referenceTables, originalValues} = this.props;
const {descriptor, resourceDescriptor, referenceTables, originalValues, prefixedValues} = this.props;
const {schema} = resourceDescriptor;
const fieldsInvalid = values(this.state.newResource).filter((field) => field.fieldValid && !field.fieldValid.valid);

Expand Down Expand Up @@ -211,12 +216,16 @@ class ReferenceResourceForm extends React.Component {
exchange_rates: referenceTables['exchange_rates']
};
let table;
const prefixedValues = {
"modified_currency": this.state.newResource['modified_currency'].value
}
try {
table = await Table.load(source, {schema});
const rows = await table.read({forceCast: true, relations});
const errors = rows.filter((row) => row.errors);
if (errors.length) {
this.setState({
prefixedValues,
resourceValid: {
valid: false,
message: errors[0].errors[0].errors[0].message
Expand All @@ -230,6 +239,7 @@ class ReferenceResourceForm extends React.Component {
}
} catch (error) {
this.setState({
prefixedValues,
resourceValid: {
valid: false,
message: error.message || 'validation fail'
Expand All @@ -255,12 +265,13 @@ class ReferenceResourceForm extends React.Component {
return (
<FieldInput
key={index}
isNonchangable={NON_CHANGABLE_FIELDS.indexOf(field.name) !==-1}
isNonchangable={NON_CHANGABLE_FIELDS.indexOf(field.name) !==-1 || (prefixedValues && prefixedValues[field.name])}
isValidationField={this.state.prefixedValues && this.state.prefixedValues[field.name]}
fieldDescriptor={field}
foreignKeys={schema.foreignKeys}
referenceTables={referenceTables}
suggestedOptions={suggestedOptions}
fieldValue={(this.state.newResource[field.name] && this.state.newResource[field.name].value) || ''}
fieldValue={(this.state.newResource[field.name] && this.state.newResource[field.name].value) || (prefixedValues && prefixedValues[field.name])}
showNewReference={this.state.showNewReference}
newReference={this.state.newReference}
onClickCreate={this.handleCreateNewReference}
Expand Down Expand Up @@ -290,6 +301,7 @@ class ReferenceResourceForm extends React.Component {
originalValues={originalValues.filter((item)=> item.field === 'year')}
resourceDescriptor={getReferenceDescriptor()}
referenceTables={referenceTables}
prefixedValues={this.state.prefixedValues}
onSelectGroup={this.handleSelectGroup}
onCancel={this.handleHideNew}
onAddNew={this.handleAddNewReference} />
Expand Down

0 comments on commit 2dd3e03

Please sign in to comment.