-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
21 changed files
with
560 additions
and
244 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
identity-service/local-storage/%40audius%2Flibs%3Adiscovery-node-timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"endpoint":"http://audius-protocol-discovery-provider-1","timestamp":1695756968160} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const PRECISION = 2 | ||
|
||
export type DecimalUtilOptions = { | ||
/** Number of digits used on the right side of human-readable values. Defaults to 2 */ | ||
precision?: number | ||
} | ||
|
||
/** | ||
* Helper to parse values from text input and give numeric values and human readable values out. Removes characters that are not numbers or decimals. | ||
*/ | ||
export const filterDecimalString = ( | ||
value: string, | ||
{ precision = PRECISION }: DecimalUtilOptions = {} | ||
) => { | ||
const input = value.replace(/[^0-9.]+/g, '') | ||
// Regex to grab the whole and decimal parts of the number, stripping duplicate '.' characters | ||
const match = input.match(/^(?<whole>\d*)(?<dot>.)?(?<decimal>\d*)/) | ||
const { whole, decimal, dot } = match?.groups || {} | ||
|
||
// Conditionally render the decimal part, and only for the number of decimals specified | ||
const stringAmount = dot | ||
? `${whole}.${(decimal ?? '').substring(0, precision)}` | ||
: whole | ||
return { human: stringAmount, value: Number(stringAmount) * 10 ** precision } | ||
} | ||
|
||
/** | ||
* Helper to pad a decimal value to a specified precision, useful for blur events on a token input | ||
*/ | ||
export const padDecimalValue = ( | ||
value: string, | ||
{ precision = PRECISION }: Pick<DecimalUtilOptions, 'precision'> = {} | ||
) => { | ||
const [whole, decimal] = value.split('.') | ||
|
||
const paddedDecimal = (decimal ?? '') | ||
.substring(0, precision) | ||
.padEnd(precision, '0') | ||
return `${whole.length > 0 ? whole : '0'}.${paddedDecimal}` | ||
} | ||
|
||
/** Converts an integer value representing a decimal number to it's human-readable form. (ex. 100 => 1.00) */ | ||
export const decimalIntegerToHumanReadable = ( | ||
value: number, | ||
{ precision = PRECISION }: DecimalUtilOptions = {} | ||
) => { | ||
return (value / 10 ** precision).toFixed(precision) | ||
} | ||
|
||
/** Converts a string representing a decimal number to its equivalent integer representation (ex. 1.00 => 100) */ | ||
export const decimalIntegerFromHumanReadable = ( | ||
value: string, | ||
{ precision = PRECISION }: DecimalUtilOptions = {} | ||
) => { | ||
return parseFloat(value) * 10 ** precision | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useState, useEffect, useCallback } from 'react' | ||
|
||
import { | ||
decimalIntegerFromHumanReadable, | ||
decimalIntegerToHumanReadable, | ||
filterDecimalString, | ||
padDecimalValue | ||
} from '@audius/common' | ||
import { useField } from 'formik' | ||
import type { | ||
NativeSyntheticEvent, | ||
TextInputChangeEventData, | ||
TextInputFocusEventData | ||
} from 'react-native' | ||
|
||
import { Text } from '../core/Text' | ||
|
||
import type { TextFieldProps } from './TextField' | ||
import { TextField } from './TextField' | ||
|
||
const messages = { | ||
dollars: '$' | ||
} | ||
|
||
/** Implements a Formik field for entering a price, including default dollar sign | ||
* adornment and conversion logic to/from human readable price. Internal value is stored | ||
* as an integer number of cents. | ||
*/ | ||
export const PriceField = (props: TextFieldProps) => { | ||
const [{ value }, , { setValue: setPrice }] = useField<number>(props.name) | ||
const [humanizedValue, setHumanizedValue] = useState( | ||
value ? decimalIntegerToHumanReadable(value) : null | ||
) | ||
|
||
useEffect(() => { | ||
if (humanizedValue !== null) { | ||
const dehumanizedValue = decimalIntegerFromHumanReadable(humanizedValue) | ||
if (value === undefined || dehumanizedValue !== value) { | ||
setPrice(dehumanizedValue) | ||
} | ||
} | ||
}, [value, humanizedValue, setPrice]) | ||
|
||
const handlePriceChange = useCallback( | ||
(e: NativeSyntheticEvent<TextInputChangeEventData>) => { | ||
const { human, value } = filterDecimalString(e.nativeEvent.text) | ||
setHumanizedValue(human) | ||
setPrice(value) | ||
}, | ||
[setPrice, setHumanizedValue] | ||
) | ||
|
||
const handlePriceBlur = useCallback( | ||
(e: NativeSyntheticEvent<TextInputFocusEventData>) => { | ||
setHumanizedValue(padDecimalValue(e.nativeEvent.text)) | ||
}, | ||
[] | ||
) | ||
|
||
return ( | ||
<TextField | ||
keyboardType='numeric' | ||
startAdornment={ | ||
<Text color='neutralLight2' weight='bold'> | ||
{messages.dollars} | ||
</Text> | ||
} | ||
{...props} | ||
value={humanizedValue ?? undefined} | ||
onChange={handlePriceChange} | ||
onBlur={handlePriceBlur} | ||
/> | ||
) | ||
} |
Oops, something went wrong.