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

fix(localizedmoneyinput): remove 'amount/currencyCode' from event name #419

Merged
merged 7 commits into from
Jan 14, 2019

Conversation

gnerkus
Copy link
Contributor

@gnerkus gnerkus commented Jan 9, 2019

Summary

  • Re-define component values as object of {amount, currencyCode}

Description

This PR re-defines the values prop expected by the LocalizedMoneyInput as an object of {amount, currencyCode}. This PR changes the required values
from:

{EUR: '12.00', USD: '25.00'}

to:

{EUR: {
  amount: '12.00',
  currencyCode: 'EUR',
},
USD: {
  amount: '25.00',
  currencyCode: 'USD',
}}

This is required to keep the change event signature consistent with the event from the contained MoneyInput components.

Visual diffs

Before After
localized-money-input-onchange-before localized-money-input-onchange-v2-after

// "price.EUR.amount"
//
// eslint-disable-next-line no-param-reassign
event.target.name = event.target.name.replace(/(.*)(\..*)$/, '$1');
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably change the element name instead as we also need these event names on the blur event.

You should be able to change the implementation of LocalizedMoneyInput.getName.

Copy link
Contributor Author

@gnerkus gnerkus Jan 9, 2019

Choose a reason for hiding this comment

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

LocalizedMoneyInput.getName works correctly:

export const getName = (namePrefix, language) =>
  namePrefix ? `${namePrefix}.${language}` : undefined;

The .amount and .currencyCode suffices come from the MoneyInput.

To change the element name would require a change to the elements contained in the MoneyInput.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, got it now! To me this is an indication that our form values might still be a bit off (or maybe they are not and this is the best we can do).

Give this approach a thought for bit:

I assume our form value currently looks like this:

{
  EUR: "12.50",
  AUD: "34.60"
}

If we changed the form value to

{
  EUR: { currencyCode: "EUR", amount: "12.50" }
  AUD: { currencyCode: "AUD", amount: "12.50" }
}

then we would not need to change the name.

This would also allow our utility functions we write for MoneyInput to be usable with LocalizedMoneyInput since then both would be dealing with a shape of { currencyCode, amount }.

We'd need to adapt some of the static conversion functions. Maybe this is simpler than mangling the event names?

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer this too. It's simpler than changing the event names.

@gnerkus gnerkus force-pushed the io-fix-onchange-localizedmoneyinput branch from fd94e04 to 7d6bdeb Compare January 10, 2019 09:58
Copy link
Contributor

@dferber90 dferber90 left a comment

Choose a reason for hiding this comment

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

Looking very good, just some small things left 👍

Could you update the PR description to reflect the latest changes? Having up-to-date PR descriptions helps us write a better changelog.

@@ -22,7 +25,7 @@ import { LocalizedMoneyInput } from '@commercetools-frontend/ui-kit';
| ------------------------------- | ---------------- | :------: | ---------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `id` | `string` | - | - | - | Used as prefix of HTML `id` property. Each input field id will have the currency as a suffix (`${idPrefix}.${lang}`), e.g. `foo.en` |
| `name` | `string` | - | - | - | Used as HTML `name` property for each input field. Each input field name will have the currency as a suffix (`${namePrefix}.${lang}`), e.g. `foo.en` |
| `value` | `object` | ✅ | - | - | Values to use. Keyed by currency, the values are the actual values, e.g. `{ USD: '12.22', EUR: '41.44' }` |
| `value` | `object` | ✅ | - | - | Values to use. Keyed by currency, the values are the actual values, e.g. `{ USD: {currencyCode: 'USD', amount: '12.22'}, EUR: {currencyCode: 'EUR', amount: '41.44'} }` |
Copy link
Contributor

Choose a reason for hiding this comment

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

"The values are the money values" instead of "actual values"

@@ -153,16 +174,21 @@ Here are examples of `centPrecision` and `highPrecision` prices.

##### `LocalizedMoneyInput.parseMoneyValues`

The `parseMoneyValues` function will turn a [`MoneyValue`](https://docs.commercetools.com/http-api-types#money) into a value the LocalizedMoneyInput component can handle `({ currencyCode: amount })`.
The `parseMoneyValues` function will turn a [`MoneyValue`](https://docs.commercetools.com/http-api-types#money) into a value the LocalizedMoneyInput component can handle `({ [currencyCode]: {currencyCode: [currencyCode], amount: [amount]} })`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes it look like there are arrays. I'd write

{ [currencyCode]: { currencyCode , amount } }

Notice that I also removed the regular brackets. If we want regular brackets, they should be outside like this "({ [currencyCode]: { currencyCode , amount } })"

amount: PropTypes.string.isRequired,
currencyCode: PropTypes.string.isRequired,
})
),
Copy link
Contributor

Choose a reason for hiding this comment

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

The value should still be required

    value: PropTypes.objectOf(
      PropTypes.shape({
        amount: PropTypes.string.isRequired,
        currencyCode: PropTypes.string.isRequired,
      })
    ).isRequired,

currencyCode,
amount: values[currencyCode],
})
MoneyInput.convertToMoneyValue(values[currencyCode])
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this could be written as

static convertToMoneyValues = values =>
  Object.values(values).map(value => MoneyInput.convertToMoneyValue(value))

},
});
if (this.props.handleChange) this.props.handleChange(event);
if (this.props.onChange) this.props.onChange(event);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@montezume montezume left a comment

Choose a reason for hiding this comment

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

Looks good to me. We need to document the change in the changelog though.

@@ -9,17 +9,31 @@ class TestComponent extends React.Component {
static displayName = 'TestComponent';
static propTypes = {
id: PropTypes.string,
value: PropTypes.objectOf(PropTypes.string).isRequired,
value: PropTypes.objectOf(
PropTypes.shape({
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a breaking change here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do I do that?

Copy link
Contributor

Choose a reason for hiding this comment

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

No change required, when we make the changelog for the next version we just need to check this PR and document the changes 😄

@gnerkus
Copy link
Contributor Author

gnerkus commented Jan 10, 2019

Currently working of the tests.

@montezume
Copy link
Contributor

@gnerkus any update on this PR? Need any help?

@gnerkus gnerkus force-pushed the io-fix-onchange-localizedmoneyinput branch from 0e71ee4 to e18178f Compare January 14, 2019 09:42
@gnerkus
Copy link
Contributor Author

gnerkus commented Jan 14, 2019

Requested changes have been made. @montezume @dferber90 please review.

Copy link
Contributor

@montezume montezume left a comment

Choose a reason for hiding this comment

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

Can you merge @gnerkus ? 🎉

@montezume montezume merged commit e28d022 into master Jan 14, 2019
@montezume montezume deleted the io-fix-onchange-localizedmoneyinput branch January 14, 2019 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants