Skip to content

Commit

Permalink
fix findings
Browse files Browse the repository at this point in the history
  • Loading branch information
pusztaienike committed Mar 18, 2021
1 parent 05ac61f commit 7586ec8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { returnValueWithUnit } from '@sensenet/controls-react'
import { formatSize } from '@sensenet/controls-react'
import { createStyles, makeStyles, Paper, useTheme } from '@material-ui/core'
import React from 'react'
import { useWidgetStyles } from '../../globalStyles'
Expand Down Expand Up @@ -42,7 +42,7 @@ export const StorageWidget: React.FunctionComponent<StorageWidgetProps> = (props
<span>{localization.storage}</span>
<span>
{localization.used(
returnValueWithUnit(allUsage),
formatSize(allUsage),
`${numberFormatter.format(round(props.data.subscription.plan.limitations.storage / 1024))} GB`,
)}
</span>
Expand Down
20 changes: 2 additions & 18 deletions packages/sn-controls-react/src/fieldcontrols/file-size.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,10 @@ import InputAdornment from '@material-ui/core/InputAdornment'
import TextField from '@material-ui/core/TextField'
import Typography from '@material-ui/core/Typography'
import React, { useState } from 'react'
import { changeTemplatedValue } from '../helpers'
import { changeTemplatedValue, formatSize } from '../helpers'
import { ReactClientFieldSetting } from './client-field-setting'
import { defaultLocalization } from './localization'

const units = ['byte', 'KB', 'MB', 'GB', 'TB']

export const round = (num: number, precision = 1) => {
const multiplier = Math.pow(10, precision)
return Math.round((num + Number.EPSILON) * multiplier) / multiplier
}

export const returnValueWithUnit = (fieldValueNumber: number, index = 0): string => {
const inHigherUnit = round(fieldValueNumber / 1024)
if (inHigherUnit >= 1 && units.length > index + 1) {
return returnValueWithUnit(inHigherUnit, index + 1)
} else {
return `${fieldValueNumber} ${units[index]}`
}
}

/**
* Field control that represents a Number field. Available values will be populated from the FieldSettings.
*/
Expand Down Expand Up @@ -99,7 +83,7 @@ export const FileSizeField: React.FC<ReactClientFieldSetting<NumberFieldSetting>
</Typography>
<Typography variant="body1" gutterBottom={true}>
{props.fieldValue && props.fieldValue !== '0' ? (
<>{returnValueWithUnit(toNumber(props.fieldValue)!, 0)}</>
<>{formatSize(toNumber(props.fieldValue)!, 0)}</>
) : (
localization.noValue
)}
Expand Down
16 changes: 16 additions & 0 deletions packages/sn-controls-react/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ export const isFullWidthField = (field: { fieldSettings: FieldSetting }, content
field.fieldSettings.Type === 'LongTextFieldSetting'
)
}

const units = ['byte', 'KB', 'MB', 'GB', 'TB']

export const round = (num: number, precision = 1) => {
const multiplier = Math.pow(10, precision)
return Math.round((num + Number.EPSILON) * multiplier) / multiplier
}

export const formatSize = (fieldValueNumber: number, index = 0): string => {
const inHigherUnit = round(fieldValueNumber / 1024)
if (inHigherUnit >= 1 && units.length > index + 1) {
return formatSize(inHigherUnit, index + 1)
} else {
return `${fieldValueNumber} ${units[index]}`
}
}

0 comments on commit 7586ec8

Please sign in to comment.