Validate reps+weight on EditSet

Numbers shouldn't contain dashes, spaces or commas.
This commit is contained in:
Brandon Presley 2023-08-12 15:22:00 +12:00
parent c97ba1151e
commit 44283fc990
2 changed files with 116 additions and 95 deletions

View File

@ -1,84 +1,85 @@
import { DateTimePickerAndroid } from '@react-native-community/datetimepicker'
import { DateTimePickerAndroid } from "@react-native-community/datetimepicker";
import {
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native'
import { format } from 'date-fns'
import { useCallback, useRef, useState } from 'react'
import { NativeModules, TextInput, View } from 'react-native'
import DocumentPicker from 'react-native-document-picker'
import { Button, Card, TouchableRipple } from 'react-native-paper'
import AppInput from './AppInput'
import ConfirmDialog from './ConfirmDialog'
import { MARGIN, PADDING } from './constants'
import { getNow, setRepo, settingsRepo } from './db'
import GymSet from './gym-set'
import { HomePageParams } from './home-page-params'
import Settings from './settings'
import StackHeader from './StackHeader'
import { toast } from './toast'
} from "@react-navigation/native";
import { format } from "date-fns";
import { useCallback, useRef, useState } from "react";
import { NativeModules, TextInput, View } from "react-native";
import DocumentPicker from "react-native-document-picker";
import { Button, Card, TouchableRipple } from "react-native-paper";
import AppInput from "./AppInput";
import ConfirmDialog from "./ConfirmDialog";
import { MARGIN, PADDING } from "./constants";
import { getNow, setRepo, settingsRepo } from "./db";
import GymSet from "./gym-set";
import { HomePageParams } from "./home-page-params";
import Settings from "./settings";
import StackHeader from "./StackHeader";
import { toast } from "./toast";
import { fixNumeric } from "./fix-numeric";
export default function EditSet() {
const { params } = useRoute<RouteProp<HomePageParams, 'EditSet'>>()
const { set } = params
const navigation = useNavigation()
const [settings, setSettings] = useState<Settings>({} as Settings)
const [name, setName] = useState(set.name)
const [reps, setReps] = useState(set.reps?.toString())
const [weight, setWeight] = useState(set.weight?.toString())
const [newImage, setNewImage] = useState(set.image)
const [unit, setUnit] = useState(set.unit)
const { params } = useRoute<RouteProp<HomePageParams, "EditSet">>();
const { set } = params;
const navigation = useNavigation();
const [settings, setSettings] = useState<Settings>({} as Settings);
const [name, setName] = useState(set.name);
const [reps, setReps] = useState(set.reps?.toString());
const [weight, setWeight] = useState(set.weight?.toString());
const [newImage, setNewImage] = useState(set.image);
const [unit, setUnit] = useState(set.unit);
const [created, setCreated] = useState<Date>(
set.created ? new Date(set.created) : new Date(),
)
const [createdDirty, setCreatedDirty] = useState(false)
const [showRemove, setShowRemove] = useState(false)
const [removeImage, setRemoveImage] = useState(false)
const weightRef = useRef<TextInput>(null)
const repsRef = useRef<TextInput>(null)
const unitRef = useRef<TextInput>(null)
set.created ? new Date(set.created) : new Date()
);
const [createdDirty, setCreatedDirty] = useState(false);
const [showRemove, setShowRemove] = useState(false);
const [removeImage, setRemoveImage] = useState(false);
const weightRef = useRef<TextInput>(null);
const repsRef = useRef<TextInput>(null);
const unitRef = useRef<TextInput>(null);
const [selection, setSelection] = useState({
start: 0,
end: set.reps?.toString().length,
})
});
useFocusEffect(
useCallback(() => {
settingsRepo.findOne({ where: {} }).then(setSettings)
}, []),
)
settingsRepo.findOne({ where: {} }).then(setSettings);
}, [])
);
const startTimer = useCallback(
async (value: string) => {
if (!settings.alarm) return
const first = await setRepo.findOne({ where: { name: value } })
const milliseconds = (first?.minutes ?? 3) * 60 * 1000 +
(first?.seconds ?? 0) * 1000
if (milliseconds) NativeModules.AlarmModule.timer(milliseconds)
if (!settings.alarm) return;
const first = await setRepo.findOne({ where: { name: value } });
const milliseconds =
(first?.minutes ?? 3) * 60 * 1000 + (first?.seconds ?? 0) * 1000;
if (milliseconds) NativeModules.AlarmModule.timer(milliseconds);
},
[settings],
)
[settings]
);
const added = useCallback(
async (value: GymSet) => {
startTimer(value.name)
console.log(`${EditSet.name}.add`, { set: value })
if (!settings.notify) return
startTimer(value.name);
console.log(`${EditSet.name}.add`, { set: value });
if (!settings.notify) return;
if (
value.weight > set.weight ||
(value.reps > set.reps && value.weight === set.weight)
) {
toast('Great work King! That\'s a new record.')
toast("Great work King! That's a new record.");
}
},
[startTimer, set, settings],
)
[startTimer, set, settings]
);
const handleSubmit = async () => {
if (!name) return
if (!name) return;
const newSet: Partial<GymSet> = {
id: set.id,
@ -90,63 +91,63 @@ export default function EditSet() {
seconds: Number(set.seconds ?? 30),
sets: set.sets ?? 3,
hidden: false,
}
};
newSet.image = newImage
newSet.image = newImage;
if (!newImage && !removeImage) {
newSet.image = await setRepo.findOne({ where: { name } }).then((s) =>
s?.image
)
newSet.image = await setRepo
.findOne({ where: { name } })
.then((s) => s?.image);
}
if (createdDirty) newSet.created = created.toISOString()
if (typeof set.id !== 'number') newSet.created = await getNow()
if (createdDirty) newSet.created = created.toISOString();
if (typeof set.id !== "number") newSet.created = await getNow();
const saved = await setRepo.save(newSet)
if (typeof set.id !== 'number') added(saved)
navigation.goBack()
}
const saved = await setRepo.save(newSet);
if (typeof set.id !== "number") added(saved);
navigation.goBack();
};
const changeImage = useCallback(async () => {
const { fileCopyUri } = await DocumentPicker.pickSingle({
type: DocumentPicker.types.images,
copyTo: 'documentDirectory',
})
if (fileCopyUri) setNewImage(fileCopyUri)
}, [])
copyTo: "documentDirectory",
});
if (fileCopyUri) setNewImage(fileCopyUri);
}, []);
const handleRemove = useCallback(async () => {
setNewImage('')
setRemoveImage(true)
setShowRemove(false)
}, [])
setNewImage("");
setRemoveImage(true);
setShowRemove(false);
}, []);
const pickDate = useCallback(() => {
DateTimePickerAndroid.open({
value: created,
onChange: (_, date) => {
if (date === created) return
setCreated(date)
setCreatedDirty(true)
if (date === created) return;
setCreated(date);
setCreatedDirty(true);
DateTimePickerAndroid.open({
value: date,
onChange: (__, time) => setCreated(time),
mode: 'time',
})
mode: "time",
});
},
mode: 'date',
})
}, [created])
mode: "date",
});
}, [created]);
return (
<>
<StackHeader
title={typeof set.id === 'number' ? 'Edit set' : 'Add set'}
title={typeof set.id === "number" ? "Edit set" : "Add set"}
/>
<View style={{ padding: PADDING, flex: 1 }}>
<AppInput
label='Name'
label="Name"
value={name}
onChangeText={setName}
autoCorrect={false}
@ -155,10 +156,14 @@ export default function EditSet() {
/>
<AppInput
label='Reps'
keyboardType='numeric'
label="Reps"
keyboardType="numeric"
value={reps}
onChangeText={setReps}
onChangeText={(newReps) => {
const fixed = fixNumeric(newReps);
setReps(fixed);
if (fixed.length !== newReps.length) toast("Reps must be a number");
}}
onSubmitEditing={() => weightRef.current?.focus()}
selection={selection}
onSelectionChange={(e) => setSelection(e.nativeEvent.selection)}
@ -166,18 +171,23 @@ export default function EditSet() {
/>
<AppInput
label='Weight'
keyboardType='numeric'
label="Weight"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onChangeText={(newWeight) => {
const fixed = fixNumeric(newWeight);
setWeight(fixed);
if (fixed.length !== newWeight.length)
toast("Weight must be a number");
}}
onSubmitEditing={handleSubmit}
innerRef={weightRef}
/>
{settings.showUnit && (
<AppInput
autoCapitalize='none'
label='Unit'
autoCapitalize="none"
label="Unit"
value={unit}
onChangeText={setUnit}
innerRef={unitRef}
@ -186,8 +196,8 @@ export default function EditSet() {
{settings.showDate && (
<AppInput
label='Created'
value={format(created, settings.date || 'P')}
label="Created"
value={format(created, settings.date || "P")}
onPressOut={pickDate}
/>
)}
@ -206,7 +216,7 @@ export default function EditSet() {
<Button
style={{ marginBottom: MARGIN }}
onPress={changeImage}
icon='add-photo-alternate'
icon="add-photo-alternate"
>
Image
</Button>
@ -215,8 +225,8 @@ export default function EditSet() {
<Button
disabled={!name}
mode='outlined'
icon='save'
mode="outlined"
icon="save"
style={{ margin: MARGIN }}
onPress={handleSubmit}
>
@ -224,7 +234,7 @@ export default function EditSet() {
</Button>
<ConfirmDialog
title='Remove image'
title="Remove image"
onOk={handleRemove}
show={showRemove}
setShow={setShowRemove}
@ -232,5 +242,5 @@ export default function EditSet() {
Are you sure you want to remove the image?
</ConfirmDialog>
</>
)
);
}

11
fix-numeric.ts Normal file
View File

@ -0,0 +1,11 @@
export const fixNumeric = (text: string) => {
let newText = text.replace(/[^0-9.-]/g, "");
let parts = newText.split(".");
if (parts.length > 2) {
newText = parts[0] + "." + parts.slice(1).join("");
}
if (newText.startsWith("-")) {
newText = "-" + newText.slice(1).replace(/-/g, "");
}
return newText;
};