Massive/SetForm.tsx

111 lines
2.9 KiB
TypeScript
Raw Normal View History

import React, {useEffect, useRef, useState} from 'react';
import {ScrollView} from 'react-native';
import {Button, Text} from 'react-native-paper';
import {getSets} from './db';
2022-08-26 01:54:51 +00:00
import MassiveInput from './MassiveInput';
2022-07-20 03:48:48 +00:00
import Set from './set';
export default function SetForm({
save,
set,
workouts,
2022-07-20 03:48:48 +00:00
}: {
set: Set;
save: (set: Set) => void;
workouts?: 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-28 08:55:12 +00:00
const [uri, setUri] = useState(set.image);
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-09-01 01:59:06 +00:00
2022-09-01 01:12:39 +00:00
useEffect(() => {
console.log('SetForm.useEffect:', {uri, name: set.name});
if (!uri)
getSets({search: set.name, limit: 1, offset: 0}).then(sets =>
setUri(sets[0]?.image),
);
}, [uri, set.name]);
2022-09-01 01:12:39 +00:00
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),
weight: Number(weight),
id: set.id,
unit,
2022-08-28 08:55:12 +00:00
image: uri,
2022-07-20 03:48:48 +00:00
});
};
return (
<>
<ScrollView style={{height: '90%'}}>
2022-08-26 01:54:51 +00:00
<MassiveInput
label="Name"
value={name}
onChangeText={setName}
autoCorrect={false}
2022-08-23 03:58:44 +00:00
autoFocus={!name}
onSubmitEditing={() => repsRef.current?.focus()}
/>
2022-08-26 01:54:51 +00:00
<MassiveInput
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}
blurOnSubmit={false}
2022-08-26 01:54:51 +00:00
innerRef={repsRef}
/>
2022-08-26 01:54:51 +00:00
<MassiveInput
label="Weight"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onSubmitEditing={handleSubmit}
2022-08-26 01:54:51 +00:00
innerRef={weightRef}
/>
2022-08-26 01:54:51 +00:00
<MassiveInput
label="Unit (kg)"
value={unit}
onChangeText={setUnit}
onSubmitEditing={handleSubmit}
2022-07-20 03:48:48 +00:00
/>
2022-08-28 08:55:12 +00:00
<Text style={{marginBottom: 10}}>
{workouts?.map((workout, index) => (
2022-08-25 02:16:30 +00:00
<React.Fragment key={workout}>
<Text
style={{
fontWeight: workout === name ? 'bold' : 'normal',
textDecorationLine: workout === name ? 'underline' : 'none',
}}>
{workout}
</Text>
{index === workouts.length - 1 ? '.' : ', '}
2022-08-25 02:16:30 +00:00
</React.Fragment>
))}
</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>
</>
);
}