running-tools

A collection of tools for runners and their coaches
git clone https://git.ashermorgan.net/running-tools/
Log | Files | Refs | README

commit 8a5118d5ed95f30b5bb233e42578aad91a5598dd
parent d8631ec1466cb833d2d99270a0204cb673805917
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Thu, 11 Nov 2021 13:14:28 -0800

Save calculator input data to localStorage

Diffstat:
Msrc/utils/localStorage.js | 4+++-
Msrc/views/PaceCalculator.vue | 30+++++++++++++++++++++++++++---
Msrc/views/RaceCalculator.vue | 39++++++++++++++++++++++++++++++---------
3 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/src/utils/localStorage.js b/src/utils/localStorage.js @@ -29,7 +29,9 @@ function get(key, defaultValue) { * @param {Object} value The value * */ function set(key, value) { - localStorage.setItem(`${prefix}.${key}`, JSON.stringify(value)); + if (typeof localStorage !== 'undefined') { + localStorage.setItem(`${prefix}.${key}`, JSON.stringify(value)); + } } export default { diff --git a/src/views/PaceCalculator.vue b/src/views/PaceCalculator.vue @@ -27,6 +27,7 @@ <script> import paceUtils from '@/utils/paces'; +import storage from '@/utils/localStorage'; import unitUtils from '@/utils/units'; import DecimalInput from '@/components/DecimalInput.vue'; @@ -47,17 +48,17 @@ export default { /** * The input distance value */ - inputDistance: 5, + inputDistance: storage.get('pace-calculator-input-distance', 5), /** * The input distance unit */ - inputUnit: 'kilometers', + inputUnit: storage.get('pace-calculator-input-unit', 'kilometers'), /** * The input time value */ - inputTime: 20 * 60, + inputTime: storage.get('pace-calculator-input-time', 20 * 60), /** * The names of the distance units @@ -106,6 +107,29 @@ export default { }; }, + watch: { + /** + * Save input distance value + */ + inputDistance(newValue) { + storage.set('pace-calculator-input-distance', newValue); + }, + + /** + * Save input distance unit + */ + inputUnit(newValue) { + storage.set('pace-calculator-input-unit', newValue); + }, + + /** + * Save input time value + */ + inputTime(newValue) { + storage.set('pace-calculator-input-time', newValue); + }, + }, + computed: { /** * The input pace (in seconds per meter) diff --git a/src/views/RaceCalculator.vue b/src/views/RaceCalculator.vue @@ -82,17 +82,17 @@ export default { /** * The input distance value */ - inputDistance: 5, + inputDistance: storage.get('race-calculator-input-distance', 5), /** * The input distance unit */ - inputUnit: 'kilometers', + inputUnit: storage.get('race-calculator-input-unit', 'kilometers'), /** * The input time value */ - inputTime: 20 * 60, + inputTime: storage.get('race-calculator-input-time', 20 * 60), /** * The race prediction model @@ -273,22 +273,43 @@ export default { watch: { /** - * Save prediction model - */ + * Save input distance value + */ + inputDistance(newValue) { + storage.set('race-calculator-input-distance', newValue); + }, + + /** + * Save input distance unit + */ + inputUnit(newValue) { + storage.set('race-calculator-input-unit', newValue); + }, + + /** + * Save input time value + */ + inputTime(newValue) { + storage.set('race-calculator-input-time', newValue); + }, + + /** + * Save prediction model + */ model(newValue) { storage.set('race-calculator-model', newValue); }, /** - * Save Riegel Model exponent - */ + * Save Riegel Model exponent + */ riegelExponent(newValue) { storage.set('race-calculator-riegel-exponent', newValue); }, /** - * Save advanced options state - */ + * Save advanced options state + */ showAdvancedOptions(newValue) { storage.set('race-calculator-show-advanced-options', newValue); },