2022-08-06 02:49:30 +00:00
|
|
|
import React, {useRef, useState} from 'react';
|
2022-08-20 04:38:18 +00:00
|
|
|
import {ScrollView, StyleSheet, Text} from 'react-native';
|
2022-07-20 03:48:48 +00:00
|
|
|
import {Button, TextInput} from 'react-native-paper';
|
|
|
|
import Set from './set';
|
|
|
|
|
|
|
|
export default function SetForm({
|
|
|
|
save,
|
|
|
|
set,
|
2022-08-24 00:46:47 +00:00
|
|
|
next,
|
2022-07-20 03:48:48 +00:00
|
|
|
}: {
|
|
|
|
set: Set;
|
|
|
|
save: (set: Set) => void;
|
2022-08-24 00:46:47 +00:00
|
|
|
next?: string;
|
2022-07-20 03:48:48 +00:00
|
|
|
}) {
|
|
|
|
const [name, setName] = useState(set.name);
|
|
|
|
const [reps, setReps] = useState(set.reps.toString());
|
|
|
|
const [weight, setWeight] = useState(set.weight.toString());
|
|
|
|
const [unit, setUnit] = useState(set.unit);
|
2022-08-07 03:09:45 +00:00
|
|
|
const [selection, setSelection] = useState({
|
|
|
|
start: 0,
|
|
|
|
end: set.reps.toString().length,
|
|
|
|
});
|
2022-07-20 03:48:48 +00:00
|
|
|
const weightRef = useRef<any>(null);
|
2022-08-23 03:58:44 +00:00
|
|
|
const repsRef = useRef<any>(null);
|
2022-07-20 03:48:48 +00:00
|
|
|
const handleSubmit = () => {
|
2022-08-23 03:58:44 +00:00
|
|
|
if (!name) return;
|
2022-07-20 03:48:48 +00:00
|
|
|
save({
|
|
|
|
name,
|
|
|
|
reps: Number(reps),
|
2022-08-20 04:38:18 +00:00
|
|
|
created: set.created,
|
2022-07-20 03:48:48 +00:00
|
|
|
weight: Number(weight),
|
|
|
|
id: set.id,
|
|
|
|
unit,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ScrollView style={{height: '90%'}}>
|
2022-08-20 04:38:18 +00:00
|
|
|
<TextInput
|
|
|
|
style={styles.marginBottom}
|
|
|
|
label="Name"
|
|
|
|
value={name}
|
|
|
|
onChangeText={setName}
|
|
|
|
autoCorrect={false}
|
|
|
|
selectTextOnFocus
|
2022-08-23 03:58:44 +00:00
|
|
|
autoFocus={!name}
|
|
|
|
onSubmitEditing={() => repsRef.current?.focus()}
|
2022-08-20 04:38:18 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.marginBottom}
|
|
|
|
label="Reps"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={reps}
|
|
|
|
onChangeText={setReps}
|
|
|
|
onSubmitEditing={() => weightRef.current?.focus()}
|
|
|
|
selection={selection}
|
|
|
|
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
|
2022-08-23 03:58:44 +00:00
|
|
|
autoFocus={!!name}
|
2022-08-20 04:38:18 +00:00
|
|
|
selectTextOnFocus
|
|
|
|
blurOnSubmit={false}
|
2022-08-23 03:58:44 +00:00
|
|
|
ref={repsRef}
|
2022-08-20 04:38:18 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.marginBottom}
|
|
|
|
label="Weight"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={weight}
|
|
|
|
onChangeText={setWeight}
|
|
|
|
onSubmitEditing={handleSubmit}
|
|
|
|
ref={weightRef}
|
|
|
|
selectTextOnFocus
|
|
|
|
/>
|
|
|
|
<TextInput
|
2022-07-20 03:48:48 +00:00
|
|
|
style={styles.marginBottom}
|
2022-08-20 04:38:18 +00:00
|
|
|
label="Unit (kg)"
|
|
|
|
value={unit}
|
|
|
|
onChangeText={setUnit}
|
|
|
|
onSubmitEditing={handleSubmit}
|
|
|
|
selectTextOnFocus
|
2022-07-20 03:48:48 +00:00
|
|
|
/>
|
2022-08-24 00:46:47 +00:00
|
|
|
{set.created && (
|
|
|
|
<Text style={{marginBottom: 10}}>
|
|
|
|
Created: {set.created?.replace('T', ' ')}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
{next && <Text>Next: {next}</Text>}
|
2022-07-20 03:48:48 +00:00
|
|
|
</ScrollView>
|
2022-08-23 03:58:44 +00:00
|
|
|
<Button
|
|
|
|
disabled={!name}
|
|
|
|
mode="contained"
|
|
|
|
icon="save"
|
|
|
|
onPress={handleSubmit}>
|
2022-07-20 03:48:48 +00:00
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
marginBottom: {
|
|
|
|
marginBottom: 10,
|
|
|
|
},
|
|
|
|
});
|