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: Add Decimal Formatting to Units and Cost - 1529 #1606

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"react-hook-form": "^7.49.2",
"react-i18next": "^14.0.3",
"react-input-mask": "^2.0.4",
"react-number-format": "^5.4.3",
"react-quill": "^2.0.0",
"react-router-dom": "^6.21.1",
"react-snowfall": "^1.2.1",
Expand Down
99 changes: 68 additions & 31 deletions frontend/src/views/Transfers/components/TransferDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Controller, useFormContext } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { LabelBox } from './LabelBox'
import { calculateTotalValue } from '@/utils/formatters'
import { NumericFormat } from 'react-number-format'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the formatter formatNumberWithCommas not good for formatting in Transfers? Just thinking on the overhead of updating dependencies when updating React library. If we have an existing formatter, I would prefer using it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I see! that was my initial thought to use number formatters to achieve it, but I faced issues with form validation and conversion, which is why I decided to go with a new solution.


export const TransferDetails = () => {
const { t } = useTranslation(['common', 'transfer', 'organization'])
Expand All @@ -22,10 +23,12 @@ export const TransferDetails = () => {
register,
control,
watch,
formState: { errors }
formState: { errors },
setValue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need setValue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice catch! We don't really need it.

} = useFormContext()
const { data: currentUser } = useCurrentUser()
const { data: orgData } = useRegExtOrgs()

const organizations =
orgData?.map((org) => ({
value: parseInt(org.organizationId),
Expand Down Expand Up @@ -65,6 +68,7 @@ export const TransferDetails = () => {
)
)
}

return (
<BCBox data-test="transfer-details">
<LabelBox label={t('transfer:detailsLabel')}>
Expand All @@ -73,15 +77,29 @@ export const TransferDetails = () => {
{currentUser?.organization?.name}
</BCTypography>
{` ${t('transfer:transfers')} `}
<TextField
inputProps={{ 'data-test': 'quantity' }}
{...register('quantity')}
placeholder={t('common:quantity')}
size="small"
error={!!errors.quantity}
helperText={errors.quantity?.message}
sx={{ width: '6rem', marginInline: '0.2rem', bottom: '0.2rem' }}

<Controller
name="quantity"
control={control}
render={({ field: { onChange, onBlur, value, name, ref } }) => (
<NumericFormat
customInput={TextField}
thousandSeparator
value={value}
onValueChange={(vals) => onChange(vals.floatValue)}
onBlur={onBlur}
name={name}
inputRef={ref}
inputProps={{ 'data-test': 'quantity' }}
placeholder={t('common:quantity')}
size="small"
error={!!errors.quantity}
helperText={errors.quantity?.message}
sx={{ width: '6rem', marginInline: '0.2rem', bottom: '0.2rem' }}
/>
)}
/>

{t('transfer:complianceUnitsTo')}
<FormControl
sx={{
Expand Down Expand Up @@ -149,30 +167,48 @@ export const TransferDetails = () => {
/>
{renderError('toOrganizationId')}
</FormControl>

{t('transfer:for')}
<TextField
id="price-per-unit"
{...register('pricePerUnit')}
placeholder={t('transfer:fairMarketText')}
size="small"
error={!!errors.pricePerUnit}
helperText={errors.pricePerUnit?.message}
sx={{
minWidth: '25rem',
marginInline: '0.2rem',
bottom: '0.2rem'
}}
InputProps={{
startAdornment: (
<InputAdornment position="start">$</InputAdornment>
),
style: { textAlign: 'right' }
}}
inputProps={{
maxLength: 13,
'data-test': 'price-per-unit'
}}

<Controller
name="pricePerUnit"
control={control}
render={({ field: { onChange, onBlur, value, name, ref } }) => (
<NumericFormat
id="price-per-unit"
customInput={TextField}
thousandSeparator
decimalScale={2}
fixedDecimalScale={false}
prefix=""
value={value}
onValueChange={(vals) => onChange(vals.floatValue)}
onBlur={onBlur}
name={name}
inputRef={ref}
placeholder={t('transfer:fairMarketText')}
size="small"
error={!!errors.pricePerUnit}
helperText={errors.pricePerUnit?.message}
sx={{
minWidth: '25rem',
marginInline: '0.2rem',
bottom: '0.2rem'
}}
InputProps={{
startAdornment: (
<InputAdornment position="start">$</InputAdornment>
),
style: { textAlign: 'right' }
}}
inputProps={{
maxLength: 13,
'data-test': 'price-per-unit'
}}
/>
)}
/>

{t('transfer:totalValueText')}
<BCTypography
variant="body4"
Expand All @@ -187,6 +223,7 @@ export const TransferDetails = () => {
})} CAD.`}
</BCTypography>
</BCTypography>

<BCTypography
variant="body4"
component="div"
Expand Down