-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
326 additions
and
32 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"module": "esnext", | ||
"jsx": "react", | ||
"baseUrl": "./" | ||
}, | ||
|
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
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,6 @@ | ||
import { useRef } from 'react'; | ||
import { debounce } from 'lodash-es'; | ||
|
||
export default function useDebounce(handler) { | ||
return useRef(debounce(handler, 250)).current; | ||
} |
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,67 @@ | ||
import { useFormik } from 'formik'; | ||
import { uniqueId } from 'lodash-es'; | ||
|
||
import { convert } from 'utils/units'; | ||
|
||
const convertRegex = /([\d.-]+)\s+(\w+)\s+in\s+(\w+)/; | ||
|
||
export default function useScaleForm(onHistoryPush) { | ||
return useFormik({ | ||
initialValues: { | ||
conversion: '' | ||
}, | ||
onSubmit: (vals, { setFieldError }) => { | ||
if (convertRegex.test(vals.conversion)) { | ||
const [, rawValue, unit, targetUnit] = | ||
vals.conversion.match(convertRegex); | ||
|
||
if (!rawValue || !unit || !targetUnit) { | ||
return setFieldError('conversion', 'Could not parse input.'); | ||
} | ||
|
||
if (!convert().from(unit).possibilities().includes(targetUnit)) { | ||
return setFieldError( | ||
'conversion', | ||
`Cannot convert ${unit} to ${targetUnit}` | ||
); | ||
} | ||
|
||
const value = parseFloat(rawValue); | ||
|
||
if (isNaN(value)) { | ||
return setFieldError('conversion', `Could not parse value.`); | ||
} | ||
|
||
onHistoryPush({ | ||
id: uniqueId(), | ||
unit, | ||
targetUnit, | ||
value | ||
}); | ||
} else { | ||
const [rawValue, unit] = vals.conversion.split(/\s+/); | ||
|
||
if (!rawValue || !unit) { | ||
return setFieldError('conversion', 'Could not parse input.'); | ||
} | ||
|
||
const value = parseFloat(rawValue); | ||
|
||
if (isNaN(value)) { | ||
return setFieldError('conversion', `Could not parse value.`); | ||
} | ||
|
||
if (!convert().possibilities('length').includes(unit)) { | ||
return setFieldError('conversion', `Unsupported unit ${unit}`); | ||
} | ||
|
||
onHistoryPush({ | ||
id: uniqueId(), | ||
unit, | ||
targetUnit: unit, | ||
value | ||
}); | ||
} | ||
} | ||
}); | ||
} |
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,37 @@ | ||
import { useCallback } from 'react'; | ||
import useLocalStorageState from 'use-local-storage-state'; | ||
|
||
import { convert } from 'utils/units'; | ||
|
||
const calculate = (entry, denominator) => ({ | ||
...entry, | ||
result: | ||
convert(entry.value).from(entry.unit).to(entry.targetUnit) * | ||
(1 / denominator) | ||
}); | ||
|
||
export default function useScaleHistory() { | ||
const [denominator, setDenominator] = useLocalStorageState('denominator', { | ||
defaultValue: 1 | ||
}); | ||
const [entries, setEntries] = useLocalStorageState('entries', { | ||
defaultValue: [] | ||
}); | ||
const clearEntries = useCallback(() => setEntries([]), [setEntries]); | ||
const pushEntry = useCallback( | ||
(entry) => | ||
setEntries((original) => [...original, calculate(entry, denominator)]), | ||
[setEntries, denominator] | ||
); | ||
const updateDenominator = useCallback( | ||
(value) => { | ||
setEntries((original) => | ||
original.map((entry) => calculate(entry, value)) | ||
); | ||
setDenominator(value); | ||
}, | ||
[setEntries, setDenominator] | ||
); | ||
|
||
return { entries, denominator, pushEntry, clearEntries, updateDenominator }; | ||
} |
Oops, something went wrong.